CS 106X – Programming Abstractions in C++Cynthia Bailey Lee
CS2 in C++ Peer Instruction
Materials by Cynthia Bailey Lee is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Permissions beyond the scope of this license may be available at
http://peerinstruction4cs.org.
2
Today’s Topics1. Queues contrasted with Stacks2. Queues example: Mouse events3. Queue example: Josephus Survivor
Puzzle4. Iterating using range-based for loop
Queues
Stacks vs. Queues Stack is referred to as a LIFO data structure
“Last in, first out” Queue is referred to as a FIFO data structure
“First in, first out”
How many of these operate as FIFO/Queue? Patients waiting in a hospital Emergency Room Passengers boarding and exiting an airplane Passengers boarding and exiting an elevator (assume all
board on ground floor and exit at the same destination) People waiting in line for an amusement park ride
(A) None (B) 1 (C) 2 (D) All 3
LIFO:
FIFO:
Image is modified by Cynthia Lee based on original from Wikimedia Commons “A seatmap over the Airbus A380” Source=self-made |Date=24 September 2007 |Author= S. Solberg J. Used under Creative Commons License
Queue ApplicationsMouse events
Event queues While your code executes, a separate program is
constantly listening for events and recording them Mouse moved, mouse clicked, mouse dragged,
keyboard key pressed, etc Every once in a while, your code can call
getNextEvent() to see what has happened getNextEvent returns the events one at a time in the
same order they happened In other words, returns them in FIFO order! When it is “recording” events, it is enqueuing
events in an event QUEUE Very common use of the Queue ADT
Queue ApplicationsJosephus Survivor Puzzle
Josephus Survivor Puzzle N people seated in a circle Skip M living people and kill
the (M+1)th, repeatedly around the circle, until two remain
Question:Where do you sit?
12
3
4
56
789
10
11
12
Josephus Survivor Puzzle Question: (you will need paper & pencil)
N = 12, M = 3Where do you sit?A. 1 or 2B. 1 or 6C. 5 or 10D. 5 or 6E. Other/none/more
12
3
4
56
789
10
11
12
Queues and Josephus Puzzle1. Enqueue everybody2. Dequeue and immediately re-enqueue M people. 3. Dequeue somebody forever. If more than two
alive, repeat steps 2&3
N = 5, M = 2
1. 2. 3. 2. 3. 2. 3.
?
Queues and Josephus Puzzle1. Enqueue everybody2. Dequeue and immediately re-enqueue M people. 3. Dequeue somebody forever. 4. If more than two alive, repeat steps 2&3
N = 5, M = 2
12345
Contents of the queue, reading from top down:A. 1, 2, 4B. 2, 4, 5C. 2, 4, 5, 1D. 4, 5, 1, 2E. Other
4512
1245
?
1. 2. 3. 2. 3. 2.34512
3.
Queues and Josephus Puzzle:What does that look like in code?
1. Enqueue everybody2. Dequeue and immediately re-enqueue M people. 3. Dequeue somebody forever 4. If more than two alive, repeat steps 2&3
Iterating over a collection
C++11 has a nice for loop just for collections ADTs Range-based for loop gives you each item in a
collection, one at a time For ADTs with a clear linear order (Vector,
Stack, Queue), they are given to you in that order
[Spoiler for Wednesday!] Some ADTs are not linear. In theory, we often consider the ordering of iteration on these to be “undefined,” but in practice some implementations allow you to rely on a certain order
C++11 range-based for loop One way to iterate over a Vector:for (int i=0; i<classlist.size(); i++){
cout << classlist[i] << endl;}
Range-based for loop makes the code cleaner:for (string name : classlist){
cout << name << endl;}
Other iteration techniques in C++ (NOT recommended for this class) STL vector and iteratorsfor (vector<int>::iterator it = classlist.begin(); it != classlist.end(); ++it){cout << *it << endl;}
Stanford library “foreach” loop (obsoleted by range-based for loop in C++11):foreach (string name in classlist){cout << name << endl;}