3.11. The Queue Abstract Data Type

The queue abstract data type is defined by the following structure and operations. A queue is structured, as described above, as an ordered collection of items which are added at one end, called the “rear,” and removed from the other end, called the “front.” Queues maintain a first-in-first-out (FIFO) ordering property. The standard queue operations are given below.

As an example, if we assume that q is a queue that has been created and is currently empty, then Table 1 shows the results of a sequence of queue operations. The queue contents are shown such that the front is on the right. 4 was the first item pushed so it is the first item returned by dequeue.

Table 1: Example Queue Operations
Queue Operation Queue Contents Return Value
q.empty() [] true
q.push(4) [4]  
q.push(12) [12,4]  
q.push(3) [3,12,4]  
q.size() [3,12,4] 3
q.empty() [3,12,4] false
q.push(97) [97,3,12,4]  
q.pop() [97,3,12]  
q.pop() [97,3]  
q.size() [97,3] 2
Next Section - 3.12. Using a Queue in C++