the stack and queue types - innovative | inventivehkaiser/spring_2015/csc1254/lectures...the stack...

40
The Stack and Queue Types Lecture 10 Hartmut Kaiser [email protected] http://www.cct.lsu.edu/˜ hkaiser/spring_2015/csc1254.html

Upload: dotruc

Post on 07-Mar-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

The Stack and Queue TypesLecture 10

Hartmut Kaiser

[email protected]

http://www.cct.lsu.edu/˜hkaiser/spring_2015/csc1254.html

Page 2: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Programming Principle of the Day

•Do the simplest thing that could possibly work A good question to ask one’s self when programming is “What is the simplest thing that could possibly work?”

This helps keep us on the path towards simplicity in the design.

http://c2.com/xp/DoTheSimplestThingThatCouldPossiblyWork.html

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

2

Page 3: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Abstract

•This lecture will focus on two other sequential data types, the stack and the queue. We will use stacks to implement conversion between ‘normal expressions’ and the equivalent reverse polish notation.

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

3

Page 4: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Introduction to Stacks

•A stack is a last-in-first-out (LIFO) data structure

•Limited access vector (or list)

•Main operations: Adding an item

Referred to as pushing it onto the stack

Removing an item

Referred to as popping it fromthe stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

4

Page 5: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Introduction to Stacks

•Definition: An ordered collection of data items

Can be accessed at only one end (the top)

•Operations: Construct a stack (usually empty)

Check if it is empty

push: add an element to the top

top: retrieve the top element

pop: remove the top element

size: returns number of elements in stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

5

Page 6: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Introduction to Stacks

•Useful for Reversing a sequence

Managing a series of undo-actions

Tracking history when browsing the web

Function call hierarchy is implemented with a stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

6

Page 7: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Push

•Push means place a new data element at the top of the stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

7

17

5

11

3

Page 8: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Push (cont.)

•Push means place a new data element at the top of the stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

8

17

5

11

3

Page 9: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Push (cont.)

•Push means place a new data element at the top of the stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

9

17

5

11

3

Page 10: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Push (cont.)

•Push means place a new data element at the top of the stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

10

17

5

11

3

Page 11: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Pop

•Pop means take a data element off the top of the stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

11

17

5

11

3

Page 12: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Pop (cont.)

•Pop means take a data element off the top of the stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

12

17

5

11

3

Page 13: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Pop (cont.)

•Pop means take a data element off the top of the stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

13

17

5

11

3

Page 14: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Pop (cont.)

•Pop means take a data element off the top of the stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

14

17

5

11

3

Page 15: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Top

•Top means retrieve the top of the stack without removing it

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

15

17

5

11

3

Page 16: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Top (cont.)

•Top means retrieve the top of the stack without removing it

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

16

17

5

11

3

3

Page 17: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Top (cont.)

•Top means retrieve the top of the stack without removing it

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

17

17

5

11

3

3

Page 18: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Linked-List Stack

•Stacks can also be implemented with a linked list

•The front node is the top of the stack

• In fact, there is std::stack<> which is an adaptor usable with different types of underlying containers (std::listis one possibility)

• std::stack<T>: implements a stack of ‘T’s

T top();void push(T const&);void pop();std::stack<T>::size_type size() const;bool empty();

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

18

Page 19: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Linked-List Stack (cont.)

•To pop, we remove the node at the front of the linked list, and return the element to the client…

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

19

top

Page 20: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Linked-List Stack (cont.)

•To pop, we remove the node at the front of the linked list, and return the element to the client…

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

20

top

Page 21: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Linked-List Stack (cont.)

•To push, we place the new element in a node and insert it at the front of the linked list…

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

21

top

Page 22: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Linked-List Stack (cont.)

•To push, we place a new element in a node and insert it at the front of the linked list…

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

22

top

Page 23: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Example: Implementing a Stack

• Implementing a stack on top of a list is trivial Live coding

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

23

Page 24: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Application of Stacks

Consider the arithmetic statement in the assignment:

x = a * b + c

Compiler must generate machine instructions:

LOAD a

MULT b

ADD c

STORE x

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

24

Note: this is "infix" notation

The operators are between

the operands

Page 25: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Reverse Polish Notation

•Postfix Notation Most compilers convert an expression in infix notation to postfix

The operators are written after the operands

So a * b + c becomes a b * c +

Advantage:

Expressions can be written without parentheses

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

25

Page 26: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Postfix and Prefix Examples

Infix RPN (Postfix) Prefix

A + B A B + + A B

A * B + C A B * C + + * A B C

A * (B + C) A B C + * * A + B C

A – (B – (C – D)) A B C D – – – – A – B – C – D

A – B – C – D A B – C – D – – – – A B C D

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

26

Prefix: Operators come

before the operands

Page 27: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Evaluating RPN Expressions

"By hand" (Underlining technique):

1. Scan the expression from left to right to find an operator.

2. Locate ("underline") the last two preceding operands and combine them using this operator.

3. Repeat until the end of the expression is reached.

Example:

2 3 4 + 5 6 - - *

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

27

Page 28: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Evaluating RPN Expressions

2 3 4 + 5 6 - - *

2 3 4 + 5 6 - - *

2 7 5 6 - - *

2 7 5 6 - - *

2 7 -1 - *

2 7 -1 - *

2 8 *

2 8 *

16

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

282 * ((3 + 4) – (5 – 6))

Page 29: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Evaluating RPN Expressions

•By using a stack algorithm Initialize an empty stack

•Repeat the following until the end of the expression is encountered Get the next token (const, var, operator) in the expression Operand – push onto stackOperator – do the following Pop 2 values from stack Apply operator to the two values Push resulting value back onto stack

•When end of expression encountered, value of expression is the (only) number left in stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

29

Note: if only 1 value on

stack, this is an invalid

RPN expression

Page 30: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Evaluating of Postfix

•Note thechangingstatus of the stack

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

30

Page 31: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Converting between Notations

•By hand: Represent infix expression as an expression tree:

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

31

A * B + C

+

C*

A B

A * (B + C)

*

A+

B C

((A + B) * C) / (D - E)

C

A B

D E

*

+

/

-

Page 32: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Converting RPN (Postfix)

•Traverse the tree in Left-Right-Parent order (post-order) to get RPN:

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

32

C

A B

D E

*

+

/

-

A B + C * D E - /

Page 33: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Converting to Prefix

•Traverse tree in Parent-Left-Right order (pre-order) to get prefix:

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

33

/ * + A B C - D E

C

A B

D E

*

+

/

-

Page 34: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Converting to Infix

•Traverse tree in Left-Parent-Right order (in-order) to get infix (must insert parenthesis)

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

34

((A + B)* C)/(D – E))C

A B

D E

*

+

/

-

Page 35: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Another RPN Conversion Method

By hand: "Fully parenthesize-move-erase" method:1. Fully parenthesize the expression.

2. Replace each right parenthesis by the corresponding operator.

3. Erase all left parentheses.

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

35

A * B + C

((A B * C +

A B * C +

A * (B + C)

(A (B C + *

A B C + *

((A * B) + C) (A * (B + C ) )

Page 36: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Introduction to Queue

•A queue is a first-in-first-out (FIFO) data structure

•Limited access vector (or list)

•Main operations: Adding an item

Referred to as pushing it onto the queue (enqueue an item, adding to the end)

Removing an item

Referred to as popping it from the queue (dequeue an item, removefrom the front)

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

36

Page 37: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Introduction to Queues

•Definition: An ordered collection of data items

Can be read at only one end (the front) and written to only at the other end (the back)

•Operations: Construct a queue (usually empty)

Check if it is empty

push: add an element to the back

front: retrieve the front element

pop: remove the front element

size: returns number of elements in queue

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

37

Page 38: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Introduction to Queues

•Useful for All kind of simulations (traffic, supermarket, etc.)

Computers use queues for scheduling

Handling keyboard events

Handling mouse events

Scrolling the screen

Printer spooler

C++ I/O streams are queues

Even if we only access one end, the other end is managed by the operating system

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

38

Page 39: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Example: Queue of Stringsvoid manage_queue()

{

queue<string> waiting_strings; // queue of 'waiting' strings

while (true) {

cout << "?> "; // ask for next line of text

string response;

getline(cin, response);

if (response.empty()) break;

if (response == "next") { // try to dequeue

if (waiting_strings.empty())

cout << "No one waiting!" << endl;

else {

cout << waiting_strings.front() << endl;

waiting_strings.pop();

}

}

else { // enqueue the line read

waiting_strings.push(response);

}

}

}

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

39

Page 40: The Stack and Queue Types - Innovative | Inventivehkaiser/spring_2015/csc1254/lectures...The Stack and Queue Types ... Postfix and Prefix Examples Infix RPN (Postfix) ... •By using

Example: Implementing a Queue

• Implementing a queue on top of a list is equally trivial Live coding

2/2

6/2

01

5, L

ect

ure

10

CS

C 1

25

4, S

pri

ng 2

01

5, S

tack

s a

nd

Qu

eu

es

40