Write a program to implement a Circular Queue and show how it differs from a normal queue.

Data Structure

A queue is a linear data structure that follows the FIFO (First In, First Out) principle. Think of it like a line of people waiting at a ticket counter: the person who arrives first is served first. However, a normal queue has a limitation—when elements are removed from the front, the space is not reused. This can lead to inefficiencies.

Circular Queue vs Normal Queue
A queue is a FIFO (First In, First Out) data structure, but a normal queue does not efficiently utilize memory. A circular queue, however, connects the rear end back to the front, making better use of available space.

Point Circular Queue Normal Queue
1. Definition A circular queue is a linear data structure in which the last position is connected back to the first position to form a circle. A normal queue is a linear data structure where elements are added at the rear and removed from the front, and it uses a linear array or linked list.
2. Overflow Condition No overflow occurs until all elements are used, even if there is empty space in the beginning. Overflow occurs when the rear reaches the end of the queue, even if there is space in the front.
3. Memory Utilization Better memory utilization because it uses the empty space in a circular manner. Less efficient as it may lead to wasted space when the queue reaches the maximum size.
4. Rear Pointer The rear pointer moves in a circular fashion, returning to the front when it reaches the end. The rear pointer moves forward without wrapping around the queue.
5. Space Efficiency More space-efficient as it reuses the vacant spaces at the front. Less space-efficient because it doesn't reuse the space left at the front after dequeuing.
6. Operations Enqueue and Dequeue operations are implemented with wraparound logic. Enqueue and Dequeue operations are straightforward without wraparound.
7. Use Case Used in scenarios like CPU scheduling and buffer management where cyclic operations are required. Used in basic queue operations, such as task scheduling or managing requests in a first-come, first-serve order.
Implementation of Circular Queue in C++ #include #define SIZE 5 using namespace std; class CircularQueue { private: int arr[SIZE]; int front, rear; public: CircularQueue() { front = rear = -1; } // Check if the queue is full bool isFull() { return (front == 0 && rear == SIZE - 1) || (front == rear + 1); } // Check if the queue is empty bool isEmpty() { return front == -1; } // Insert an element void enqueue(int value) { if (isFull()) { cout << "Queue is Full\n"; return; } if (front == -1) front = 0; rear = (rear + 1) % SIZE; arr[rear] = value; cout << "Inserted: " << value << endl; } // Remove an element void dequeue() { if (isEmpty()) { cout << "Queue is Empty\n"; return; } cout << "Deleted: " << arr[front] << endl; if (front == rear) front = rear = -1; else front = (front + 1) % SIZE; } // Display the queue void display() { if (isEmpty()) { cout << "Queue is Empty\n"; return; } cout << "Queue elements: "; int i = front; while (true) { cout << arr[i] << " "; if (i == rear) break; i = (i + 1) % SIZE; } cout << endl; } }; int main() { CircularQueue cq; cq.enqueue(10); cq.enqueue(20); cq.enqueue(30); cq.enqueue(40); cq.enqueue(50); cq.display(); cq.dequeue(); cq.dequeue(); cq.display(); cq.enqueue(60); cq.display(); return 0; } Explanation of the Code 1. CircularQueue Class: Uses an array arr[] of fixed size SIZE. front and rear track positions. isFull() and isEmpty() functions check queue conditions. 2. Enqueue Operation: If full, it prints "Queue is Full." Else, it inserts an element and updates rear. 3. Dequeue Operation: If empty, it prints "Queue is Empty." Else, it removes an element and updates front. 4. Display Function: Iterates in a circular manner to show elements. Output of the Program Inserted: 10 Inserted: 20 Inserted: 30 Inserted: 40 Inserted: 50 Queue elements: 10 20 30 40 50 Deleted: 10 Deleted: 20 Queue elements: 30 40 50 Inserted: 60 Queue elements: 30 40 50 60 Conclusion A normal queue suffers from wasted space after deletions. A circular queue efficiently manages space by reusing vacant positions. This is done using modulo operations to wrap around. Thus, circular queues are widely used in memory management, scheduling algorithms, and buffering mechanisms.