If you liked the post, Share on Facebook, Tweet and Google Plus (use buttons above). You can also Subscribe to our feed via Email for free.
Home » Programming » Circular Queue Program in C using Array
Circular Queue Program in C using Array
Posted by
Akash
on 22 March 2012
This is a c language program code for data structure circular queue. The code is simple to understand and has 3 functions other than the main function. The functions insert(), del() and display() are for enqueue, dequeue and view(or traverse) operations respectively.
The program implementation is done using array.
//Program for circular queue #include<stdio.h> #include<conio.h> #define max 5 void insert(); void del(); void display(); int cq[max]; int front=-1,rear=-1; int main() { int choice; while(1) { printf("\nPress 1 to Enqueue\n"); printf("Press 2 to Dequeue\n"); printf("Press 3 to Display\n"); printf("Press 4 to Exit\n"); printf("Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: del(); break; case 3: display(); break; case 4: return 0; default: printf("\n Invalid Choice:"); break; } } getch(); } void insert() { int add_item; if((front==0 && rear==max-1)||(front==rear+1)) { printf("\n Queue Overflow.\n\n"); return; } if(front==-1) { front=0; rear=0; } else { if(rear==max-1) rear=0; else rear=rear+1; getch(); } printf("\n Input the element for insertion in queue:"); scanf("%d",&add_item); cq[rear]=add_item; printf("Element inserted."); } void del() { if(front==-1) { printf("\n Queue Underflow.\n\n"); return; } printf("\n Element deleted from the Queue is %d \n\n",cq[front]); if(front==rear) { front=-1; rear=-1; } else { if(front==max-1) { front=0; } else front=front+1; } } void display() { int frontpos=front; int rearpos= rear; if(front==-1) { printf("\n Queue is empty.\n"); return; } printf("\n Queue elements is: \n"); if(frontpos<=rearpos) { while(frontpos<=rearpos) { printf("%d ",cq[frontpos]); frontpos++; } } else { while(frontpos<=max-1) { printf("%d ",cq[frontpos]); frontpos++; } frontpos=0; while(frontpos<=rearpos) { printf("%d ",cq[frontpos]); frontpos++; } } printf("n"); }
0 comments:
Post a Comment