Overview of Circular Queue

 Let's say another student wants to wait in the queue to get his/her assignment checked. There is space available on the bench, but the student cannot occupy it. 

If the students shift to left, then they can make space the new student. However, this is very time-consuming.
So, logically the person would occupy the vacant seat.
But, will make sure that when the professor calls the early entrants will go first.

Assume a queue of length l. When the queue is empty, the number assigned to the front and rear is -1. In a circular queue, the front index comes right after the last index.

Algorithm for enqueue:

  1. The queue is overflow if front=0 and rear=l-1. End the program.
  2. There are two cases for insertion:
  • There is space from the rear side. If rear<l-1, then insert the element at queue[rear] and increment rear.
  • There is space from front side.If rear=l-1 and front!=0, then set rear=0 and insert element at queue[rear].

Algorithm for dequeue:

  1. The queue is underflowing if rear=front=-1. End the program.
  2. Now there might be three scenarios.
  • If there is only one element in the queue i.e. front=rear. Delete element and set front and rear to -1.
  • Else if there are new elements and old elements have to be deleted.

Delete element and front=0.

  • else delete element and increment front.

Applications of the circular queue are:
  1. It overcomes the space efficiency limitation of the queue.
  2. Used for CPU scheduling and process scheduling.
  3. Used in traffic management.
  4. Used in memory management.





Comments

Popular posts from this blog

Overview of Tree Data Structure

Overview of Three Dimensional Array

Overview of Priority Queue