10.3.1. FIFO Queues

A first-in-first-out queue (FIFO queue) is an abstract data type. Each queue object holds a sequence of values. The type and operations are as follows.

Type Queue

When an object of type Queue is created it starts out as an empty queue. Items can be added and removed by the operations below.

isEmpty(q)

Return true if queue q is empty, false if not.

insertAtEnd(x, q)

Add x to the end of q.

removeFirst(q)

Remove the first item from q and return it. If q is empty, return −1.

peek(q)

Return the first item in q without modifying q. If q is empty, return −1.

Notice that you are only allowed to add a value at the end and only allowed to remove a value from the front of the queue.

Although a queue can contain any type of item, we will only look at queues whose items are integers for simplicity.