Upload
irdginfo
View
73
Download
0
Embed Size (px)
Citation preview
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)
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
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)