Circular Queue in C using Array
Here you will get the program code of Circular Queue in C programming using Array. It is simple program to understand the circular queue concept in in Data Structure.
What is Circular Queue?
A circular queue is an abstract data type that includes a collection of data and allows for data addition at the end of the queue and data removal at the beginning. Circular queues have a fixed size.
Circular queue follows FIFO(First In, First Out) principle. Items are added to the circular queue at the back end, and eliminated at the front end.
Program code of Circular Queue 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 | //Circular Queue in C using Array #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 20 int cq[MAX_SIZE]; int front = -1; int rear = -1; // Check if circular queue is empty int isEmpty() { return (front == -1 && rear == -1); } // Check if circular queue is full int isFull() { return ((rear + 1) % MAX_SIZE == front); } // add element to circular queue void enqueue(int value) { if (isFull()) { printf("Queue is full. Cannot enqueue.\n"); return; } if (isEmpty()) { front = 0; rear = 0; } else { rear = (rear + 1) % MAX_SIZE; } cq[rear] = value; } // Delete element to circular queue int dequeue() { if (isEmpty()) { printf("Queue is empty. Cannot dequeue.\n"); return -1; } int removedValue = cq[front]; if (front == rear) { front = -1; rear = -1; } else { front = (front + 1) % MAX_SIZE; } return removedValue; } // Display data of circular queue void display() { if (isEmpty()) { printf("\nQueue is empty.\n"); return; } int i = front; printf("\nCircular Queue elements: "); while (1) { printf("%d ", cq[i]); if (i == rear) { break; } i = (i + 1) % MAX_SIZE; } printf("\n"); } int main() { int choice, value; while (1) { printf("\nCircular Queue Menu:\n"); printf("1. Enqueue\n"); printf("2. Dequeue\n"); printf("3. Display\n"); printf("4. Quit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: if (isFull()) { printf("Queue is full. Cannot enqueue.\n"); } else { printf("Enter the value to enqueue: "); scanf("%d", &value); enqueue(value); printf("Enqueued: %d\n", value); } break; case 2: if (isEmpty()) { printf("\nQueue is empty. Cannot dequeue.\n"); } else { value = dequeue(); printf("Dequeued: %d\n", value); } break; case 3: display(); break; case 4: exit(0); // exit the program default: printf("\nInvalid choice. Please try again.\n"); } } return 0; } |
Output
Circular Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 1
Enter the value to enqueue: 100
Circular Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 1
Enter the value to enqueue: 200
Circular Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 1
Enter the value to enqueue: 300
Circular Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 3
Circular Queue elements: 100 200 300
Circular Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 2
Dequeued: 100
Circular Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 3
Circular Queue elements: 200 300
Circular Queue Menu:
1. Enqueue
2. Dequeue
3. Display
4. Quit
Enter your choice: 4