In: Computer Science
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (explain briefly in java)
Why a circular queue is more benefiting than a single dimension array queue?
One of the advantages of the circular queue is that we can utilize the spaces in front of the queue. In an single dimension array queue, when the queue turns out to be full, we can't embed the following component regardless of whether there is a space in front the queue. However, utilizing the circular queue, we can utilize the space to store new elements.
How to do indexing in a circular queue?
Front gets the front element in the queue, rear gets the last element in the queue.
enQueue(value) is the capacity is utilized to embed a component into the circular queue. In a circular queue, the new component is constantly embedded at Rear position.
Steps include in enQueue are:
Check whether queue is full or not – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)).
In the event that it is full, at that point show Queue is full. On the off chance that line isn't full, at that point, check if (rear == SIZE – 1 && front != 0) on the off chance that it is valid, at that point set rear=0 and insert element.
deQueue() is used utilized to erase a component from the circular queue. In a circular queue, the component is constantly erased from front position.
Steps include in deQueue are
Check whether line is Empty methods check (front==-1).
In the event that it is unfilled, at that point show Queue is vacant. On the off chance that line isn't unfilled, at that point stage 3
Check in the event that (front==rear) on the off chance that it is valid, at that point set front=rear= - 1 else check on the off chance that (front==size-1), on the off chance that it is valid, at that point set front=0 and return the element.