Stack PPT2
Stack PPT2
Stack
• Linear Data Structure
• Access is allowed only at one point of the structure,
normally termed the top of the stack
– access to the most recently added item only
Stack
• Elements are added to and removed from the top of the
stack (the most recently added items are at the top of
the stack).
• Described as a
"Last In First Out" (LIFO) data structure.
OR First In Last Out data structure.
• Operations are limited:
– push (add item to stack)
– pop (remove top item from stack)
3
Last In First Out
E top
D top D D top
C top C C C
B top B B B B
A A A A A
A top
Operation - PUSH
Operation - POP
Applications of Stack
• Recursion
• Parsing
• Infix to Postfix/Prefix Conversion
• Prefix/Postfix Evaluation
• Undo and Redo Operation in Editors
• Matching Paranthesis
• Reverse word/string
Uses of Stacks
• The runtime stack used by a
process (running program) to
keep track of methods in
progress
• Search problems
• Undo, redo, back, forward
Stacks 8
Recursion
Recursion
CIS 068
Recursion
• Sometimes, the best way to solve a
problem is by solving a smaller version of
the exact same problem first
CIS 068
Recursion
What’s behind this function ?
It computes f! (factorial)
CIS 068
Tracing the example
public int f(int a){
if (a==1)
RECURSION !
return(1);
else
return(a * f( a-1));
}
CIS 068
Watching the Stack
public int factorial(int a){
if (a==1)
return(1);
else
return(a * factorial( a-1));
}
a=1
Return to L4
a=2
Return to L4
a=3
Return to L4
a=4 a=4
Return to L4 Return to L4
a=5 a=5
… a=5
Initial After 1 recursion After 4th recursion
a=1
Return to L4
a=2 a = 2*1 = 2
Return to L4 Return to L4
a=3 a=3 a = 3*2 = 6
Return to L4 Return to L4 Return to L4
a=4 a=4 a=4 a = 4*6 = 24
Return to L4 Return to L4 Return to L4 Return to L4
a=5 a=5 a=5 a=5 a = 5*24 = 120
After 4th recursion Result
CIS 068
Algebraic Expression
• An algebraic expression is a legal combination of operands and
the operators.
• Operand is the quantity (unit of data) on which a mathematical
operation is performed.
• Operand may be a variable like x, y, z or a constant like 5, 4,0,9,1
etc.
• Operator is a symbol which signifies a mathematical or logical
operation between the operands. Example of familiar operators
include +,-,*, /, ^
• Considering these definitions of operands and operators now we
can write an example of expression as x+y*z.
Infix, Postfix and Prefix Expressions
• INFIX Expression : From our schools times we have been
familiar with the expressions in which operands surround the
operator, e.g. x+y, 6*3 etc this way of writing the Expressions
is called infix notation.
A+B
(A+B) * (C + D)
A-B/(C*D^E)
Examples of infix to prefix and post fix
Infix PostFix Prefix
(A+B) * (C + D)
A-B/(C*D^E)
Examples of infix to prefix and post fix
Infix PostFix Prefix
bitwise or 6 left-to-right
logical or 4 left-to-right
?: conditional 3 right-to-left
= += -= assignment 2 right-to-left
/= *= %=
<<= >>=
&= ^= =
, comma 1 left-to-right
I/P Stack
Symbol Preceden Precedenc
ce f e g
+, – 1 2
^ or ⬆ or $
*, / 3 4
6 5
( 7 0
) 0 -
When do we need to use them…
• So, what is actually done is expression is
scanned from user in infix form; it is converted
into prefix or postfix form and then evaluated
without considering the parenthesis and
priority of the operators.
Example
Infi x Reverse Polish (Postfi x)
((a+b)/ d^((e-f)+g)) ab+def-g+^/
z+(y*x-(w/ v^u)*t)*s zyx*wvu^/ t*-s*+
(a+b)*(c-d)^e*f ab+cd-e^*f*
(a+b)*(c^(d-e)+f)-g ab+cde-^f+*g-
a+(((b-c)*(d-e)+f)/ g)^(h-j) abc-de-*f+g/ hj-^+
a/ b^c+d*e-a*c abc^/ de*+ac*-
(a+b)*c+d/ (b+a*c)+d ab+c*dbac*+/ +d+
((((m^n^d)-(a^c-b)/ (p-q)*e)+f)-x) mnd^^ac^b-pq-/ e*-f+x-
m+(n-(a^(b-c)*d)/ f) mnabc-^d*f/ -+
Algorithm for Infix to Postfix
1) Examine the one by one character from the input.
2) If it is operand, append in output string.
3) If it is opening parenthesis, push it on stack.
4) If it is an operator, then
i) If stack is empty, push operator on stack.
ii) If the top of stack is opening parenthesis, push operator on stack
iii) If operator has higher priority than the top of stack, push operator on
stack.
iv) Else pop the operator from the stack and append in output string,
repeat step 4