You are on page 1of 7

Lecture 05: Stack Applications CS221: Data Structures & Algo.

Stack Applications

Dr. Zahid Halim

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 05: Stack Applications CS221: Data Structures & Algo.

Quiz
• Write a function that finds the largest number stored in a linked
list.

• Assume we have one-way linked list.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 05: Stack Applications CS221: Data Structures & Algo.

Example of Stacks
INFIX, POSTFIX and PREFIX
– Infix: A+B-C
– Postfix: AB+C-
– Prefix: -+ABC

Order of Precedence of Operators


– Parenthesis
– Exponentiation
– Multiplication/Division
– Addition/Subtraction

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 05: Stack Applications CS221: Data Structures & Algo.

Infix: ( (A+B)*C-(D-E) ) $ (F+G)


Conversion to Postfix Expression

• ( (AB+)*C-(DE-) ) $ (FG+)
• ( (AB+C*)-(DE-) ) $ (FG+)
• (AB+C*DE--) $ (FG+)
• AB+C*DE- -FG+$

Exercise: Convert the following to Postfix


• ( A + B ) * ( C – D)
• A $ B * C – D + E / F / (G + H)

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 05: Stack Applications CS221: Data Structures & Algo.

Conversion to Prefix Expression


The precedence rules for converting an expression from infix to prefix are identical. The
only change from postfix is that the operator is placed before the operands rather than after
them.

Evaluating a Postfix Expression


Each operator in a postfix string refers to the previous two operands in the string.

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 05: Stack Applications CS221: Data Structures & Algo.
Algorithm to Evaluate a Postfix Expression

opndstk = the empty stack Example:


/* scan the input string reading one element */ Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 +
/* at a time into symb */ symb opnd1 opnd2 value opndstk
while (not end of input) { 6 6
symb = next input character; 2 6,2
if (symb is an operand)
3 6,2,3
push(opndstk, symb)
+ 2 3 5 6,5
else {
/* symb is an operator */ - 6 5 1 1

opnd1 = pop(opndstk); 3 6 5 1 1,3

opnd2 = pop(opndstk); 8 6 5 1 1,3,8

value = opnd2 OPERATOR opnd1; 2 6 5 1 1,3,8,2


push(opndstk, value); / 8 2 4 1,3,4
} /* end else */ + 3 4 7 1,7
} /* end while */ * 1 7 7 7
return (pop(opndstk)); 2 1 7 7 7,2

$ 7 2 49 49

3 7 2 49 49,3

+ 49 3 52 52

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 05: Stack Applications CS221: Data Structures & Algo.

Algorithm to Convert Infix to Postfix


Q is the Expression in infix
P is the output in postfix

1. PUSH “(“ on to the STACK and add “)” at end of Q


2. Scan Q from Left to Right repeat step 3-6 for each element of Q until STACK is
empty
3. If an operand is encountered add it to P
4. If a Left parenthesis is encountered push it on to STACK
5. If an operator X is encountered then:
a) Repeatedly pop from STACK and add to P each operator which has
same or higher precedence than X
b) Add X to STACK
6. If a right parenthesis is encountered then
a) Repeatedly pop from STACK and add to P each operator until a left
parenthesis is encountered
b)Remove the left parenthesis.
7. Exit

Consider an Example: Q: A + ( B * C - ( D / E ^ F ) * G ) * H

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like