0% found this document useful (0 votes)
23 views34 pages

DS Week 8 Lecture Rev Operations of Stacks Part 2 by DR Gaurav

Uploaded by

titirshaasingh
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)
23 views34 pages

DS Week 8 Lecture Rev Operations of Stacks Part 2 by DR Gaurav

Uploaded by

titirshaasingh
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/ 34

Subject: Data Structure

Using C++ (CSET243)

Week 8 Lecture

Stack and Its Applications


What, Why and How?

by

Dr Gaurav Kumar
Asst. Prof, Bennett University
Quick Recap of Previous Weeks’ Learnings

• Understood the Stacks and Real Life Applications


Applications of Stack
7) Expression Conversion Ex- 2 + 4

Stack is also used for expression conversion. This is one of the most
important applications of stack.

Ex- 2 + 4 → 24+
(Infix Notation) (Postfix Notation)
Understanding Polish Notation
• Polish Notation in the data structure is a method of
expressing mathematical, logical, and algebraic equations
universally.

• This notation is used by the compiler to evaluate


mathematical equations based on their order of operations.

• When parsing mathematical expressions, three types of


notations are commonly used
Prefix: : Infix Notation, Prefix Notation, and Postfix Notation.
Importance of Polish Notation

If the expression is: 4 + 6 * 2

Output ?? 16

20

Results Depend on the Order


Importance of Polish Notation

If the expression is: (4 + 6) * 2

Output ?? 16

20

Results Depend on the Order


Operator Precedence Rules

Operators Symbols

Parenthesis ( ), {}, [ ]

Exponents ^

Multiplication and Division *, /

Addition and Subtraction +,-


Importance of Polish Notation

If the expression is: 2^2^3

Output ?? 4^3 = 64

2^8 = 256

Results Depend on the Order


Associativity of Operators
Operators Symbols Associativity

Parenthesis ( ), {}, [ ] Left to Right

Exponents ^ Right to Left*

Multiplication and Division *, / Left to Right

Addition and Subtraction +,- Left to Right

*Exponential operator associativity changes with the language used. There is no standard guideline.
Expression Conversion using Stack

• Infix to prefix

• Infix to postfix

• Prefix to infix

• Prefix to postfix

• Postfix to infix
Infix to Postfix Conversion

Example: a* (b+c+d) → abc+d+*


Infix to Postfix Conversion
Example: a* (b+c+d) → abc+d+*

Step 1
Scan the expression from left to right

Step 2
If the reading symbol is an operand,
then immediately append it to the
Postfix Expression.
Infix to Postfix Conversion

Step 3
If the reading symbol is any operator +, –, *, /, then
Push it onto the Stack.

However, first, pop the operators which are already on


the stack that have higher or equal precedence than
the current operator and append them to the postfix.

If an open parenthesis ‘(‘ is there on top of the stack


then push the operator into the stack.
Infix to Postfix Conversion

Step 4
If the reading symbol is left
parenthesis ‘( ‘, then Push it
onto the Stack.
Infix to Postfix Conversion

Step 5
If the reading symbol is an
operand, then append it to the
Postfix Expression. (Step 2)
Infix to Postfix Conversion
Step 6
If the reading symbol is any operator +, –, *, /, then
Push it onto the Stack.

However, first, pop the operators which are already


on the stack that have higher or equal precedence
3
than the current operator and append them to the
postfix.

If an open parenthesis ‘(‘ is there on top of the stack


then push the operator into the stack.
Infix to Postfix Conversion

Step 6
If the reading symbol is an operand, then
append it to the Postfix Expression. (Step 2)
Infix to Postfix Conversion
Step 7
If the reading symbol is any operator +, –, *, /,
then Push it onto the Stack.

However, first, pop the operators which are


already on the stack that have higher or equal
precedence than the current operator and
append them to the postfix.

If an open parenthesis ‘(‘ is there on top of the


stack then push the operator into the stack.
Infix to Postfix Conversion

Step 8
If the reading symbol is an operand, then
append it to the Postfix Expression. (Step 2)
Infix to Postfix Conversion

Step 9
:If the reading symbol is right parenthesis ‘)’,
then Pop all the contents of the stack until
the respective left parenthesis is popped and -9
append each popped symbol to Postfix
Expression.
Infix to Postfix Conversion

Step 10
If the input is over, pop all the
remaining symbols from the stack
and append them to the postfix.
Algorithm of Infix to Postfix Conversion

Step 1: Scan all the symbols one by one from left to right in the given Infix Expression. If the reading symbol is an operand, then
immediately append it to the Postfix Expression.

Step 2: If the reading symbol is left parenthesis ‘( ‘, then Push it onto the Stack.

Step 3: If the reading symbol is right parenthesis ‘)’, then Pop all the contents of the stack until the respective left parenthesis is popped
and append each popped symbol to Postfix Expression.

Step 4: If the reading symbol is an operator (+, –, *, /), then Push it onto the Stack. However, first, pop the operators which are already
on the stack that have higher or equal precedence than the current operator and append them to the postfix. If an open parenthesis is there
on top of the stack then push the operator into the stack.

Step 5: If the input is over, pop all the remaining symbols from the stack and append them to the postfix.
Infix to Postfix Conversion

Example: a* (b+c+d) → abc+d+*

Shortcut Approach

a* (b+c+d) → a* ((b+c)+d))

→ a* ((bc+) +d)

→ a*(bc+d+)

→ abc+d+*
Advantage of Postfix Expression over Infix Exp

Example: a* (b+c+d) → abc+d+*

Postfix Expressions does not require the usage of the parenthesis which are
necessary in case of infix expression.

In postfix expression, there are no operator precedence rules. So, the


order of operations is always clear.

Postfix Expressions are easier to evaluate using a stack-based solution.


Assessment Time

Problem 1:
Infix: (a+b)*c
Postfix:?????

Output: ab+c*
Assessment Time

Problem 2:
Infix: a*b+c-d
Postfix:?????

Output: ab*c+d-
KBC Assessment Time

Problem 3

Infix: a + b * (c / (d ^ e)) ^ ((f * g) – h)

Postfix: ?????

Output: abcde^/fg*h-^*+
Postfix Expression Evaluation
Step 1: Scan the expression from left to right.

Step 2: If we encounter any operand in the expression, then


Infix expression: 6 + 3 * 4
we push the operand in the stack.
Postfix expression: 634*+
Step 3: When we encounter any operator in the expression,
Output = 18 then we pop the corresponding operands from the stack,
perform the evaluation, and computed value is again push
into the stack.

Step 4: Repeat Step1-3, when we finish with the scanning


of the expression, the final value remains in the stack.
Postfix Expression Evaluation
Example 1: Postfix expression: 6 3 4 * +
Input Stack
634*+ empty Push 6
34*+ 6 Push 3

4*+ 36 Push 4

*+ 436 Pop 4 and 3, and perform


4*3 = 12. Push 12 into the
stack.
+ 12 6 Pop 12 and 6 from the stack,
and perform 12+6 = 18.
Push 18 into the stack.

The result of the above expression is 18


Postfix Expression Evaluation
Example 2: Postfix expression: 3 4 * 2 5 * +

Input Stack
34*25*+ empty Push 3
4*25*+ 3 Push 4
*2 5 * + 43 Pop 4 and 3 from the stack and perform
4*3 = 12. Push 12 into the stack.

25*+ 12 Push 2
5*+ 2 12 Push 5
*+ 5 2 12 Pop 5 and 2 from the stack and perform
5*2 = 10. Push 10 into the stack.

+ 10 12 Pop 10 and 12 from the stack and


perform 10+12 = 22. Push 22 into the
stack.

The result of the above expression is 22


Assessment Time

Problem 1: 2 3 1 * + 9 –
Output:
(A) 4
(B) - 4
(C) 3
(D) 5

Correct Answer is B Infix Expression: 2 + (3 * 1) – 9


Assessment Time

Problem 1: 8 2 3 ^ / 2 3 * + 5 1 * –
Note that ^ is the exponentiation operator. The top
two elements of the Stack after the first * are:
(A) 6,1
(B) 5,7
(C) 3,2
(D) 1,5

Correct Answer is A
Next Week Topics for Reading

Queue and Implementation


Any Queries?
Office MCub311
Discussion Time: 3-5 PM
Mob: +91-8586968801

You might also like