Circular Queue in Data Structure in C using Linked List
Here you will get and learn the program code of Circular Queue in Data Structure in C using Linked List.
What is Circular Queue in Data Structure?
Circular Queue in data structure works like a regular queue but with a circular arrangement of elements. It has a fixed size and consists of a front and rear end. When elements are dequeued (removed), the front moves forward, making space for new elements. It’s efficient for tasks with a fixed buffer size or when you want to reuse empty spaces.
It’s commonly used in situations where a continuous flow of data is required, like computer memory management and scheduling algorithms.
Program Code of Circular Queue in Data Structure in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | //Circular Queue in Data Structure in C using Linked List #include<stdio.h> #include<stdlib.h> struct queue{ int info; struct queue *link; }; struct queue *front=NULL,*rear=NULL; int count=0; //insert data in queue void push(int n) { struct queue *newnode; newnode=(struct queue*)malloc(sizeof(struct queue)); newnode->info=n; newnode->link=NULL; if(count==0) front=newnode; else rear->link=newnode; rear=newnode; rear->link=front; count++; } //Delete data in queue int pop(void) { int n; struct queue *temp; if(count==0) return (-1); count--; if(front==rear) { n=front->info; free(front); front=NULL; rear=NULL; }else { temp= front ; n = temp-> info ; front = front -> link ; rear -> link = front ; free ( temp ) ; } return n; } // count size of queue int size(void) { return count; } //print data void display(void) { struct queue *temp; int i; if(count==0) printf("Queue is Empty"); else { temp=front; for(i=0;i<count;i++) { printf("%d ",temp->info); temp=temp->link; } } printf("\n"); } int main() { int n,ch=10; while(ch!=0) { printf("\nCircular Queue using Linked List\n"); printf("1.Push\n"); printf("2.Pop\n"); printf("3.Size of Queue\n"); printf("4.Display List\n"); printf("0.EXIT\n"); printf("\nEnter Your Choice : "); scanf("%d",&ch); switch(ch) { case 1: { printf("Enter number to insert : \n"); scanf("%d",&n); push(n); break; } case 2: { n=pop(); if(n==-1) printf("Queue is empty\n"); else printf("%d Number is Poped\n",n); break; } case 3: { n=size(); printf("Size of queue is %d\n",n); break; } case 4: { printf("Queue is --> "); display(); } case 0: break; default: printf("Wrong Choice! \n"); break; } } } |
Output
Circular Queue using Linked List
1.Push
2.Pop
3.Size of Queue
4.Display List
0.EXIT
Enter Your Choice : 1
Enter number to insert :
100
Circular Queue using Linked List
1.Push
2.Pop
3.Size of Queue
4.Display List
0.EXIT
Enter Your Choice : 1
Enter number to insert :
200
Circular Queue using Linked List
1.Push
2.Pop
3.Size of Queue
4.Display List
0.EXIT
Enter Your Choice : 1
Enter number to insert :
300
Circular Queue using Linked List
1.Push
2.Pop
3.Size of Queue
4.Display List
0.EXIT
Enter Your Choice : 4
Queue is –> 100 200 300
Circular Queue using Linked List
1.Push
2.Pop
3.Size of Queue
4.Display List
0.EXIT
Enter Your Choice : 2
100 Number is Poped
Circular Queue using Linked List
1.Push
2.Pop
3.Size of Queue
4.Display List
0.EXIT
Enter Your Choice : 4
Queue is –> 200 300
Circular Queue using Linked List
1.Push
2.Pop
3.Size of Queue
4.Display List
0.EXIT
Enter Your Choice : 3
Size of queue is 2
Circular Queue using Linked List
1.Push
2.Pop
3.Size of Queue
4.Display List
0.EXIT
Enter Your Choice : 0