Stacks and Queues 25 Stack In/Out LIFO: Last-in First-out Push - - PowerPoint PPT Presentation

stacks and queues
SMART_READER_LITE
LIVE PREVIEW

Stacks and Queues 25 Stack In/Out LIFO: Last-in First-out Push - - PowerPoint PPT Presentation

Stacks and Queues 25 Stack In/Out LIFO: Last-in First-out Push Pop Undo/Redo Back/Forward Function call/return Stack 26 Queue HPC FIFO: First-in First-out Back Front In Out Jobs Enqueue Dequeue Queue 27 Queue and Stack ADT


slide-1
SLIDE 1

Stacks and Queues

25

slide-2
SLIDE 2

Stack

LIFO: Last-in First-out

26

In/Out Push Pop

Stack

Undo/Redo Back/Forward

Function call/return

slide-3
SLIDE 3

In Enqueue Out Dequeue

Front Back

Queue

Queue

FIFO: First-in First-out

27

HPC

Jobs

slide-4
SLIDE 4

Queue and Stack ADT

Queue

Enqueue Dequeue Front Size Empty?

Stack

Push Pop Top Size Empty?

28

slide-5
SLIDE 5

Stack/Queue Implementation

29

List

Push_back Pop_back Push_front Pop_front Front Back Size Empty

Stack

Push Pop Top Size Empty

Queue

Enqueue Dequeue Front Size Empty

slide-6
SLIDE 6

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)

slide-7
SLIDE 7

Circular Array Queue

31

Front Back

slide-8
SLIDE 8

Circular Array Queue

32

Front Back

Enqueue

slide-9
SLIDE 9

33

Front

Dequeue

Circular Array Queue

Back

slide-10
SLIDE 10

Circular Linked List Queue

34

Back Front

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

Stack Applications

Expression evaluation Human-friendly infix expressions

The operator falls between the two operands 3 + 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 operands 325 ×+= 13 Easier to compute in one pass No need for parentheses

37

slide-14
SLIDE 14

Evaluate postfix expressions

Infix: 3 × 5 + 4/2 × 2 = 34 Postfix: 35 × 42/+2 ×

38

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands
slide-15
SLIDE 15

Postfix Evaluation Example

39

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands
slide-16
SLIDE 16

Postfix Evaluation Example

40

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

3

Push

slide-17
SLIDE 17

Postfix Evaluation Example

41

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

3 5

Push

slide-18
SLIDE 18

Postfix Evaluation Example

42

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

3 5

Pop

slide-19
SLIDE 19

Postfix Evaluation Example

43

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

3 5

Pop

slide-20
SLIDE 20

Postfix Evaluation Example

44

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

3 5 ⨉

slide-21
SLIDE 21

Postfix Evaluation Example

45

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

15

Push

slide-22
SLIDE 22

Postfix Evaluation Example

46

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

15

Push

4

slide-23
SLIDE 23

Postfix Evaluation Example

47

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

15 4

Push

2

slide-24
SLIDE 24

Postfix Evaluation Example

48

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

15 4 2

slide-25
SLIDE 25

Postfix Evaluation Example

49

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

15 4 2

Pop Pop

/ 2

Push

slide-26
SLIDE 26

Postfix Evaluation Example

50

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

15 2

Pop Pop

+ 17

Push

slide-27
SLIDE 27

Postfix Evaluation Example

51

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

17

Push

2

slide-28
SLIDE 28

Postfix Evaluation Example

52

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

17 2

Pop Pop

⨉ 34

Push

slide-29
SLIDE 29

Postfix Evaluation Example

53

3 5 ⨉ 4 2 / + 2 ⨉

Stack of

  • perands

34

slide-30
SLIDE 30

Infix to Postfix Conversion

Convert the input into a sequence of

  • perators and operands

Account for operator precedence Account for parentheses Example

Infix (input): 3 × 5 + 4/2 × 2 Postfix (desired output): 35 × 42/+2 ×

54

slide-31
SLIDE 31

Example

55

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

(

Input Output

slide-32
SLIDE 32

Example

56

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

(

Input Output

3

slide-33
SLIDE 33

Example

57

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

(

Input Output

3

slide-34
SLIDE 34

Example

58

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

(

Input Output

35

slide-35
SLIDE 35

Example

59

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

(

Input Output

35⨉

+

slide-36
SLIDE 36

Example

60

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

(

Input Output

35⨉4

+

slide-37
SLIDE 37

Example

61

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

(

Input Output

35⨉4

+ /

slide-38
SLIDE 38

Example

62

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

(

Input Output

35⨉42

+ /

slide-39
SLIDE 39

Example

63

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

Input Output

35⨉42/+

slide-40
SLIDE 40

Example

64

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

Input Output

35⨉42/+

slide-41
SLIDE 41

Example

65

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

Input Output

35⨉42/+2

slide-42
SLIDE 42

Example

66

( 3 ⨉ 5 + 4 / 2 ) ⨉ 2

Stack of

  • perators

Input Output

35⨉42/+2⨉