3.12. Using a Queue in C++¶
Now we will turn our attention to using the queue data structure with the help of the Standard Template Library (STL) in C++.
As we described in Chapter 1, in C++, as in any object-oriented programming language, the implementation of choice for an abstract data type such as a queue is a class in which the queue operations are implemented as methods. Fortunately, the STL already has a well written implementation of the queue class.
In following queue implementation (ActiveCode 1)
push operations occur, new items are always added on the rear end
of the queue and
pop operations will
manipulate the opposite side, the front end.
- 10, 20
- Remember the first item added to the queue is the first item removed. Remember FIFO.
- 20, 30
- Yes, first in first out means that the 10 is now gone.
- 10, 30
- Queues and stacks are both data structures where you can only access the first or the last items.
- 10, 20, 30
- Oops, maybe you missed the pop call at the end?
Q-3: Suppose you have the following series of queue operations.
queue<int> q; q.push(10); q.push(20); q.push(30); q.pop();
What items are left on the queue?