3-10 The Static Queue

The Concept of a Queue

We’ve all stood in line at one time or another.  The way a line is supposed to work is that new people enter the line at the end (or “tail”) and people exit the line at the front (or “head”).  People that “bud-in” or jump a line are frowned upon.  You may have heard the word “queue” used in reference to a line of people before.

In computer science, a queue is another data structure that holds items in a sequence.  Unlike the stack however, the elements in a queue are added at the tail (end) and removed from the head (front).  A queue is a First In, First Out (FIFO) data structure, a name reflecting the fact that the first element enqueued (added) to the queue is the first item to be dequeued (removed) from it.  In other words, elements may only be removed from the queue in the same order in which they were placed in the queue.

Queue ADT Interface

As with a stack, a queue is expected to support certain operations:

  1. To add (“enqueue“) an element to the tail (end) of the queue (must not be full).
  1. To remove (“dequeue“) an element at the head (front) of the queue (must not be empty).
  1. To look (“peek“) at the element at the head of the queue, but not remove it.
  1. To check if the queue is empty (“isEmpty“).
  1. To check how many elements are in the queue (“size“).
  1. To clear all elements from the queue (“clear“).

Defining an interface for a queue ADT is clear-cut:

Now it’s your turn!

You Try!

  1. Based on the StaticStack and StaticStackTest classes from last day and using the QueueADT interface code above, implement your own StaticQueue class and StaticQueueTest class to demonstrate all operations.