+ All Categories
Home > Documents > CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics...

CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics...

Date post: 30-Oct-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
15
CS 106X – Programming Abstractions in C++ Dr. Cynthia Bailey Lee
Transcript
Page 1: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

CS 106X – Programming

Abstractions in

C++ Dr. Cynthia Bailey Lee

Page 2: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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

Page 3: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

Stacks

Page 4: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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);

Page 5: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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);

}

}

Page 6: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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

Page 7: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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

Page 8: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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

Page 9: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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

Page 10: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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*++

Page 11: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

Queues

Page 12: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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

Page 13: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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.

Page 14: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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

Page 15: CSE 20 – Discrete Mathematicsstanford.edu/class/archive/cs/cs106x/cs106x.1142/...Today’s Topics 1. Compare Stack and Vector implementations of our file-reversing function 2. Stack

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


Recommended