29
Basic Data Structures Stacks

Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Embed Size (px)

Citation preview

Page 1: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Basic Data Structures

Stacks

Page 2: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Stacks

• A collection of objects

• Objects can be inserted into or removed from the collection at one end (top)

• First-in-last-out

Page 3: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Stack Applications: Reversing a Word

•RAIL LIAR

Page 4: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Empty

Stack

• Start from an empty stack

• Insert the word RAIL into the stack

Page 5: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

R A I L

R

Page 6: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

R A I LAR

Page 7: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

R A I L IAR

Page 8: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

R A I LL

IAR

Page 9: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

• The Word is now in the stack.

• Now empty the Stack .

LIAR

Page 10: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

L L

IAR

Page 11: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

L IIAR

Page 12: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

L I A

AR

Page 13: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

L I A R

R

Page 14: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Stack Operations

• push: push a new item on the top of the stack

• pop: if the stack is not empty, remove the top item of the stack; not defined if the stack is empty

• top: if the stack is not empty, read the value of the top item of the stack; not defined if the stack is empty

Page 15: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Practice

• What’s remained in the stack after the following operations, assuming that at the beginning the stack is empty?

push(3);

push(5);

pop();

top();

push(9);

Page 16: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate Postfix Expressions

• Operator comes after the operandsPostfix Infix

5 4 + 5 + 4

5 4 + 3 * (5 + 4) * 3

5 4 + 9 6 - * (5 + 4) * (9 - 6)

Page 17: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate Postfix: Keep Intermediate Results

• E.g. the result of (5 + 4) while we calculate (9 - 6)

• stacks do this!

• Assumptions:– binary operators only– we can split the expression string into tokens

(pieces)

Page 18: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate Postfix: the Algorithm

• while there are more tokens in the input string:– if next token is a number, push it onto the stack

– if next token is an operand•pop two elements off the stack•perform the operation•stack the result

• pop final element as answer

Page 19: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *

5 is a number, place it on the stack

5

Page 20: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *

4 is a number, place it on the stack

45

Page 21: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *

+ is an operand

•Pop two numbers

•Apply Operand

45

45 + = 9

Page 22: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *Put the result back onto the stack

95 4+ = 9

Page 23: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *

9 is a number, place it on the stack

99

Page 24: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *

6 is a number, place it on the stack

69

9

Page 25: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *

- is an operand

• Pop two numbers

• Apply Operand

9

69

69 - = 3

Page 26: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *- is an operand

Put the result back onto the stack

969 - = 3

3

Page 27: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *

93

* is an operand

Pop two numbers

Apply Operand

39 * = 27

Page 28: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *

27

* is an operand

Pop two numbers

Apply Operand

39 * = 27

Page 29: Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out

Evaluate

5 4 + 9 6 - *No more tokens

in the string

• Pop the final answer

27

5 4 + 9 6 - *= (5 + 4) * (9 - 6)= 27