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

Lecture 09 - Applications of Stacks

The document discusses the applications of stacks, particularly in arithmetic expression evaluation using different notations such as infix, prefix, and postfix. It outlines the process of converting expressions between these notations and provides algorithms for evaluating arithmetic expressions. Additionally, it includes examples and classroom activities related to the topic.

Uploaded by

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

Lecture 09 - Applications of Stacks

The document discusses the applications of stacks, particularly in arithmetic expression evaluation using different notations such as infix, prefix, and postfix. It outlines the process of converting expressions between these notations and provides algorithms for evaluating arithmetic expressions. Additionally, it includes examples and classroom activities related to the topic.

Uploaded by

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

Application of stack

CSE 2101: Data Structures


Lecture 09: Applications of Stacks

Md Zahidul Islam
Computer Sciece & Engineering Discipline
[email protected]

April 07, 2015


Application of stack

O UTLINE

Application of stack
Application of stack

O BJECTIVES

I Discover stack applications


I Learn how to use a stack for arithmatic expression
evaluation
Application of stack

E XPRESSION EVALUATION USING STACK

I Polish notation (depends on the position of the operator):


I infix: A + B
I prefix: +A B
I postfix: A B+
I How about A + B × C and (A + B) × C??
I So, operator precedence plays an important role here
I Two rules applied during the conversion:
1. operations with the highest precedence are converted first
2. each time a portion of the expression is converted, it
becomes an operand
Application of stack

P OLISH NOTATIONS

I We consider five binary operations: addition (+),


subtraction (-), multiplication (∗), division (/), and
exponentiation($ or ˆ).
I Precedence: Exponentiation > Multiplication or Division
> Addition or Subtraction.
I Convert ((A + B) ∗ C − (D − E))$(F + G) to postfix and
prefix.
Application of stack

P OLISH NOTATION CONVERSION

I To prefix: ((A + B) ∗ C − (D − E)) $ (F + G)


Application of stack

P OLISH NOTATION CONVERSION

I To prefix: ((A + B) ∗ C − (D − E)) $ (F + G)


= ((+A B) ∗ C − (−D E))$(+F G)
Application of stack

P OLISH NOTATION CONVERSION

I To prefix: ((A + B) ∗ C − (D − E)) $ (F + G)


= ((+A B) ∗ C − (−D E))$(+F G)
= ((∗ + A B C) − (−D E)) $ (+F G)
Application of stack

P OLISH NOTATION CONVERSION

I To prefix: ((A + B) ∗ C − (D − E)) $ (F + G)


= ((+A B) ∗ C − (−D E))$(+F G)
= ((∗ + A B C) − (−D E)) $ (+F G)
= (− ∗ +A B C − D E) $ (+F G)
Application of stack

P OLISH NOTATION CONVERSION

I To prefix: ((A + B) ∗ C − (D − E)) $ (F + G)


= ((+A B) ∗ C − (−D E))$(+F G)
= ((∗ + A B C) − (−D E)) $ (+F G)
= (− ∗ +A B C − D E) $ (+F G)
=$ - * + A BC - DE + F
I To postfix: ((A + B) ∗ C − (D − E)) $ (F + G)
Application of stack

P OLISH NOTATION CONVERSION

I To prefix: ((A + B) ∗ C − (D − E)) $ (F + G)


= ((+A B) ∗ C − (−D E))$(+F G)
= ((∗ + A B C) − (−D E)) $ (+F G)
= (− ∗ +A B C − D E) $ (+F G)
=$ - * + A BC - DE + F
I To postfix: ((A + B) ∗ C − (D − E)) $ (F + G)
= ((A B+) ∗ C − (D E−)) $ (F G+)
Application of stack

P OLISH NOTATION CONVERSION

I To prefix: ((A + B) ∗ C − (D − E)) $ (F + G)


= ((+A B) ∗ C − (−D E))$(+F G)
= ((∗ + A B C) − (−D E)) $ (+F G)
= (− ∗ +A B C − D E) $ (+F G)
=$ - * + A BC - DE + F
I To postfix: ((A + B) ∗ C − (D − E)) $ (F + G)
= ((A B+) ∗ C − (D E−)) $ (F G+)
= ((A B + C∗) − (D E−)) $ (F G+)
Application of stack

P OLISH NOTATION CONVERSION

I To prefix: ((A + B) ∗ C − (D − E)) $ (F + G)


= ((+A B) ∗ C − (−D E))$(+F G)
= ((∗ + A B C) − (−D E)) $ (+F G)
= (− ∗ +A B C − D E) $ (+F G)
=$ - * + A BC - DE + F
I To postfix: ((A + B) ∗ C − (D − E)) $ (F + G)
= ((A B+) ∗ C − (D E−)) $ (F G+)
= ((A B + C∗) − (D E−)) $ (F G+)
= (A B + C ∗ D E − −) $ (F G+)
Application of stack

P OLISH NOTATION CONVERSION

I To prefix: ((A + B) ∗ C − (D − E)) $ (F + G)


= ((+A B) ∗ C − (−D E))$(+F G)
= ((∗ + A B C) − (−D E)) $ (+F G)
= (− ∗ +A B C − D E) $ (+F G)
=$ - * + A BC - DE + F
I To postfix: ((A + B) ∗ C − (D − E)) $ (F + G)
= ((A B+) ∗ C − (D E−)) $ (F G+)
= ((A B + C∗) − (D E−)) $ (F G+)
= (A B + C ∗ D E − −) $ (F G+)
=A B + C * DE - - FG + $
Application of stack

I NFIX TO POSTFIX

I Let we have an expression ((A + B) ∗ C):


Application of stack

I NFIX TO POSTFIX

I Let we have an expression ((A + B) ∗ C):


last part to convert
z }| {
(1 (2 A + B)2 ∗ C)1
| {z }
first part to convert

I We first handle the last pair of parentheses encloses


(LIFO!!)
I In a postfix notation we add the operands first then the
operator, so the operators must be stored to be retrieved
and inserted into its proper position (depending on
precedence and order of occurrence)
Application of stack

S TEPS TO CONVERT AN INFIX EXPRESSION TO POSTFIX

Start with an infix expression and scan the expression from let
to right:
Step 1 When we get an operand we add it to the postfix expression
Step 2 When we get an opening parentheses we push it on the stack
Step 3 When we get a closing parenthesis we pop the stack and add the symbols to the
postfix expression until we get the opening parenthesis on to the stack
Step 4 When an operator is encountered
I We peek the stack and check the precedence of the operator and the
peeked operator
I If the peeked operator has the same or higher precedence, then we pop
and add the peeked operator to the postfix expression
I Step 4 continues until we peek an operator which has less precedence
than the operator encountered in the infix expression.
Step 5 We push the operator on to the stack
Step 6 If the stack is not empty after we reach the end of the expression, we pop the
stack and add the operators to the postfix expression
Application of stack

A N EXAMPLE
I (A ∗ B − (C/D$E))
Application of stack

A N EXAMPLE
I (A ∗ B − (C/D$E))
Symbol scanned Stack Postfix expression
( (
A ( A
∗ (, ∗ A
B (, ∗ AB
− (, − AB∗
( (, −, ( AB∗
C (, −, ( AB ∗ C
/ (, −, (, / AB ∗ C
D (, −, (, / AB ∗ CD
$ (, −, (, /, $ AB ∗ CD
E (, −, (, /, $ AB ∗ CD
) (, − AB ∗ C D $, /
) AB ∗ CD$/−

I HW1: Program to convert an infix program to prefix form


I HW2: Work with expression having operands with more
than one digit
Application of stack

F ORMAL ALGORITHM : INFIX TO POSTFIX


1. Repeat step 1 until the end of input
[Scan the input from left to right, one character at a time]
sym = next input character
2. If sym is an operand
add sym to P (here P is the postfix expression)
3. If sym is an opening parentheses
Push ‘(’ on to the opstack
4. If sym is an operator op’
a. op = opstack.peek()
b. if op has an higher or same precedence than op’
topSym = opstack.pop()
add topSym to P
[Repeat a and b until we get an op which has lower precedence than op’ or an ‘)’]
[End of if structure]
5. if sym is a right parentheses
a. repeatedly pop the opstack and add the operators to P until a left parentheses is
encountered.
b. Pop the left parentheses from the opstack (Do not add it to P)
6. If the opstack is not empty
Repeatedly pop opstack and add the operators to P
[End of algorithm]
Application of stack

E VALUATING AN ARITHMATIC EXPRESSION

I 6 + (2 ∗ 3)
= 6 + 6 = 12
Application of stack

E VALUATING AN ARITHMATIC EXPRESSION

I 6 + (2 ∗ 3)
= 6 + 6 = 12
I 623 ∗ +
Application of stack

E VALUATING AN ARITHMATIC EXPRESSION

I 6 + (2 ∗ 3)
= 6 + 6 = 12
I 623 ∗ +
1. Scan P from left to right and repeat steps 2, 3 and 4 for each element of P.
2. If an operand is encountered, push it on stack.
3. If an operator opr is encountered,
a. Set, op2 = s.pop()
op1 = s.pop();
b. Perform, result = op1 opr op2;
c. Push result back onto stack
[End of if]
[End of step 2 loop]
4. Set value equal to the top element on stack
5. Exit
Application of stack

A N EXAMPLE

I 623 + −3 ∗ 12 + $
Application of stack

A N EXAMPLE

I 623 + −3 ∗ 12 + $
Symb op1 op2 result Stack
6 6
2 6, 2
3 6, 2, 3
+ 2 3 5 6, 5
− 6 5 1 1
3 1, 3
∗ 1 3 3 3
1 3, 1
2 3, 1, 2
+ 1 2 3 3, 3
$ 3 3 27 27
Application of stack

C LASS R OOM G ROUP W ORK

Implement the program to evaluate an arithmatic expression


given in infix form.
Application of stack

Thank you!!!
Solve ProblemSet05.

You might also like