You are on page 1of 2

/*queues using arrays*/ #include<stdio.h> #include<stdlib.

h> struct queue { int f,r,size,*p; }; struct queue *create(int max) { struct queue *q; q=(struct queue *)malloc(sizeof(struct queue)); q->f=-1; q->r=-1; q->size=max; q->p=(int *)malloc(sizeof(int)*max); return q; } int isfull(struct queue *q) { if(q->r==q->size-1) return 1; return 0; } int isempty(struct queue *q) { if(q->f==-1) return 1; return 0; } void enqueue(struct queue *q,int x) { if(isfull(q)) printf("overflow"); else if(isempty(q)) { q->r=q->f=0; q->p[q->r]=x; } else q->p[++q->r]=x; } void dequeue(struct queue *q) { if(isempty(q)) printf("underflow"); else { printf("%d is deleted\n",q->p[q->f]); if(q->f==q->r) q->f=q->r=-1; else q->f++; } } void display(struct queue *q) { int i; if(!isempty(q)) for(i=q->f;i<=q->r;i++) printf("%d\t",q->p[i]);

} main() { struct queue *q=create(10); int ch,x; while(1) { printf("\nMENU\n"); printf("1:enqueue\n2:dequeue\n3:display\n"); printf("enter your choice"); scanf("%d",&ch); switch(ch) { case 1:printf("enter the element to insert"); scanf("%d",&x); enqueue(q,x); printf("\n"); display(q); break; case 2:dequeue(q); printf("\n"); break; case 3:display(q); break; default:exit(0); } } }

You might also like