Professional Documents
Culture Documents
S2 B.Tech CSE(AI)
Week 3 Lecture 1 STACK ADT
Overview
• STACK ADT. And it’s operations. Implementation completed.
• Applications discussion ongoing .
• Undo operation.
• Recursive calls
3. Checking balancing parenthesis
• Given an expression we need to find whether the parenthesis in the
expression are balanced or not .
Examples: (A+B)
(A+B)*(C+D)
((X+y)+(a+b) .
Not Balanced.
• Example 2.
Balanced.
Algorithm
Not Balanced.
Another application of STACK
• Evaluation of Arithmetic expressions.
• Hence to solve the Infix Expression compiler will scan the expression
multiple times to solve the sub-expressions in expressions orderly
which is very in-efficient.
• Example: a+b*c+d
Postfix Notation .
• <Operand > <Operand > <Operator>
Examples
Postfix is good? Why?
• While evaluating the expression order of evaluation is not disrupted?
Prefix notation:
Postfix notation
Algorithm
Time complexity= O(n), n is the
expression length .
Stack Based Solution
• Q– Infix Expression
• P- Post fix expression
• S- Character Stack – Temporarily stores parenthesis and operators.
• Initial step: Push ‘(’ on to stack and ‘)’ onto Q, Infix expression.
• Operand------ Add it to P
• Operator ----Perform some operations . Add the scanned operator to
Stack
• (------- push on to stack
• ) ------- pop from the stack Until a ( is found in the stack.
Example
• Step 1:
Example 2:
Example :
• Step 1:
Example: Find the answers
1. (A-B)*(D/E)
2. 2.
TO Do List
• Revise the applications.
• Next week a MCQ Quiz will be there on stack. Details we will discuss
on Friday
• Attend the lab .( 1- 33) Join in channel 1 with Ms Lekshmi Ma’am
• (34- 76) ) Join in channel 2 with me.
• Lab from 2:00 3:30 PM be in the meeting. After that leave the
channel be online till 5:00 PM. Upload the class work and leave.