5 data structures-stack
TRANSCRIPT
![Page 1: 5 data structures-stack](https://reader035.vdocument.in/reader035/viewer/2022071905/55c817a9bb61ebc66b8b47b5/html5/thumbnails/1.jpg)
Stack
• List of items in the order they were added• Only two operations– Push– Pop
• Push adds an item to the front of the stack• Pop removes an item from the front of the
stack• This makes a stack last-in-first-out (LIFO)
![Page 2: 5 data structures-stack](https://reader035.vdocument.in/reader035/viewer/2022071905/55c817a9bb61ebc66b8b47b5/html5/thumbnails/2.jpg)
Stack
• Push
• Pop
12
Next
Head 43
Next
65
Next
23
Next
12
Next
Head 43
Next
65
Next
23
Next
33
Next
12
Next
Head 43
Next
65
Next
23
Next33
Next
![Page 3: 5 data structures-stack](https://reader035.vdocument.in/reader035/viewer/2022071905/55c817a9bb61ebc66b8b47b5/html5/thumbnails/3.jpg)
Stack in Postfix (RPN) Math• The infix expression 5 + ((1 + 2) *4) −3 can be written in RPN as 5 1 2 + 4 * + 3 - • The expression is evaluated left-to-right, with the inputs interpreted as shown in
the following table• When a computation is finished, its result remains as the top (and only) value in
the stack; in this case, 14.
Input Operation Stack Comment5 Push operand 51 Push operand 5, 12 Push operand 5, 1, 2+ Add 5, 3 Pop two values (1, 2) and push result (3)4 Push operand 5, 3, 4* Multiply 5, 12 Pop two values (3, 4) and push result (12)
+ Add 17 Pop two values (5, 12) and push result (17)3 Push operand 17, 3− Subtract 14 Pop two values (17, 3) and push result (14)