stacks and queues - ucr computer science and engineering · 2017-10-11 · lists, stacks, and...

42
Stacks and Queues 25

Upload: others

Post on 19-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Stacks and Queues

25

Page 2: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Stack

LIFO: Last-in First-out

26

In/Out

Push Pop

Stack

Undo/Redo

Back/Forward

Function call/return

Page 3: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

In

EnqueueOut

Dequeue

FrontBack

Queue

Queue

FIFO: First-in First-out

27

HPC

Jobs

Page 4: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Queue and Stack ADT

Queue

Enqueue

Dequeue

Front

Size

Empty?

Stack

Push

Pop

Top

Size

Empty?

28

Page 5: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Stack/Queue Implementation

29

ListPush_back

Pop_back

Push_front

Pop_front

Front

Back

Size

Empty

StackPush

Pop

Top

Size

Empty

QueueEnqueue

Dequeue

Front

Size

Empty

Page 6: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Queue Implementation

30

Queue Array Impl. Linked List

Impl.Enqueue O(1) O(1)

Dequeue O(n) O(1)

Front O(1) O(1)

Memory overhead Small Big

Random access O(1) O(n)

Page 7: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Circular Array Queue

31

Front

Back

Page 8: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Circular Array Queue

32

Front

Back

Enqueue

Page 9: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

33

Front

Dequeue

Circular Array Queue

Back

Page 10: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Circular Linked List Queue

34

Back

Front

Page 11: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Queue Implementation

35

Queue Circular Array

Impl.

Circular Linked

List Impl.

Enqueue O(1) O(1)

Dequeue O(1) O(1)

Front O(1) O(1)

Memory overhead Small Big

Page 12: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Standard Template Library (STL)

Lists, stacks, and queues are all implemented

in STL

In a real program, you would better use them;

why?

For the sake of learning, you are not allowed

to use STL during this class unless otherwise

mentioned

36

Page 13: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Stack Applications

Expression evaluation

Human-friendly infix expressions

The operator falls between the two operands3 + 2 × 5 = 13

Easier to read and understand

Can be easily broken into pieces

Machine-friendly postfix expressions

The operator is placed after the two operands325 ×+= 13

Easier to compute in one pass

No need for parentheses37

Page 14: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Evaluate postfix expressions

Infix: 3 × 5 + 4/2 × 2 = 34

Postfix: 35 × 42/+2 ×

38

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

Page 15: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

39

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

Page 16: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

40

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

3Push

Page 17: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

41

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

3

5Push

Page 18: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

42

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

3

5Pop

Page 19: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

43

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

35Pop

Page 20: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

44

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

35 ⨉

Page 21: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

45

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

15Push

Page 22: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

46

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

15

Push

4

Page 23: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

47

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

15

4

Push

2

Page 24: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

48

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

15

4

2

Page 25: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

49

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

15

4 2Pop

Pop/

2Push

Page 26: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

50

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

15 2Pop

Pop+

17Push

Page 27: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

51

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

17

Push

2

Page 28: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

52

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

17 2Pop

Pop⨉

34Push

Page 29: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Postfix Evaluation Example

53

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

operands

34

Page 30: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Infix to Postfix Conversion

Convert the input into a sequence of

operators and operands

Account for operator precedence

Account for parentheses

Example

Infix (input): 3× 5 + 4/2 × 2

Postfix (desired output): 35 × 42/+2 ×

54

Page 31: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

55

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

(

Input

Output

Page 32: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

56

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

(

Input

Output

3

Page 33: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

57

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

(

Input

Output

3

Page 34: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

58

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

(

Input

Output

35

Page 35: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

59

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

(

Input

Output

35⨉

+

Page 36: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

60

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

(

Input

Output

35⨉4

+

Page 37: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

61

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

(

Input

Output

35⨉4

+

/

Page 38: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

62

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

(

Input

Output

35⨉42

+

/

Page 39: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

63

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

Input

Output

35⨉42/+

Page 40: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

64

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

Input

Output

35⨉42/+

Page 41: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

65

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

Input

Output

35⨉42/+2

Page 42: Stacks and Queues - UCR Computer Science and Engineering · 2017-10-11 · Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why?

Example

66

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

operators

Input

Output

35⨉42/+2⨉