Circular Queue
Algorithm:Circular Queue Operations Let us consider a circular queue,CQ with front side and rear side. Element will be inserted in rear side and will be deleted from front side. If rear is in the last position and front is in the middle position and there is any space in CQ then the item wlii be inserted in the 1st position.This algorithm helps us to perform insertion and deletion operation in the queue,CQ. Initially, front=rear=-1; Qinsert(CQ[1..N),front,rear,item) { if(front=(rear+1)%N) //checks if the Queue is full { print "Queue full" } else { rear=(rear+1)%N CQ[rear]=item //element is inserted in rear side if(front=-1) //if front is -1 then front is { initialized to 0 front=0 } } } Qdelete(CQ[1..N),front,rear) { if(front=-1 and rear=-1) //checks if the Queue is empty { print "Queue empty" } else { item=CQ[front] if(front=rear) //if front and rear is equal then they { both are initialized to -1 front=-1 rear=-1 } else { front=(front+1)%N //front is incremented and the element } in front deleted } return item } show(CQ[1..N),front,rear) { if(front=-1 and rear=-1) //checks if the Queue is empty { print "Queue empty" } else { for(i=front to rear) { print "CQ[i]" //elements are printed one by one from front to rear } } } C Program For Circular Queue Source Code: #include<stdio.h> #include<stdlib.h> #include<math.h> int Q[5],f=-1,r=-1; void CQinsert(int item) { if(f==(r+1)%5) { printf("\nQueue full."); } else { r=(r+1)%5; Q[r]=item; if(f==-1) { f=0; } } } int CQdelete() { int item; if(f==-1) { printf("\nQueue empty."); } else { item=Q[f]; if(f==r) { f=-1; r=-1; } else { f=(f+1)%5; } } return item; } void show() { int i=0; if(f==-1) { printf("\nQueue empty."); } else { printf("\nThe elements of the Queue are:"); if(f=f && i<=r) printf("\t%d",Q[i]); else printf("\t_"); } } else if(f==r) { for(i=0;i<=4;i++) { if(i==f) printf("\t%d",Q[i]); else printf("\t_"); } } else { for(i=0;i<=4;i++) { if(i<=r) printf("\t%d",Q[i]); else if(i>r && i %d",item); } break; case 3: show(); break; case 4: exit(0); } } } OUTPUT: 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:1 Enter item:11 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:1 Enter item:22 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:1 Enter item:33 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:1 Enter item:44 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:1 Enter item:55 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:3 The elements of the Queue are: 11 22 33 44 55 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:1 Enter item:66 Queue full. 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:2 The popped item is-> 11 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:2 The popped item is-> 22 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:2 The popped item is-> 33 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:1 Enter item:77 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:3 The elements of the Queue are: 77 _ _ 44 55 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:2 The popped item is-> 44 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:2 The popped item is-> 55 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:2 1.CQinsert 2.CQdelete 3.Show 4.Exit Enter your choice:3 Queue empty.
Tags:
Stack & Queue
0 comments