CS 106X – Programming Abstractions in C++ Dr. Cynthia Bailey Lee

2

Today’s Topics

1. Compare Stack and Vector implementations of our file-reversing function 2. Stack example: evaluator 3. Queue example: Josephus Survivor Puzzle

Stacks Stack or Vector? void mystery(ifstream& infile) { Stack lines; Vector lines; while (true) { string line; getline(infile, line); if (infile.fail()) break; lines.push(line); lines.insert(lines.size(), line); } while (!lines.isEmpty()) { cout << lines.pop() << endl; } cout << lines[lines.size()-1] } << endl; lines.remove(lines.size()-1); Vector version void mystery(ifstream& infile) { Vector 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 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

 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 Contents of the  4 3 * 7 2 5 * + + stack, reading from top down: * * A. 7, 12 4 3 12 7 2 5 ? B. 2, 7, 12 4 12 7 2 ? C. 10, 7,12 12 7 ? D. 10, 5, 2, 7, 12 ? 12 ? E. Other ? 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 1 12 2 circle, until two remain 3  Question: 11

 Where do you sit? 10 4

9 5 8 6 7 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

Contents of the  N = 5, M = 2 queue, reading from top down: 1. 2. 3. 2. 3. 2. 3. A. 4, 5, 1 1 2 3 4 ? B. 5, 1, 3 2 3 4 5 ? C. 5, 1, 3, 4 3 4 5 1 ? D. 4, 5, 1, 3 4 5 1 3 ? E. Other 5 1 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