CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics...
Transcript of CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics...
CS 106X – Programming
Abstractions in
C++ Dr. Cynthia Bailey Lee
Today’s Topics
1. Compare Stack and Vector
implementations of our file-reversing
function
2. Stack example: Reverse Polish Notation
evaluator
3. Queue example: Josephus Survivor
Puzzle
2
Stacks
Stack or Vector?
void mystery(ifstream& infile) {
Stack<string> lines;
while (true) {
string line;
getline(infile, line);
if (infile.fail()) break;
lines.push(line);
}
while (!lines.isEmpty()) {
cout << lines.pop() << endl;
}
}
Vector<string> lines;
lines.insert(lines.size(), line);
cout << lines[lines.size()-1]
<< endl;
lines.remove(lines.size()-1);
Vector version
void mystery(ifstream& infile) {
Vector<string> lines;
while (true) {
string line;
getline(infile, line);
if (infile.fail()) break;
lines.insert(lines.size(), line);
}
while (!lines.isEmpty()) {
cout << lines[lines.size()-1] <<
endl;
lines.remove(lines.size()-1);
}
}
Operator Precedence and
Syntax Trees Ignoring operator precedence rules, how
many distinct results are there to the following arithmetic expression?
3 * 3 + 3 * 3
A. 1
B. 2
C. 3
D. 4
E. More than 4
Reverse Polish Notation
Ambiguities don’t exist in RPN
Also called “postfix” because the operator
goes after the operands
Postfix (RPN):
4 3 * 4 3 * +
Equivalent Infix:
(4*3) + (4*3)
http://commons.wikimedia.org/wiki/File:Hewlett-Packard_48GX_Scientific_Graphing_Calculator.jpg
Reverse Polish Notation
This postfix expression:
4 3 * 7 2 5 * + +
Is equivalent to this infix expression:
A. ((4*3) + (7*2)) + 5
B. (4*3) + ((7+2) + 5)
C. (4*3) + (7 + (2*5))
D. Other/none/more than one
http://commons.wikimedia.org/wiki/File:Hewlett-Packard_48GX_Scientific_Graphing_Calculator.jpg
Stacks and RPN
Evaluate this expression with the help of a stack
Encounter a number: PUSH it
Encounter an operator: POP two numbers and
PUSH result
4 3 * 7 2 5 * + +
4 3
4
12 *
7
12
2
7
12
* ?
?
?
?
?
?
Contents of the stack, reading from top down:
A. 7, 12
B. 2, 7, 12
C. 10, 7,12
D. 10, 5, 2, 7, 12
E. Other
5
2
7
12
Stacks and RPN:
What does that look like in code? Evaluate this expression with the help of a stack
Encounter a number: PUSH it
Encounter an operator: POP two numbers and PUSH result
43*725*++
Queues
Josephus Survivor Puzzle
N people seated in a circle
Every Mth one is killed,
repeatedly around the
circle, until two remain
Question:
Where do you sit?
1 2
3
4
5
6 7
8
9
10
11
12
Queues and Josephus Puzzle 1. Enqueue everybody
2. Dequeue and immediately re-enqueue M-1
people.
3. Dequeue somebody forever. If more than two
alive, repeat steps 2&3
N = 5, M = 2
1
2
3
4
5
Contents of the
queue, reading
from top down:
A. 4, 5, 1
B. 5, 1, 3
C. 5, 1, 3, 4
D. 4, 5, 1, 3
E. Other
3
4
5
1
4
5
1
3
?
?
?
?
1. 2. 3. 2. 3. 2.
2
3
4
5
1
3.
Queues and Josephus Puzzle:
What does that look like in code? 1. Enqueue everybody
2. Dequeue and immediately re-enqueue M-1 people.
3. Dequeue somebody forever If more than two alive, repeat steps 2&3
Stacks vs. Queues
Stack is commonly known as a LIFO data structure
“Last in, first out”
Queue is commonly known as a FIFO data structure
“First in, first out”
Self-study: How many of these operate as LIFO/Stack? Passengers boarding and exiting an elevator
Patients waiting in a hospital Emergency Room
Passengers boarding and exiting an airplane
(A) None (B) 1 (C) 2 (D) All 3