Prerequisite – Queues
Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called ‘Ring Buffer’.
In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we can not insert the next element even if there is a space in front of queue.
Operations on Circular Queue:
- Front: Get the front item from queue.
- Rear: Get the last item from queue.
- enQueue(value) This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at Rear position.
- Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)).
- If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert element.
- deQueue() This function is used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position.
- Check whether queue is Empty means check (front==-1).
- If it is empty then display Queue is empty. If queue is not empty then step 3
- Check if (front==rear) if it is true then set front=rear= -1 else check if (front==size-1), if it is true then set front=0 and return the element.
Elements in Circular Queue are: 14 22 13 -6 Deleted value = 14 Deleted value = 22 Elements in Circular Queue are: 13 -6 Elements in Circular Queue are: 13 -6 9 20 5 Queue is Full
Time Complexity: Time complexity of enQueue(), deQueue() operation is O(1) as there is no loop in any of the operation.
- Memory Management: The unused memory locations in the case of ordinary queues can be utilized in circular queues.
- 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.
- CPU Scheduling: Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur.
This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Queue | Set 1 (Introduction and Array Implementation)
- Difference between Circular Queue and Priority Queue
- Implementation of Deque using circular array
- Array implementation of queue (Simple)
- Stack and Queue in Python using queue Module
- Queue - Linked List Implementation
- Check if a queue can be sorted into another queue using a stack
- Reversing a Queue using another Queue
- Priority Queue | Set 1 (Introduction)
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Majority element in a circular array of 0's and 1's
- Difference between Array, Queue and Stack
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Find the largest multiple of 3 | Set 1 (Using Queue)
- Check if Queue Elements are pairwise consecutive | Set-2
- Maximize sum of consecutive differences in a circular array
- Minimum bit changes in Binary Circular array to reach a index
- Minimum number of colors required to color a Circular Array
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs