Queue: Enqueue vs Dequeue (Fill out table, Shift needed?)

782 Views Asked by At

I'm filling out this table and wondering if I did it right:

Table to fill out

We learned it like this: dequeue(Q) -> returns most front (right-hand) element enqueue(Q,k) -> inserts k as new most rear (left-hand) element

I'm kinda unsure because I have to make a shift, when I want to enqueue a new item (all other items shift by one to the front), is that correct?

Thanks for your help!

1

There are 1 best solutions below

0
On

This diagram is correct for one implementation of the queue data structure.

If the row of boxes represents a fixed size array, it's not a very efficient implementation because of the shifting as you've observed. A different implementation called circular queue removes the need to shift items.