Stack Applications
Stack Applications
E
P
G G
O N N N
A
P
A A A
Name : Name : Name : Name : Name :
“L” “LE” “LEG” “LEGN” “LEGNA”
Polish Notation
● The process of writing the operators of expression either before their
operands or after them is called “Polish Notation”
● The main property of Polish Notation is that the order in which operations are
to be performed is ascertained by the position of the operators and
operands in the expression
Polish Notation
● The notation refers to these complex arithmetic expressions in three forms:
○If the operator symbols are placed between its
operands, then the expression is in infix notation
■A + B
○If the operator symbols are placed before its operands,
then the expression is in prefix notation
■ +AB
○If the operator symbols are placed after its operands,
then the expression is in postfix notation
■ AB+
Notation
A+B + AB AB +
(A - C) + B + - ACB AC – B +
A+(B*C) + A * BC ABC *+
(A+B)/(C-D) /+ AB – CD AB + CD -/
infixVect
(a+b-c)*d–(e+f)
postfixVect
Infix to postfix conversion
stackVect
infixVect
a+b-c)*d–(e+f)
postfixVect
(
Infix to postfix conversion
stackVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
(
Infix to postfix conversion
stackVect
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
Infix to postfix conversion
stackVect
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
Infix to postfix conversion
stackVect
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
Infix to postfix conversion
stackVect
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
Infix to postfix conversion
stackVect
infixVect
*d–(e+f)
postfixVect
ab+c-
Infix to postfix conversion
stackVect
infixVect
d–(e+f)
postfixVect
ab+c-
*
Infix to postfix conversion
stackVect
infixVect
–(e+f)
postfixVect
ab+c-d
*
Infix to postfix conversion
stackVect
infixVect
(e+f)
postfixVect
ab+c–d*
-
Infix to postfix conversion
stackVect
infixVect
e+f)
postfixVect
ab+c–d*
(
-
Infix to postfix conversion
stackVect
infixVect
+f)
postfixVect
ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+-
Infix of Prefix Expression
/* Reading the expression takes place from left to right */
ReverseVect
c*b+a
postfixVect
Infix to postfix conversion
stackVect
infixVect
*b+a
postfixVect
c
Infix to postfix conversion
stackVect
infixVect
b+a
postfixVect
c
*
Infix to postfix conversion
stackVect
infixVect
+a
postfixVect
cb
*
Infix to postfix conversion
stackVect
infixVect
a
postfixVect
cb*
+
Infix to postfix conversion
stackVect
infixVect
postfixVect
cb*a
+
Infix to postfix conversion
stackVect
infixVect
postfixVect
cb*a+
Infix to postfix conversion
stackVect
infixVect
postfixVect
cb*a+
prefixVect
+a*bc
Evaluation of Prefix Expression
n=1 1
2 * fact(1)
n=2 2*1=2
3 * fact(2)
n=3 3*2=6
4 * fact(3)
n=4 4 * 6 = 24
1. Preparation
2. Translating each recursive call P in procedure P.
3. Translating each return in procedure P.
1. Preparation
(a)define a stack STPAR for each parameter PAR, a stack
STVAR for each local variable VAR, and a local variable
ADD and a stack STADD to hold return address.
(b) Set TOP = NULL
2. Translation of “step K.call P.”
(a) Push the current values of the parameters and local
variable onto the appropriate stacks, and push the
new return address [step ] K+1 onto STADD.
(b) reset the parameters using the new argument values.
(c) go to step 1.[The beginning of procedure P]
3. Translation of “Step J.return”
(d)if STADD is empty, then return.[control is return to the
main program].
(e)Restore the top values of the stacks.That is, set the
parameters and local variables equal to the top values on
the stacks, and set ADD equal to the top value on the
STADD.
(f) Go to the step ADD.
4. Step L. return
Translation of recursive to non-recursive procedure
●Declare STACK to hold all the local variables
●Push all the local variables and parameters called by
value into the stack
●At the end of recursive function or whenever the function
returns to the calling program, the following steps
should be performed
○If stack is empty, then the recursion has finished;
make a normal return
○Otherwise pop the stack to restore the values of all
local variables and parameters called by value
Difference between Recursion and Iteration
●ITERATION ●RECURSIVE
● It is a process of executing ● Recursion is a technique of
statements repeatedly, until defining anything in terms of
some specific condition is itself
specified ● There must be an exclusive if
● Iteration involves four clear cut statement inside the recursive
steps, initialization, condition, function specifying stopping
execution and updating condition
● Any recursive problem can be ● Not all problems has recursive
solved iteratively solution
● Iterative computer part of a ● Recursion is generally a worst
problem is more efficient in option to go for simple program
terms of memory utilization and or problems not recursive in
execution speed nature
Tower of Hanoi
●Suppose three peg – A, B & C
●On peg A there are finite numbers of n disks with
decreasing size
Tower of Hanoi - Rules
●Only one disk may be moved at a time (only the top disk
on any peg may be moved to any other peg)
●A larger disk can be placed on a smaller (smaller disk can
not be placed on larger)
Tower of Hanoi – Plates Move
Algorithm : Tower(N, BEG, AUX, END)
●This procedure gives a recursive solution to the Towers of
Hanoi problem for N disks
Step 1. If N = 1 then
(a) Write : BEG -> END
(b) Return
End of If Structure
Step 2. [Move N – 1 disks peg BEG to peg AUX ]
Call TOWER(N-1, BEG, END, AUX)
Step 3. Write : BEG->END
Step 4. [Move N – 1 disks peg AUX to peg END]
Call TOWER(N-1, AUX, BEG, END)
Step 5. Return