Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from...
-
Upload
duane-higgins -
Category
Documents
-
view
214 -
download
0
Transcript of Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from...
![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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/1.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/9.jpg)
• 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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/17.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/18.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/20.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/21.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/22.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/23.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/24.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/25.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/26.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/27.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/28.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022070411/56649f535503460f94c77d02/html5/thumbnails/29.jpg)
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