Stack ADT Tiziana Ligorio 1 Todays Plan Questons? Stack ADT 2 - - PowerPoint PPT Presentation

stack adt
SMART_READER_LITE
LIVE PREVIEW

Stack ADT Tiziana Ligorio 1 Todays Plan Questons? Stack ADT 2 - - PowerPoint PPT Presentation

Stack ADT Tiziana Ligorio 1 Todays Plan Questons? Stack ADT 2 Abstract Data Types Bag List Stack 3 Stack 34 A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack


slide-1
SLIDE 1

Stack ADT

Tiziana Ligorio

1

slide-2
SLIDE 2

Today’s Plan

Questons? Stack ADT

2

slide-3
SLIDE 3

Abstract Data Types

Bag List Stack

3

slide-4
SLIDE 4

Stack

A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack

4

34

slide-5
SLIDE 5

Stack

A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack

5

34

slide-6
SLIDE 6

Stack

A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack

6

127 34

slide-7
SLIDE 7

Stack

A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack

7

34 127

slide-8
SLIDE 8

Stack

A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack

8

13 34 127

slide-9
SLIDE 9

Stack

A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack

9

34 127 13

slide-10
SLIDE 10

Stack

A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack

10

13 34 127

slide-11
SLIDE 11

Stack

A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack LIFO: Last In First Out Only top of stack is accessible (top), no other

  • bjects on the stack are visible

11

34 127

slide-12
SLIDE 12

Applications

Very simple structure Many applications:
 program stack
 balancing parenthesis
 evaluating postfix expressions
 backtracking
 . . . and more

12

slide-13
SLIDE 13

Program Stack

13

slide-14
SLIDE 14

Program Stack

1 void f(int x, int y)
 2 {
 3 int a;
 4 // stuff here
 5 if(a<13)
 6 a = g(a);
 7 // stuff here
 8 } 9 int g(int z)
 10 {
 11 int p ,q;
 12 // stuff here
 13 return q;
 14 }

14

slide-15
SLIDE 15

Program Stack

1 void f(int x, int y)
 2 {
 3 int a;
 4 // stuff here
 5 if(a<13)
 6 a = g(a);
 7 // stuff here
 8 } 9 int g(int z)
 10 {
 11 int p ,q;
 12 // stuff here
 13 return q;
 14 }

Stack Frame for f() parameters return address local variables

a x y

15

Address of instruction after call to f()

. . .

slide-16
SLIDE 16

Program Stack

16

Stack Frame for f() Stack Frame for g() parameters parameters return address return address local variables local variables

a p q x y z

Address of instruction after call to f()

Address of instruction on line 7

. . . . . .

1 void f(int x, int y)
 2 {
 3 int a;
 4 // stuff here
 5 if(a<13)
 6 a = g(a);
 7 // stuff here
 8 } 9 int g(int z)
 10 {
 11 int p ,q;
 12 // stuff here
 13 return q;
 14 }

slide-17
SLIDE 17

Program Stack

17

Stack Frame for f() parameters return address local variables

a x y

Address of instruction after call to f()

. . .

1 void f(int x, int y)
 2 {
 3 int a;
 4 // stuff here
 5 if(a<13)
 6 a = g(a);
 7 // stuff here
 8 } 9 int g(int z)
 10 {
 11 int p ,q;
 12 // stuff here
 13 return q;
 14 }

slide-18
SLIDE 18

Balancing Parentheses

18

slide-19
SLIDE 19

Balancing Parentheses

19

int f(){if(x*(y+z[i])<47){x += y}}

slide-20
SLIDE 20

Balancing Parentheses

20

int f(){if(x*(y+z[i])<47){x += y}}

slide-21
SLIDE 21

Balancing Parentheses

21

int f(){if(x*(y+z[i])<47){x += y}}

slide-22
SLIDE 22

Balancing Parentheses

22

int f(){if(x*(y+z[i])<47){x += y}}

slide-23
SLIDE 23

Balancing Parentheses

23

int f(){if(x*(y+z[i])<47){x += y}}

slide-24
SLIDE 24

Balancing Parentheses

24

int f(){if(x*(y+z[i])<47){x += y}}

push

(

slide-25
SLIDE 25

Balancing Parentheses

25

int f(){if(x*(y+z[i])<47){x += y}}

pop

slide-26
SLIDE 26

Balancing Parentheses

26

int f(){if(x*(y+z[i])<47){x += y}}

push

{

slide-27
SLIDE 27

Balancing Parentheses

27

int f(){if(x*(y+z[i])<47){x += y}} {

slide-28
SLIDE 28

Balancing Parentheses

28

int f(){if(x*(y+z[i])<47){x += y}} {

slide-29
SLIDE 29

{

Balancing Parentheses

29

int f(){if(x*(y+z[i])<47){x += y}} (

push

slide-30
SLIDE 30

Balancing Parentheses

30

int f(){if(x*(y+z[i])<47){x += y}} { (

slide-31
SLIDE 31

Balancing Parentheses

31

int f(){if(x*(y+z[i])<47){x += y}} { (

slide-32
SLIDE 32

{ (

Balancing Parentheses

32

int f(){if(x*(y+z[i])<47){x += y}} (

push

slide-33
SLIDE 33

Balancing Parentheses

33

int f(){if(x*(y+z[i])<47){x += y}} { ( (

slide-34
SLIDE 34

Balancing Parentheses

34

int f(){if(x*(y+z[i])<47){x += y}} { ( (

slide-35
SLIDE 35

Balancing Parentheses

35

int f(){if(x*(y+z[i])<47){x += y}} { ( (

slide-36
SLIDE 36

{ ( (

Balancing Parentheses

36

int f(){if(x*(y+z[i])<47){x += y}} [

push

slide-37
SLIDE 37

Balancing Parentheses

37

int f(){if(x*(y+z[i])<47){x += y}} { ( ( [

slide-38
SLIDE 38

Balancing Parentheses

38

int f(){if(x*(y+z[i])<47){x += y}}

pop

{ ( (

slide-39
SLIDE 39

Balancing Parentheses

39

int f(){if(x*(y+z[i])<47){x += y}}

pop

{ (

slide-40
SLIDE 40

Balancing Parentheses

40

int f(){if(x*(y+z[i])<47){x += y}} { (

slide-41
SLIDE 41

Balancing Parentheses

41

int f(){if(x*(y+z[i])<47){x += y}} { (

slide-42
SLIDE 42

Balancing Parentheses

42

int f(){if(x*(y+z[i])<47){x += y}} { (

slide-43
SLIDE 43

Balancing Parentheses

43

int f(){if(x*(y+z[i])<47){x += y}}

pop

{

slide-44
SLIDE 44

Balancing Parentheses

44

int f(){if(x*(y+z[i])<47){x += y}}

push

{ {

slide-45
SLIDE 45

Balancing Parentheses

45

int f(){if(x*(y+z[i])<47){x += y}} { {

slide-46
SLIDE 46

Balancing Parentheses

46

int f(){if(x*(y+z[i])<47){x += y}} { {

slide-47
SLIDE 47

Balancing Parentheses

47

int f(){if(x*(y+z[i])<47){x += y}} { {

slide-48
SLIDE 48

Balancing Parentheses

48

int f(){if(x*(y+z[i])<47){x += y}} { {

slide-49
SLIDE 49

Balancing Parentheses

49

int f(){if(x*(y+z[i])<47){x += y}} { {

slide-50
SLIDE 50

Balancing Parentheses

50

int f(){if(x*(y+z[i])<47){x += y}} { {

slide-51
SLIDE 51

Balancing Parentheses

51

int f(){if(x*(y+z[i])<47){x += y}}

pop

{

slide-52
SLIDE 52

Balancing Parentheses

52

int f(){if(x*(y+z[i])<47){x += y}}

Finished reading Stack is empty Parentheses are balanced

pop

slide-53
SLIDE 53

Balancing Parentheses

53

int f(){if(x*(y+z[i])<47){x += y}

Finished reading Stack not empty Parentheses NOT balanced

{

slide-54
SLIDE 54

Balancing Parentheses

54

for(char ch : st)
 { if ch is an open parenthesis character
 push it on the stack else if ch is a close parenthesis character
 if it matches the top of the stack
 pop the stack 
 else
 return unbalanced
 // else it is not a parenthesis } if stack is empty
 return balanced else return unbalanced


slide-55
SLIDE 55

Evaluating Postfix Expressions

55

slide-56
SLIDE 56

Postfix Expressions

Operator applies to the two operands immediately preceding it

56

Infix: 2 * (3 + 4) 2 * 3 + 4 Postfix: 2 3 4 + * 2 3 * 4 +

slide-57
SLIDE 57

Evaluating Postfix Expressions

Operator applies to the two operands immediately preceding it Assumptions / simplifications:


  • String is syntactically correct postfix expression

  • No unary operators

  • No exponentiation operation

  • Operands in string are single integer values

57

Postfix: 2 3 4 + *

slide-58
SLIDE 58

Evaluating Postfix Expressions

58

Postfix: 2 3 4 + *

2

slide-59
SLIDE 59

Evaluating Postfix Expressions

59

Postfix: 2 3 4 + *

2 3

slide-60
SLIDE 60

Evaluating Postfix Expressions

60

Postfix: 2 3 4 + *

2 3 4

slide-61
SLIDE 61

Evaluating Postfix Expressions

61

Postfix: 2 3 4 + *

2 3 4

slide-62
SLIDE 62

Evaluating Postfix Expressions

62

Postfix: 2 3 4 + *

4

+

2 3

slide-63
SLIDE 63

Evaluating Postfix Expressions

63

Postfix: 2 3 4 + *

3 4

+

2

slide-64
SLIDE 64

Evaluating Postfix Expressions

64

Postfix: 2 3 4 + *

3 4

+ = 7

2

slide-65
SLIDE 65

2

Evaluating Postfix Expressions

65

Postfix: 2 3 4 + *

7

slide-66
SLIDE 66

Evaluating Postfix Expressions

66

Postfix: 2 3 4 + *

2 7

slide-67
SLIDE 67

Evaluating Postfix Expressions

67

Postfix: 2 3 4 + *

7 2

slide-68
SLIDE 68

Evaluating Postfix Expressions

68

Postfix: 2 3 4 + *

2 7

*

slide-69
SLIDE 69

Evaluating Postfix Expressions

69

Postfix: 2 3 4 + *

2 7

* = 14

slide-70
SLIDE 70

Evaluating Postfix Expressions

70

Postfix: 2 3 4 + *

14

Done reading string The top of the stack is the result

slide-71
SLIDE 71

Evaluating Postfix Expressions

Operator applies to the two operands immediately preceding it Assumptions / simplifications:


  • string is syntactically correct postfix expression

  • No unary operators

  • No exponentiation operation

  • Operands in string are single integer values

71

Postfix: 2 3 * 4 +

slide-72
SLIDE 72

Evaluating Postfix Expressions

72

Postfix: 2 3 * 4 +

2

slide-73
SLIDE 73

Evaluating Postfix Expressions

73

Postfix: 2 3 * 4 +

2 3

slide-74
SLIDE 74

Evaluating Postfix Expressions

74

Postfix: 2 3 * 4 +

3

*

2

slide-75
SLIDE 75

Evaluating Postfix Expressions

75

3

Postfix: 2 3 * 4 +

2

* = 6

slide-76
SLIDE 76

Evaluating Postfix Expressions

76

Postfix: 2 3 * 4 +

6

slide-77
SLIDE 77

4

Evaluating Postfix Expressions

77

Postfix: 2 3 * 4 +

6

slide-78
SLIDE 78

Evaluating Postfix Expressions

78

Postfix: 2 3 * 4 +

4

+

6

slide-79
SLIDE 79

Evaluating Postfix Expressions

79

Postfix: 2 3 * 4 +

6 4 10

+ =

slide-80
SLIDE 80

Evaluating Postfix Expressions

80

Postfix: 2 3 * 4 +

10

Done reading string The top of the stack is the result

slide-81
SLIDE 81

Evaluating Postfix Expressions

for(char ch : st)
 { if ch is an operand 
 push it on the stack else // ch is an operator op
 {
 //evaluate and push the result

  • perand2 = pop stack

  • perand1 = pop stack


result = operand1 op operand2
 push result on stack }
 }

81

slide-82
SLIDE 82

Lecture Activity

Describe an algorithm that translates the infix expression below into postfix (you can use drawings to explain): Hint: use 2 stacks, one for operators and parentheses another one for the operands and postfix expression. Once converted use the empty stack to invert the order

82

Infix: 2 * (3 + 4) Postfix: 2 3 4 + *

slide-83
SLIDE 83

83

2 3 4 (

+

Infix: 2 * (3 + 4) Postfix: 2 3 4 + *

2 3 4 *

+

1. Read characters onto corresponding stack until ‘)’

  • 2. Pop operator stack and push

it onto postfix stack ignoring ‘(‘

Postfix Stack Operator Stack Postfix Stack Operator Stack

*

Postfix Stack Operator Stack

  • 3. Push everything onto empty

stack to invert Then read pop and print.

2 3 4 *

+

slide-84
SLIDE 84

84

2 3 (

Infix: (2 * 3 )+ 4 Postfix: 2 3 *4 +

2 3 4 *

+

1. Read characters

  • nto corr. stack until ‘)’
  • r end of string
  • 2. If reading a ‘)’, move operators

to Postfix Stack until a ‘(‘ discard it and continue reading string

Postfix Stack Operator Stack Postfix Stack Operator Stack

* 2 3 *

Postfix Stack Operator Stack

  • 3. Keep reading

until ‘)’ -> 2.

  • r end of string -> 4.
  • 4. Move operators to

Postfix Stack

2 3 4 *

+

Postfix Stack Operator Stack

slide-85
SLIDE 85

Infix: (2 * 3 )+ 4 Postfix: 2 3 *4 +

2 3 4 *

+

  • 4. Move operators to

Postfix Stack

Postfix Stack Operator Stack Postfix Stack Operator Stack

  • 5. Pop and push onto empty

stack to invert, then print

2 3 4 *

+

slide-86
SLIDE 86

Search a Flight Map

Fly from Origin to Destination following map

  • 1. Reach destination
  • 2. Reach city with no departing flights (dead end)
  • 3. Go in circles forever

86

slide-87
SLIDE 87

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

87

P Origin = P , Destination = Z C = visited C = backtracked

slide-88
SLIDE 88

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

88

P Origin = P , Destination = Z R C = visited C = backtracked

slide-89
SLIDE 89

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

89

P Origin = P , Destination = Z R X C = visited C = backtracked

slide-90
SLIDE 90

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

90

P Origin = P , Destination = Z R X C = visited C = backtracked

slide-91
SLIDE 91

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

91

P Origin = P , Destination = Z R X C = visited C = backtracked

slide-92
SLIDE 92

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

92

P Origin = P , Destination = Z R X C = visited C = backtracked

slide-93
SLIDE 93

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

93

P Origin = P , Destination = Z R X W C = visited C = backtracked

slide-94
SLIDE 94

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

94

P Origin = P , Destination = Z R X W S C = visited C = backtracked

slide-95
SLIDE 95

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

95

P Origin = P , Destination = Z R X W S T C = visited C = backtracked

slide-96
SLIDE 96

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

96

Origin = P , Destination = Z R X W S T P C = visited C = backtracked

slide-97
SLIDE 97

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

97

Origin = P , Destination = Z R X W S T P C = visited C = backtracked

slide-98
SLIDE 98

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

98

Origin = P , Destination = Z R X W S T P C = visited C = backtracked

slide-99
SLIDE 99

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

99

Origin = P , Destination = Z R X W S T Y P C = visited C = backtracked

slide-100
SLIDE 100

Backtracking

Avoid dead end by backtracking Avoid traveling in circles by marking visited cities

100

Origin = P , Destination = Z R X W S T Y Z P C = visited C = backtracked

slide-101
SLIDE 101

Backtracking

101

Origin = P , Destination = Z

P

slide-102
SLIDE 102

P

Backtracking

102

Origin = P , Destination = Z

R

slide-103
SLIDE 103

P

Backtracking

103

Origin = P , Destination = Z

R X

slide-104
SLIDE 104

P

Backtracking

104

Origin = P , Destination = Z

R

slide-105
SLIDE 105

Backtracking

105

Origin = P , Destination = Z

P

slide-106
SLIDE 106

P W

Backtracking

106

Origin = P , Destination = Z

slide-107
SLIDE 107

P W

Backtracking

107

Origin = P , Destination = Z

S

slide-108
SLIDE 108

P W

Backtracking

108

Origin = P , Destination = Z

S T

slide-109
SLIDE 109

Backtracking

109

Origin = P , Destination = Z

P W S

slide-110
SLIDE 110

Backtracking

110

Origin = P , Destination = Z

P W

slide-111
SLIDE 111

P W

Backtracking

111

Origin = P , Destination = Z

Y

slide-112
SLIDE 112

P W Y

Backtracking

112

Origin = P , Destination = Z

Z

slide-113
SLIDE 113

Backtracking

while(not found flights from origin to destination)
 { if no flight exists from city on top of stack to unvisited destination
 pop the stack //BACKTRACK else 
 {
 select an unvisited city C accessible from city currently at top of stack 
 push C on stack
 mark C as visited } }

113

slide-114
SLIDE 114

Program Stack and Recursion

Recursion works because function waiting for result/ return from recursive call are on program stack Order of execution determined by stack

114

slide-115
SLIDE 115

More Applications

Balancing anything!


  • html tags (e.g <p> matches </p>

Reverse characters in a word or words in a sentence Undo mechanism for editors or backups Traversals (graphs / trees) . . .

115

slide-116
SLIDE 116

Stack ADT

116

#ifndef STACK_H_
 #define STACK_H_
 
 template<<typename ItemType>
 class Stack 
 {
 
 public:
 Stack();
 void push(const ItemType& new_entry); //adds an element to top of stack
 void pop(); // removes element from top of stack
 ItemType top() const; // returns a copy of element at top of stack
 int size() const; // returns the number of elements in the stack
 bool isEmpty() const;//returns true if no elements on stack, else false 
 
 private:
 //implementation details here }; //end Stack
 
 #include "Stack.cpp"
 #endif // STACK_H_`