Unit 4 Stack and Queues
Unit 4 Stack and Queues
Stacks
• What is a Stack?
– A stack is a linear structure in which items can be
inserted and removed only at one end.
• What can we do with a stack?
o push – insert an item into the stack
o pop – Look at the item on top of the stack and
remove it
E top
D top D D
C top C C C
B top B B B B
A top A A A A A
Postponed Decisions
• Stacks are frequently used to indicate the
order of processing of data when certain steps
of processing must be postponed until other
conditions are fulfilled .
C top
B top B B top
top A A A A top
A
Array Representation of Stacks
• Linear array
• One way list
Linked Representation of Stacks
Arithmetic Expressions
• Precedence of operations
Assume that Any parenthesis free operations on the same level are
performed from left to right
Polish Notations
• Polish notation, named after the Polish
mathematician Jan Lukasiewicz, refers to the
notation in which operator symbol is placed
before its two operands e.g.
• +AB
• -CD
• *EF
• /GH
In POSTFIX Notation, An OPERATOR is
placed IMMEDIATELY AFTER its
OPERANDS.
INFIX POSTFIX
a+b ab+
a+b*c abc*+
a*b+c ab*c+
(a + b) * c ab+c*
Building an Arithmetic Expression Evaluator
stack:<empty>
output: []
FPE Infix to Postfix
((A+B)*(C-E))/(F+G))
stack:(
output: []
FPE Infix to Postfix
(A+B)*(C-E))/(F+G))
stack:((
output: []
FPE Infix to Postfix
A+B)*(C-E))/(F+G))
stack:(((
output: []
FPE Infix to Postfix
+B)*(C-E))/(F+G))
stack:(((
output: [A]
FPE Infix to Postfix
B)*(C-E))/(F+G))
stack:(((+
output: [A]
FPE Infix to Postfix
)*(C-E))/(F+G))
stack:(((+
output: [A B]
FPE Infix to Postfix
*(C-E))/(F+G))
stack:((
output: [A B + ]
FPE Infix to Postfix
(C-E))/(F+G))
stack:((*
output: [A B + ]
FPE Infix to Postfix
C-E))/(F+G))
stack:((*(
output: [A B + ]
FPE Infix to Postfix
-E))/(F+G))
stack:((*(
output: [A B + C ]
FPE Infix to Postfix
E))/(F+G))
stack:((*(-
output: [A B + C ]
FPE Infix to Postfix
))/(F+G))
stack:((*(-
output: [A B + C E ]
FPE Infix to Postfix
)/(F+G))
stack:((*
output: [A B + C E - ]
FPE Infix to Postfix
/(F+G))
stack:(
output: [A B + C E - * ]
FPE Infix to Postfix
(F+G))
stack:(/
output: [A B + C E - * ]
FPE Infix to Postfix
F+G))
stack:(/(
output: [A B + C E - * ]
FPE Infix to Postfix
+G))
stack:(/(
output: [A B + C E - * F ]
FPE Infix to Postfix
G))
stack:(/(+
output: [A B + C E - * F ]
FPE Infix to Postfix
))
stack:(/(+
output: [A B + C E - * F G ]
FPE Infix to Postfix
)
stack:(/
output: [A B + C E - * F G + ]
FPE Infix to Postfix
stack:<empty>
output: [A B + C E - * F G + / ]
Evaluation of Postfix Expression
• P : 5, 6, 2, +, *, 12, 4, /, -, ) This is Sentinel at end of P
• STACK : 5
5 6
5 6 2
5 8
40
40 12
40 12 4
40 3
37 Result of Postfix expression
Let's see an example to better understand the algorithm:
Expression: 456*+
Let's see an example to better understand the algorithm:
Expression: 456*+
Examples:
(A + B) * (C + D) *+AB+CD AB+CD+*
3. Each operand simply add to the expression(Postfix) and does not change the state
of the stack.
4. If any higher or equal precedence operator is in the stack, and next we get any
lower preference operator then we pop that higher preference operator and push
lower preference operator onto the stack.
5. In any parenthesis expression if there is left parenthesis in the stack then we only
remove this parenthesis when a right parenthesis of a same level encounter.
6. Exit
Evaluation of Postfix Expression using Stack:
1) A + (B * C - (D / E ^ F) * G) * H
For example:
5! = 5 * 4 * 3 * 2 * 1 = 120
The usual definition of “n factorial” is