0% found this document useful (0 votes)
24 views26 pages

Application of Stack

Uploaded by

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

Application of Stack

Uploaded by

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

05/07/24 Dr. Kazi A.

Kalpoma 1
Application of stack continuation……
• Syntax parsing, Expression evaluation and
Expression conversion.

• Banking Transaction View


- You view the last transaction first.

• Backtracking and implementation of recursive


function, calling function.

• Towers of Hanoi

• Inventory Systems like Issuing students the Multi-


meters… you will be issued the most recently
returned item likely
Validity checking of an arithmetic expression

When there is a opening parenthesis; brace or


bracket, there should be the corresponding
closing parenthesis. Otherwise, the expression
is not a valid one. We can check this validity
using stack.

05/07/24 Dr. Kazi A. Kalpoma 3


Stack in Problem Solving
• Consider a mathematical expression that includes
several sets of nested parenthesis, e.g
( x + (y – (a +b)) )
• We want to ensure that parenthesis are nested
correctly and the expression is valid

• Validation
1. There is an equal number of closing and opening
parentheses
2. Every closing parenthesis is preceded by a
matching opening parenthesis
Algorithm
• Whenever an opening is encountered, PUSH()
on to stack
• Whenever a closing is encountered,
– If stack is empty, expression is invalid.
– If stack is nonempty, POP() the stack and check
with corresponding closing parenthesis. If match
occurs continue. Otherwise expression is invalid.
• When the end of expression is reached, stack
must be empty; otherwise expression invalid.
Symbol Stack
[ [
( [(
Example: A [(
+ [(
[(A+B)-{(C+D)-E}] B [(
) [
- [
{ [{
( [{(
C [{(
+ [{(
D [{(
) [{
- [{
E [{
} [
]
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: 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.

• POSTFIX: Postfix notation are also Known as Reverse Polish


Notation (RPN). They are different from the infix and prefix
notations in the sense that in the postfix notation, operator
comes after the operands, e.g. xy+, xyz+* etc.

• PREFIX: Prefix notation also Known as Polish notation. In the


prefix notation, as the name only suggests, operator comes
before the operands, e.g. +xy, *+xyz etc.
Operator Priorities
• How do you figure out the operands of an
operator?
–a+b*c
–a*b+c/d
• This is done by assigning operator priorities.
priority(*) = priority(/) > priority(+) = priority(-)
• When an operand lies between two operators,
the operand associates with the operator that
has higher priority.
Tie Breaker
• When an operand lies between two operators
that have the same priority, the operand
associates with the operator on the left.
–a*b/c/d

Delimiters
• Sub expression within delimiters is treated as a
single operand, independent from the remainder
of the expression.
– (a + b) * (c – d) / (e – f)
WHY PREFIX and POSTFIX ?

• Why to use these weird looking PREFIX and POSTFIX notations


when we have simple INFIX notation?

• To our surprise INFIX notations are not as simple as they seem


specially while evaluating them. To evaluate an infix
expression we need to consider Operators’ Priority and
Associative property
– For example expression 3+5*4 evaluate to 32 i.e. (3+5)*4
or to 23 i.e. 3+(5*4).

• To solve this problem Precedence or Priority of the operators


were defined. Operator precedence governs evaluation order.
An operator with higher precedence is applied before an
operator with lower precedence.
Infix Expression Is Hard To Parse
• Need operator priorities, tie breaker, and
delimiters.
• This makes computer evaluation more
difficult than is necessary.
• Postfix and prefix expression forms do not
rely on operator priorities, a tie breaker,
or delimiters.
• So it is easier to evaluate expressions that
are in these forms.
Both prefix and postfix notations have an advantage over infix
that while evaluating an expression in prefix or postfix form we
need not consider the Priority and Associative property (order of
brackets).

– E.g. x/y*z becomes


– */xyz in prefix and
– xy/z* in postfix.

Both prefix and postfix notations make


Expression Evaluation a lot easier.
When do we need to use them… 
• So, what is actually done in 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.
Algorithm for Infix to Postfix conversion
1) Examine the ith element in the input, infix[i].
2) If it is operand, output it at kth location (pop to postfix[k]).
3) else If it is opening parenthesis ‘(’, push it on stack.
4) else If it is a closing parenthesis ‘)’,
pop (operators) from stack and output to postfix[k] them until an
opening parenthesis ‘(’ is encountered in the top of stack.
pop and discard the opening parenthesis ‘(’ from stack.
5) else If it is an operator ‘+’, ‘-’, ‘*’, ‘/’, ‘%’ then
i) If stack is empty, push operator on stack.
ii) else If the top of stack is opening parenthesis ‘(’, push on stack
iii) else If it has higher priority than the top of stack, push on stack.
iv) else pop the operator from the stack and output it, repeat step 5

6) If there is more input in infix[], go to step 1


7) If there is no more input, pop the remaining operators to output.
Suppose we want to convert infix: 2*3/(2-1)+5*3 into Postfix form,

Input (infix) Stack Output (Postfix)


2 Empty 2
* * 2
3 * 23
/ / 23*
( /( 23*
2 /( 23*2
- /(- 23*2
1 /(- 23*21
) / 23*21-
+ + 23*21-/
5 + 23*21-/5
* +* 23*21-/5
3 +* 23*21-/53
Empty 23*21-/53*+
So, the Postfix Expression is 23*21-/53*+
Example
• ( 5 + 6) * 9 +3
will be
• 56+9*3+

Conversion of infix to postfix: try it at home.


Evaluating Postfix Notation
• Use a stack to evaluate an expression in
postfix notation.
• The postfix expression to be evaluated is
scanned from left to right.
• Variables or constants are pushed onto the
stack.
• When an operator is encountered, the
indicated action is performed using the top
elements of the stack, and the result
replaces the operands on the stack.
Algorithm: To evaluate a postfix value

1. Add “)” at the right end of the expression.


2. Scan p from left to right and repeat step 3 and 4
for each element of p until encountered “)”.
3. If an operand is encountered, PUSH it in stack.
4. If an operator is encountered, then
1. To remove two element from stack Call POP() twice
and first POP() put to A and second one to B.
2. Evaluate C = B  A.
3. PUSH(C) in stack.
5. Set the value = POP(), the top element of stack.
6. Exit
Postfix expressions:
Algorithm using stacks (cont.)
Question : Evaluate the following
expression in postfix :
623+-382/+*2^3+
Final answer is ?
• 49
• 51
• 52
• 7
• None of these
Evaluate- 623+-382/+*2^3+ )
3
2 2
6 6 6

623+-382/+*2^3+)
5
6

65-382/+*2^3+) 1382/+*2^3+)
8 2 4
3 3 8 3
1 1 1 3 1
1
Cont. of Evaluation- 623+-382/+*2^3+
134+*2^3+)
4
3 7
1 1

72^3+) 493+) )

2 3
7 7 49
= 52 49 52
Algorithm: To evaluate a prefix value

1. Add “(” at the left end of the expression.


2. Scan p from right to left and repeat step 3 and 4
for each element of p until encountered “(”.
3. If an operand is encountered, PUSH it in stack.
4. If an operator is encountered, then
1. To remove two element from stack Call POP() twice
and first POP() put to A and second one to B.
2. Evaluate C = A  B
3. PUSH(C) in stack.
5. Set the value = POP(), the top element of stack.
6. Exit

You might also like