Implementing the queue with a fixed size array

  • We can implement a queue with an array and 2 indexes head and tail:

    where:

    • head = the location of the front (first position) of the queue

    • tail = the location of the open spot at the tail (last position) of the queue

Implementing the queue with a fixed size array

  • The enqueue(x) method will append the element x at the tail of the queue:

    Before enqueue(x):

    After enqueue(x):

Implementing the queue with a fixed size array

  • The dequeue(x) method will remove the element x at the head of the queue:

    Before dequeue(x):

    After dequeue(x):

Problem with the implementation

  • Elements dequeued from the front of the array are unused:

  • We must move the queue content forward to reclaim the usage:

    This may require many copy operations...

There is clever way to re-used the unused array elements -- discussed next