Data Structures

Circular Queue in Data Structure

Circular Queue is a linear data structure that follows the First-In-First-Out (FIFO) order like the Linear Queue. Additionally, the last position is connected back to the first position to form the circle. This solves the problem of the Linear Queue to use the memory space effectively.

Representation

Circular Queue Representation
Circular Queue Representation
  • Front: Front side of the Queue
  • Rear: Rear side of the Queue
  • Element: The actual data (represented as 10, 20, 30, 40, 50, and 60)
  • Circular: The first and last position is connected to form the circle.

Circular Queue Operations and Time Complexity

OperationDescriptionTime
Complexity
Space
Complexity
EnqueueInsert a new element on the RearO(1)O(n)
DequeueDelete the element from the FrontO(1)O(n)
PeekReturns the front element without removingO(1)O(n)
IsFullCheck if the Queue is fullO(1)O(n)
IsEmptyCheck if the Queue is emptyO(1)O(n)
SearchSearch the element by valueO(n)O(n)
Time and Space complexity of Circular Queue operations

Implementations of Circular Queue

Circular Queues can be implemented using Array or Linked List. However, we must choose the correct implementation based on the requirements.

Advantages and Disadvantages

Advantages

  • Memory space is effectively used in Array implementation.
  • The linked list implementation can traverse the whole list starting from any node.

Disadvantages

  • We must keep track of the last position and switch the Front/Rear pointers to the first position.

Applications of Circular Queue

When to use circular queues?

Prefer circular queues when you choose to implement the Queue using an array. Because, in linear form, the array memory is not effectively utilized. The elements are dequeued from the Front side, but the empty spaces are unused.

In Circular Queues, the empty spaces at the front are utilized since the Rear can circle to the beginning. The new elements are inserted at the beginning of the Array.

Realtime Applications

  • Traffic system to switch on the traffic lights in a circular fashion based on the time slot.
  • Operating systems often maintain the Queue of processes for CPU scheduling.
Copyright copyright 2024 Simplerize