Circular Queue: Implementation and Applications In linear queue, when we delete any element, only front increment by one but position is not used later. So, when we perform more add and delete operations, memory wastage increases. But in circular queue, memory is utilized if we delete any element that position is used later due to its circular representation. In a standard queue data structure re-buffering problem occurs for each dequeue operation. To solve this problem by joining the front and rear ends of a queue to make the queue as a circular queue Circular queue is a linear data structure. It follows FIFO principle. In circular queue the last node is connected back to the first node to make a circle. Circular linked list fallow the First In First Out principle Elements are added at the rear end and the elements are deleted at front end of the queue Both the front and the rear pointers points to the beginning of the array. It is also called as Ring buffer. Items can inserted and deleted from a queue in O(1) time. Circular Queue can be created in three ways they are: Using single linked list Using double linked list Using arrays Using single linked list: It is an extension for the basic single linked list. In circular linked list Instead of storing a Null value in the last node of a single linked list, store the address of the 1st node (root) forms a circular linked list. Using circular linked list it is possible to directly traverse to the first node after reaching the last node. The following figure shows circular single linked list: Algorithm for Circular Queue: Declare and initialize necessary variables such as head = 0, tail = 0 etc. For enqueue operation, If head = (tail+1)%maxsize
print "Queue is Full" queue[tail] = item tail = (tail+1)%maxsize For enqueue of next data item, goto step 2 For dequeue operation, If head = tail print "Queue is Empty" Remove item i.e. item = queue[head] Set head = (head + 1)%MAXSIZE For next dequeue operation, goto step 4 Stop Source Code for Circular Queue: * C++ Program to Implement Circular Queue #include <iostream> #define MAX 5 using namespace std; * Class Circular Queue.class Circular_Queue private: int *cqueue_arr; int front, rear; public: Circular_Queue() cqueue_arr = new int [MAX]; rear = front = -1; * Insert into Circular Queue void insert(int item) if ((front == 0 && rear == MAX-1) (front == rear+1)) cout<<"queue Overflow \n"; return;
if (front == -1) front = 0; rear = 0; if (rear == MAX - 1) rear = 0; else rear = rear + 1; cqueue_arr[rear] = item ; * Delete from Circular Queue * void del() if (front == -1) cout<<"queue Underflow\n"; return ; cout<<"element deleted from queue is : "<<cqueue_arr[front]<<endl; if (front == rear) front = -1; rear = -1; if (front == MAX - 1) front = 0; else front = front + 1; * Display Circular Queue void display() int front_pos = front, rear_pos = rear; if (front == -1)
cout<<"queue is empty\n"; return; cout<<"queue elements :\n"; if (front_pos <= rear_pos) while (front_pos <= rear_pos) cout<<cqueue_arr[front_pos]<<" "; front_pos++; while (front_pos <= MAX - 1) cout<<cqueue_arr[front_pos]<<" "; front_pos++; front_pos = 0; while (front_pos <= rear_pos) cout<<cqueue_arr[front_pos]<<" "; front_pos++; cout<<endl; ; * Main int main() int choice, item; Circular_Queue cq; Do cout<<"1.insert\n"; cout<<"2.delete\n"; cout<<"3.display\n"; cout<<"4.quit\n"; cout<<"enter your choice : "; cin>>choice; switch(choice)
case 1: cout<<"input the element for insertion in queue : "; cin>>item; cq.insert(item); case 2: cq.del(); case 3: cq.display(); case 4: default: cout<<"wrong choice\n"; End of switch while(choice!=4); return0; Input the element for insertion in queue: 3 Input the element for insertion in queue: 2 Input the element for insertion in queue: 6 Input the element for insertion in queue: 4 Input the element for insertion in queue: 1 Enter your choice: 3 Queue elements: 3 2 6 4 1
Enter your choice: 2 Element deleted from queue is: 3 Enter your choice: 3 Queue elements: 2 6 4 1 Enter your choice: 4 Applications: Adding large integers: Large integers can be added very easily using circular queues. Here the rightmost digit is placed in the front node and leftmost digit is placed in the rear node. Memory management: The unused memory locations in the case of ordinary queues can be utilized in circular queues. Computer controlled traffic system: In computer controlled traffic system, circular queues are used to switch on the traffic lights one by one repeatedly as per the time set.