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)
as 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.
Self Check
- 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?
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.