//QueueArray #include<stdio.h> #include<stdbool.h> #include<stdlib.h> struct QueueType{ int front; int rear; int capacity; int *list; }; typedef struct QueueType queue; queue* newqueue (int capacity) { queue *self; self=(queue*)malloc(sizeof(queue)); self->capacity=capacity; self->list=(int*)malloc(sizeof(int)*capacity); self->rear=-1; self->front=0; return self; } bool enqueue (queue *self,int d) { if(self->rear+1==self->capacity) return false; else { self->rear++; self->list[self->rear]=d; } return true; } bool dequeue (queue *self, int *pelem) { if(self->front>self->rear) return false; else { *pelem=self->list[self->front]; self->front++; } return true; } void display(queue *self) { int i; if(self->front>self->rear) printf("Queue is empty\n"); else printf("queue is as follows\n"); for(i=self->front;i<=self->rear;i++) { printf("%d |",self->list[i]); } putchar('\n'); } int main () { int elem,d,choice; queue *ql; ql=newqueue(2); printf("1.enqueue\n2.dequeue\n3.dispaly\n4.exit\n"); do{ printf("Enter choice:"); scanf("%d",&choice); switch(choice) { case 1:printf("Enter data:"); scanf("%d",&d); enqueue(ql,d); break; case 2:dequeue(ql,&elem); printf("deletede elem %d \n",elem); break; case 3:display(ql); break; case 4:exit(0); break; } }while(true); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter