circular queue
Circular queue
It is the type of queue in the data structure. The problem of the memory wastage faced in the normal queue is resolved in the circular.
How does a circular queue work?
In this structure, we enqueue the value until the end of the queue is reached then again start from the front of the structure. The increment of the structure is to work on the module division with the size of the queue.
Photo:
Implementation of the circular queue:
Circular operations:
Two pointer front and rear are used.
Front is used to track the first element and initially, set value with -1.
Rear is used to track the last element and initially, set value with -1.
Enqueue operation:
Firstly,check if the queue is full
If not full then increase the value of the front from -1 to 0.
Also, increase the value of the rear by 1, if the rear already pointed to the end then its next would be the start of the structure.
Dequeue operation
Firstly,check if the queue is empty or not.
Return the value of the front pointer.
Likewise in enqueue,increase the index of the front to 1.
Reset the value of the front and rear pointer.
If the queue is full then additional cases would be added in the code.
Front =0 && Rear == size-1
Front = rear =rear+1
Implementation of the circular queue:
Application:
Memory management:It is used in memory management.
Process Scheduling: A CPU used in scheduling.
Traffic Systems.
Comments
Post a Comment