Data Structures Unit 1-Linear Structures
Data Structures Unit 1-Linear Structures
DATA STRUCTURES
Unit 1-Linear structures
Year/Sem : II/III
Subject Code: 1151CS102
Topic : Applications of stack
Faculty Name : Mrs.Vijitha.S
Date : 26.08.2020
School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
Application of stack
Application of stack:
5 * 8 - 12 / 4
07/09/2021 Department of Computer Science and Engineering 3
Application of stack
Expression Evaluation:
5 * 8 = 40 and 12 / 4 = 3
Infix expressions:
Example: X+Y
Prefix expressions:
Example: +XY
and Project
Management
(SEPM)
Postfix expressions:
Example: XY+
LOW LOW
+ - -----lowest
priority
HIGH HIGH
LOW HIGH
and Project
Management
(SEPM)
C +(* ABC
Scan ‘A’
Scan ‘+’ - +(- ABC*
Scan ‘(’ ( +(-( ABC*
Scan ‘B’ D +(-( ABC*D
Scan ‘*’ / +(-(/ ABC*D
Scan ‘C’ E +(-(/ ABC*DE
Scan ‘-’ and Project
^ +(-(/^ ABC*DE
Management
Scan ‘(’ (SEPM) F +(-(/^ ABC*DEF
Scan ‘D’
) +(-(/^) ABC*DEF
Scan ‘/’
26.06.2020 Department of Computer Science and Engineering 19
Infix to postfix
Input Stack Output
consider the infix expression
A+(B*C-(D/E^F)*G)*H ) +(- ABC*DEF^/
* +(-* ABC*DEF^/
Scan ‘E’ G +(-* ABC*DEF^/G
Scan ‘^’ ) +(-*) ABC*DEF^/G
Scan ‘F’ ) + ABC*DEF^/G*-
Scan ‘)’ * +* ABC*DEF^/G*-
Scan ‘*’ H +* ABC*DEF^/G*-H
Scan ‘G’ END ABC*DEF^/G*-H*+
Scan ‘)’
Scan ‘*’
Scan ‘H’ and Project
Management
(SEPM)
Postfix expression is
ABC*DEF^/G*-H*+
26.06.2020 Department of Computer Science and Engineering 20
EVALUATING POSTFIX EXPRESSION
• Declare a character stack S.
• Now traverse the expression string exp.
If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘)
then push it to stack.
If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then
pop from stack and if the popped character is the matching starting
bracket then fine else parenthesis are not balanced.
• After complete traversal, if there is some starting bracket left in
stack then “not balanced”
Balancing Parenthesis
Department of Computer Science and Engineering 22
BALANCING PARENTHESIS
{} {(}
({[]}) ([(()])
{[]()} {}[])
[{({}[]({ [{)}(]}]
})}]
Department of Computer Science and Engineering 23
Thank You