0% found this document useful (0 votes)
6 views47 pages

Stack Applications

The document discusses various applications of stacks, including reversing strings and lists, and evaluating expressions using Polish Notation (infix, prefix, and postfix). It also explains the conversion of infix expressions to postfix and prefix forms, as well as the evaluation of prefix expressions. Additionally, it covers recursion, its translation to non-recursive procedures, and compares recursion with iteration, concluding with an explanation of the Tower of Hanoi problem and its recursive solution.

Uploaded by

tdhanush2299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views47 pages

Stack Applications

The document discusses various applications of stacks, including reversing strings and lists, and evaluating expressions using Polish Notation (infix, prefix, and postfix). It also explains the conversion of infix expressions to postfix and prefix forms, as well as the evaluation of prefix expressions. Additionally, it covers recursion, its translation to non-recursive procedures, and compares recursion with iteration, concluding with an explanation of the Tower of Hanoi problem and its recursive solution.

Uploaded by

tdhanush2299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Applications of Stack

Reverse String or List


● We can accomplish this task by pushing each character or member from the
string/list in the order it appears
● When the line is finished, characters are then proposed off the stack

“They come off in a reverse order”


Reverse string – Name=“Angel”
L
P E E
G G G
U
N N N N
S A A A A A

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

Infix Notation Prefix Notation Postfix 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 -/

(A + (B * C))/(C – (D * B)) /+ A * BC – C * DB ABC * + CDB * - /


Convert Infix expression to Postfix expression
A – It is an expression B – Postfix expression
Step 1. Push left parenthesis “(“ into STACK and add right parenthesis “)” to the
end of A
Step 2. Scan A from left to right and repeat step 3 to 6 for each element of A
until the stack is empty
Step 3. If an operand is encountered, add it to B
Step 4. If a left parenthesis is encountered push it onto the stack
Step 5. If an operator is encountered then
1. Repeatedly pop from the STACK and add to B each operator (on the top of
stack) which has the same precedence as or higher precedence than
operator
2. Add operator to STACK
Step 6. If a right parenthesis is encountered, then
3. Repeatedly pop from the STACK and add to B each operator (on the top of
STACK) until a left parenthesis is encountered
4. Remove the left parenthesis. (Do not add left parenthesis to B)
Step 7. Exit
Infix to postfix conversion

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 */

Step 1. Reverse the infix expression i.e A+B*C will

become C*B+A. Note while reversing each ‘(‘ will


become ‘)’ and each ‘)’ becomes ‘(‘.
Step 2. Obtain the postfix expression of the modified

expression i.e CB*A+.


Step 3. Reverse the postfix expression. Hence in our

example prefix is +A*BC.


Infix to prefix conversion
infixVect
a+b*c

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

/* Reading the expression take place from right to left /*


Step 1. Read the next element
Step 2. If element is operand then
i. Push the element in the stack
Step 3. If element is operator then
i. Pop two operands from the stack
ii. Evaluate the expression formed by two operands and the operator
iii. Push the results of the expression in the stack end
Step 4. if no more elements then
i. Pop the result else
goto step 1
Step 5. Exit
Recursion
●The function which call itself (In function body ) again and
again is known as recursive function. This function will call
itself as long as the condition is satisfied.
●This recursion can be removed by two ways:
1. Through Iteration.
2. Through Stack

●Many mathematical functions can be defined recursively:


○ Factorial
○ Fibonacci
○ Euclid's GCD (greatest common denominator)
○ Fourier Transform
Factorial function in c
int fact(int n)
{
if(n==1)
{
return 1;
}
else
{
return(n*fact(n-1));
}
}
Use of STACK – FACTORIAL EXECUTION

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

n=5 5 * fact(4) 5 * 24 = 120


Removal Through Stack
●Suppose P is a recursive procedure . The translation of
recursive procedure P into a non recursive procedure
work as follows:

● First of all, one defines:


1. A stack STPAR for each parameter PAR
2. A stack STVAR for each local variable VAR
3.A local variable ADD and a stack STADD to hold return
address.
●The algorithm which translates the recursive procedure P
into a non recursive procedure follows. It consist of three
parts:

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy