# What Is a Circular Queue in Data Structure?

//

Heather Bennett

A circular queue is a type of data structure that follows the First-In-First-Out (FIFO) principle. It is similar to a regular queue, with the main difference being that the last element is connected to the first element to form a circle. This allows for efficient utilization of memory and avoids wastage.

How Does a Circular Queue Work?
A circular queue uses an array as its underlying data structure. It maintains two pointers, ‘front’ and ‘rear’, which represent the index of the first and last elements in the queue, respectively. Initially, both pointers are set to -1.

When an element is enqueued (added) into the circular queue, the ‘rear’ pointer is incremented by one, and the new element is inserted at that index. If ‘rear’ becomes equal to or greater than the size of the array, it wraps around to 0, thus creating a circular effect.

Similarly, when an element is dequeued (removed) from the circular queue, the ‘front’ pointer is incremented by one. If ‘front’ becomes equal to or greater than the size of the array, it also wraps around to 0.

The circular nature of this data structure allows for efficient utilization of memory space since it can overwrite previously dequeued elements once capacity is reached.

Advantages of Using a Circular Queue
1. Efficient Memory Usage: Unlike linear queues where memory may be wasted if elements are dequeued before reaching full capacity, circular queues efficiently utilize memory by reusing empty spaces. 2.

Constant Time Complexity: Enqueuing and dequeuing operations in a circular queue have a constant time complexity of O(1), regardless of its size. 3. Easy Implementation: The implementation of a circular queue using an array is straightforward compared to other complex data structures.

Limitations and Considerations
1. Fixed Size: Circular queues have a fixed size defined during initialization. This size cannot be changed dynamically.

2. Overflow and Underflow: Similar to regular queues, circular queues can suffer from overflow (enqueuing when the queue is full) or underflow (dequeuing when the queue is empty). Proper checks need to be implemented to avoid such situations.

### Implementation of Circular Queue in C++

To help you understand better, here’s an example of how a circular queue can be implemented in C++:

“`cpp
const int MAX_SIZE = 5;

class CircularQueue {
private:
int queue[MAX_SIZE];
int front;
int rear;

public:
CircularQueue() {
front = -1;
rear = -1;
}

bool isFull() {
return ((front == 0 && rear == MAX_SIZE – 1) || (rear == (front – 1) % (MAX_SIZE – 1)));
}

bool isEmpty() {
return (front == -1);
}

void enqueue(int item) {
if (isFull()) {
cout << "Queue is full. Unable to enqueue." << endl; return; } else if (isEmpty()) { front = rear = 0; } else if (rear == MAX_SIZE - 1 && front != 0) { rear = 0; } else { rear++; } queue[rear] = item; } void dequeue() { if (isEmpty()) { cout << "Queue is empty. Unable to dequeue." << endl; return; } if (front == rear) { front = rear = -1; } else if (front == MAX_SIZE - 1) { front = 0; } else { front++; } } int frontElement() { if (isEmpty()) { cout << "Queue is empty." << endl; return -1; } return queue[front]; } }; int main() { CircularQueue cq; cq.enqueue(10); cq.enqueue(20); cq.enqueue(30); cout << "Front element: " << cq.frontElement() << endl; cq.dequeue(); cout << "Front element after dequeue: " << cq.frontElement() << endl; return 0; } ``` In the above example, we define a circular queue class with methods for enqueue, dequeue, and checking if the queue is full or empty. The main function demonstrates how to use these methods.

• isFull() checks if the circular queue is full.
• isEmpty() checks if the circular queue is empty.
• enqueue(item) adds an element to the rear of the circular queue.
• dequeue() removes an element from the front of the circular queue.
• frontElement() returns the element at the front of the circular queue without removing it.

### In Conclusion

A circular queue is a useful data structure that allows efficient utilization of memory and provides constant time complexity for enqueuing and dequeuing operations. Understanding its implementation will help you solve problems that require a FIFO approach with limited memory resources.

Remember to always consider edge cases such as overflow and underflow when using a circular queue. With proper handling, you can harness its benefits in your applications effectively.