Circular Queue

Circular Queue is special type queue, which follows First in First Out (FIFO) rule and as well as instead of ending queue at the last position, it starts again from the first position after the last position and behaves like circular linear data structure.

We can say it is extension of the queue data structure such that last element of the queue is associated with the first element of the queue and it is also referred as Circular Buffer. If we talk about normal queue, no additional element can be added if queue if full. Also, we can’t add the element in the queue if there is space left in front of queue, so to overcome this problem Circular Queue comes to the picture, so we can use circular queue to solve this problem with efficient manner.

Circular Queue

Operations on Circular Queue

Enqueue Operation in Circular Queue:

  • Firstly, we have to whether the queue is full or not
  • For adding the first element in the queue, set front to 0
  • Increase rear by 1 circularly and add the new element at the position which is pointed by rear

Dequeue Operation in Circular Queue:

  • Firstly, we have to check whether the queue is empty or not.
  • Return the element which is pointed by front.
  • Increase front by 1
  • In case of last element of the queue, set values of front and rear to -1

Implementation of Circular Queue

The number of ways for implementing the circular queue using:

  • Array
  • Linked List

Circular Queue Implementation in C language

Using Array: –

Output

Circular Queue

Using LinkedList: –

Outputs: –

Circular Queue

Circular Queue Implementation in Java language

Output: –

Circular Queue

Time Complexity: –

if we talk about time complexity of enqueue and dequeue operation of a Circular Queue is O(1) for array Implementation.

Advantages of Circular Queue

  • No leaks of memory because it doesn’t use dynamic memory.
  • Simple implementation
  • All operations in constant time O(1)

Applications of Circular Queue

  • Memory Management in Operating System
  • CPU Scheduling
  • Buffering Data Streams
  • Trafficking Signal Systems

source