Stack Unitv
Stack Unitv
❑ Recursion
Stacks
end STACK
4 Stack Empty
Locations=07
3
2
1
Top = Stack Empty
0
-1
Empty Stack
4 Stack Empty
Locations=06
3
2
1
Top =
0 50 0
Push (50)
4 Stack Empty
Locations=05
3
2
1 25
Top =
0 50 1
Push (25)
4 Stack Empty
Locations=04
3
2 78
1 25
Top =
0 50 2
Push (78)
4 Stack Empty
Locations=05
3
2 Popped element :
78
1 25
Top =
0 50 1
Pop ()
6 Stack Max
5 Size=07
4 Stack Empty
Locations=06
3
2 Popped element :
1 25
Top =
0 50 0
Pop ()
4 Stack Empty
Locations=07
3
2
1 Popped element :
0 50
Top =
Pop () -1
4 Stack Empty
Locations=03
3 78
2 90
1 45
Top =
0 23 03
Push (23),Push(45),Push(90),Push(78)
6 80 Stack Max
5 65 Size=07
4 43 Stack Empty
Locations=00
3 78
2 90 Top =
06
1 45
0 23
Push (43),Push(65),Push(80)
Pop ()
• Function: Removes top Item from stack and returns it .
❑ Converting Expressions
❑ Evaluating expressions
❑ String Reverse
❑ Number Conversion
❑ Backtracking
❑ Parenthesis Checking
When function
execution completes, it
is popped from stack
Rank Operator
1 ^
2 */%
3 + - (Binary)
We can convert any expression to any other two forms using Stack Data Structure
A * (B + C) – D / E
Output as it is.
the Stack
inside, is maximum.
★But when another operator is to come on the top of ‘(‘ then its
precedence is least.
is
★Next token ), means that pop all the elements from Stack and
parenthesis.
★Next, we will insert the division operator into the Stack because its
Expression as it is.
postfixVect
infixVect
a+b-c)*d–(e+f)
postfixVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
infixVect
*d–(e+f)
postfixVect
ab+c-
infixVect
d–(e+f)
postfixVect
ab+c-
postfixVect
ab+c-d
postfixVect
ab+c–d*
postfixVect
ab+c–d*
(
-
postfixVect
ab+c–d*e
(
-
postfixVect
+ ab+c–d*e
(
-
postfixVect
+ ab+c–d*ef
(
-
infixVect
postfixVect
ab+c–d*ef+
postfixVect
ab+c–d*ef+-
icp(ch)
{ isp( ch)
if(ch=='+' || ch=='-') {
return 1; if(ch=='+' || ch=='-')
if(ch=='*' || ch=='/') return 1;
return 2;
if(ch=='*' || ch=='/')
if(ch=='^')
return 2;
return 3;
if(ch=='(') if(ch=='^')
return 5; return 4;
else else
return 0; return 0;
} }
Operation
Expression Stack Output
5^E+D*(C^B+A) Empty -
^E+D*(C^B+A) Empty 5 Print
E+D*(C^B+A) ^ 5 Push
+D*(C^B+A) ^ 5E Push
D*(C^B+A) + 5E^ Pop And Push
*(C^B+A) + 5E^D Print
(C^B+A) +* 5E^D Push
C^B+A) +*( 5E^D Push
^B+A) +*( 5E^DC Print
B+A) +*(^ 5E^DC Push
+A) +*(^ 5E^DCB Print
A) +*(+ 5E^DCB^ Pop And Push
) +*(+ 5E^DCB^A Print
End +* 5E^DCB^A+ Pop Until '('
End Empty 5E^DCB^A+*+ Pop Every element
77
Algorithm :- Postfix to Infix
1. Read the symbol from the input based on the input symbol go to step
2 or 3.
2. If symbol is operand then push it into stack.
3. If symbol is operator then pop top 2 values from the stack.
4. this 2 popped value is our operand .
5. create a new string and put the operator between this operand in
string.
6. push this string into stack.
7. At the end only one value remain in stack which is our infix
expression.
Final answer is
• 49
• 51
• 52
• 7
• None of these
It computes f! (factorial)
a!
a * (a-1)!
a=1
Return to
L1 a = 2*1 =
a=2
Return to 2 to
Return
L2 L2 a = 3*2 =
a=3 a=3
Return to
Return to L3
6 to
Return
a = 4*6 =
L3 L3
a=4 a=4 a=4 24
Return to Return to Return to Return to
L4 L4 L4 L4 a = 5*24 =
a=5 a=5 a=5 a=5
120
After 4th recursion Result
Solution on stack
Solution on stack
n
f(x) = x
❑ Recursive solutions are often less efficient, in terms of both time and space,
than iterative solutions
• The recursive version does about the same amount of work as the non
recursive version
• The recursive version is shorter and simpler than the non recursive
solution
bottommost bottommost
stack 1 stack 2
More than two stacks (n)
memory is divided into n equal segments
boundary[stack_no]
0 ≤ stack_no < MAX_STACKS
top[stack_no]
0 ≤ stack_no < MAX_STACKS
All stacks are empty and divided into roughly equal segments.