SlideShare a Scribd company logo
DATA STRUCTURES
Applications of Stacks
Dr. P. Subathra
subathrakishore@yahoo.com
Professor
Dept. of Information Technology
KAMARAJ College of Engineering & Technology
(AUTONOMOUS)
Madurai
Tamil Nadu
India
CS8391 – DATA STRUCTURES
ONLINE CLASSES – CLASS NO. 14
25.09.2020
(10:00 AM – 12:00 PM)
Applications of Stacks
Prefix, Postfix, Infix Notation
Infix Notation
• To add A, B, we write
A+B
• To multiply A, B, we write
A*B
• The operators ('+' and '*') go in between the
operands ('A' and 'B')
• This is "Infix" notation.
Prefix Notation
• Instead of saying "A plus B", we could say "add
A,B " and write
+ A B
• "Multiply A,B" would be written
* A B
• This is Prefix notation.
Postfix Notation
• Another alternative is to put the operators
after the operands as in
A B +
and
A B *
• This is Postfix notation.
• The terms infix, prefix, and postfix tell us
whether the operators go between, before, or
after the operands.
Pre A In B Post
Parentheses
• Evaluate 2+3*5.
• + First:
(2+3)*5 = 5*5 = 25
• * First:
2+(3*5) = 2+15 = 17
• Infix notation requires Parentheses.
What about Prefix Notation?
• + 2 * 3 5 =
= + 2 * 3 5
= + 2 15 = 17
• * + 2 3 5 =
= * + 2 3 5
= * 5 5 = 25
• No parentheses needed!
Postfix Notation
• 2 3 5 * + =
= 2 3 5 * +
= 2 15 + = 17
• 2 3 + 5 * =
= 2 3 + 5 *
= 5 5 * = 25
• No parentheses needed here either!
Conclusion:
• Infix is the only notation that requires
parentheses in order to change the order in
which the operations are done.
CS314 Stacks 13
Mathematical Calculations
• What does 3 + 2 * 4 equal?
2 * 4 + 3? 3 * 2 + 4?
• The precedence of operators affects the order of
operations.
• A mathematical expression cannot simply be evaluated left
to right.
• A challenge when evaluating a program.
• Lexical analysis is the process of interpreting a program.
What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3
CONVERSION OF
INFIX
TO
PREFIX AND POSTFIX
EXPRESSIONS
Fully Parenthesized Expression
• A FPE has exactly one set of Parentheses
enclosing each operator and its operands.
• Which is fully parenthesized?
( A + B ) * C
( ( A + B) * C )
( ( A + B) * ( C ) )
Infix to Prefix Conversion
Move each operator to the left of its operands &
remove the parentheses:
( ( A + B) * ( C + D ) )
Infix to Prefix Conversion
Move each operator to the left of its operands &
remove the parentheses:
( + A B) * ( C + D ) )
Infix to Prefix Conversion
Move each operator to the left of its operands &
remove the parentheses:
( + AB) * ( C + D ) )
Infix to Prefix Conversion
Move each operator to the left of its operands &
remove the parentheses:
( + AB) * +CD ) )
Infix to Prefix Conversion
Move each operator to the left of its operands &
remove the parentheses:
( + AB) * + CD ) )
Infix to Prefix Conversion
Move each operator to the left of its operands &
remove the parentheses:
* + AB) + CD ) )
Infix to Prefix Conversion
Move each operator to the left of its operands &
remove the parentheses:
* + AB) + CD ) )
* + AB)+ CD ) )
Infix to Prefix Conversion
Now Remove all the Closing Parenthesis:
* + AB)+ CD ) )
* + AB+ CD
Infix to Prefix Conversion
Move each operator to the left of its operands &
remove the parentheses:
* + A B + C D
Order of operands does not change!
Infix to Postfix
( ( ( A + B ) * C ) - ( ( D + E ) / F ) )
Infix to Postfix
( ( ( A + B ) * C ) - ( ( D + E ) / F ) )
( ( ( AB + * C ) - ( ( D E + / F ) )
Infix to Postfix
( ( ( AB + * C ) - ( ( D E + / F ) )
( ( ( AB + C * - ( ( D E + / F ) )
( ( ( AB + C * - ( ( D E + F / )
( ( ( AB +C * - ( ( D E + F / )
Infix to Postfix
( ( ( AB +C * - ( ( D E + F / )
( ( ( AB +C * - ( ( D E + F / )
( ( ( AB +C * ( ( D E + F / -
( ( ( AB +C * ( ( D E + F / -
Infix to Postfix
( ( ( AB +C * ( ( D E + F / -
( ( ( AB +C * ( ( D E + F / -
Remove all open parenthesis
AB +C * D E + F / -
Postfix Expression: AB +C * D E + F / -
• Operand order does not change!
• Operators are in order of evaluation
Infix to Postfix
• Initialize a Stack for operators, output list
• Split the input into a list of tokens.
• for each token (left to right):
if it is operand: append to output
if it is '(': push onto Stack
if it is ')': pop & append till '('
if it in '+-*/':
while peek has precedence ≥ it:
pop & append
push onto Stack
pop and append the rest of the Stack.
Operators Precedence & Associativity Table
Operator Precedence
InfixVect
postfixVect
( a + b - c ) * d – ( e + f )
Infix to postfix conversion
infixVect
postfixVect
a + b - c ) * d – ( e + f )
(
stackVect
Infix to postfix conversion
infixVect
postfixVect
+ b - c ) * d – ( e + f )
(
a
Infix to postfix conversionstackVect
infixVect
postfixVect
b - c ) * d – ( e + f )
(
a
+
Infix to postfix conversionstackVect
infixVect
postfixVect
- c ) * d – ( e + f )
(
a b
+
Infix to postfix conversionstackVect
infixVect
postfixVect
c ) * d – ( e + f )
(
a b +
-
Infix to postfix conversionstackVect
infixVect
postfixVect
) * d – ( e + f )
(
a b + c
-
Infix to postfix conversionstackVect
infixVect
postfixVect
* d – ( e + f )
a b + c -
Infix to postfix conversionstackVect
infixVect
postfixVect
d – ( e + f )
a b + c -
*
Infix to postfix conversionstackVect
infixVect
postfixVect
– ( e + f )
a b + c - d
*
Infix to postfix conversionstackVect
infixVect
postfixVect
( e + f )
a b + c – d *
-
Infix to postfix conversionstackVect
infixVect
postfixVect
e + f )
a b + c – d *
-
(
Infix to postfix conversionstackVect
infixVect
postfixVect
+ f )
a b + c – d * e
-
(
Infix to postfix conversionstackVect
infixVect
postfixVect
f )
a b + c – d * e
-
(
+
Infix to postfix conversionstackVect
infixVect
postfixVect
)
a b + c – d * e f
-
(
+
Infix to postfix conversionstackVect
infixVect
postfixVect
a b + c – d * e f +
-
Infix to postfix conversionstackVect
infixVect
postfixVect
a b + c – d * e f + -
Infix to postfix conversionstackVect
Infix to Postfix Expression
https://www.youtube.com/watch?v=OVFwgYrM
Shw
Infix to Prefix Expression
• https://www.youtube.com/watch?v=sJ0VhIbv
Ctc
EVALUATION OF POSTFIX EXPRESSION
SIMPLE POSTFIX EXPRESSION EVALUATION
Evaluation Rule of a Postfix
Expression States
• While reading the expression from left to
right, push the element in the stack if it is an
operand.
• Pop the two operands from the stack, if the
element is an operator and then evaluate it.
• Push back the result of the evaluation.
• Repeat it till the end of the expression.
• Finally the result is in the Stack
Example : Evaluate the Postfix
Expression
• 456*+
Evaluation of Postfix Expression
https://www.youtube.com/watch?v=uh7fD8WiT
28

More Related Content

What's hot

What's hot (20)

My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Circular queue
Circular queueCircular queue
Circular queue
 
yieldとreturnの話
yieldとreturnの話yieldとreturnの話
yieldとreturnの話
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzers
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
Queues
QueuesQueues
Queues
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Python Operators
Python OperatorsPython Operators
Python Operators
 
Infix postfixcoversion
Infix postfixcoversionInfix postfixcoversion
Infix postfixcoversion
 
Recursion
RecursionRecursion
Recursion
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 
Stacks
StacksStacks
Stacks
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 

Similar to 2.2 stack applications Infix to Postfix & Evaluation of Post Fix

week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxssusere3b1a2
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluationJeeSa Sultana
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdfsoniasharmafdp
 
DS1.pptx
DS1.pptxDS1.pptx
DS1.pptxMomin24
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6ecomputernotes
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expressionDrkhanchanaR
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Ahmed Khateeb
 
EXPRESSIONS.pptx
EXPRESSIONS.pptxEXPRESSIONS.pptx
EXPRESSIONS.pptxSachin013
 
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptxmrizwan38
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptxJAYAPRIYAR7
 
Conversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad balochConversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad balochSarmad Baloch
 
Please need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdfPlease need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdfpristiegee
 
Postfix_to_prefix_conversion_stack Application.ppt
Postfix_to_prefix_conversion_stack Application.pptPostfix_to_prefix_conversion_stack Application.ppt
Postfix_to_prefix_conversion_stack Application.pptShubhadaMone1
 
Postfix_to_prefix conversion-stack application.ppt
Postfix_to_prefix conversion-stack application.pptPostfix_to_prefix conversion-stack application.ppt
Postfix_to_prefix conversion-stack application.pptShubhadaMone1
 

Similar to 2.2 stack applications Infix to Postfix & Evaluation of Post Fix (20)

week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
 
DS1.pptx
DS1.pptxDS1.pptx
DS1.pptx
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Stack application
Stack applicationStack application
Stack application
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Lecture6
Lecture6Lecture6
Lecture6
 
EXPRESSIONS.pptx
EXPRESSIONS.pptxEXPRESSIONS.pptx
EXPRESSIONS.pptx
 
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptx
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
Conversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad balochConversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad baloch
 
Team 4
Team 4Team 4
Team 4
 
Please need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdfPlease need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdf
 
Postfix_to_prefix_conversion_stack Application.ppt
Postfix_to_prefix_conversion_stack Application.pptPostfix_to_prefix_conversion_stack Application.ppt
Postfix_to_prefix_conversion_stack Application.ppt
 
Postfix_to_prefix conversion-stack application.ppt
Postfix_to_prefix conversion-stack application.pptPostfix_to_prefix conversion-stack application.ppt
Postfix_to_prefix conversion-stack application.ppt
 

More from P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai

More from P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai (20)

TestFile
TestFileTestFile
TestFile
 
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
1. 6 doubly linked list
1. 6 doubly linked list1. 6 doubly linked list
1. 6 doubly linked list
 
1. 5 Circular singly linked list
1. 5 Circular singly linked list1. 5 Circular singly linked list
1. 5 Circular singly linked list
 
1. 4 Singly linked list deletion
1. 4 Singly linked list deletion1. 4 Singly linked list deletion
1. 4 Singly linked list deletion
 
1. 3 singly linked list insertion 2
1. 3 singly linked list   insertion 21. 3 singly linked list   insertion 2
1. 3 singly linked list insertion 2
 
1. 2 Singly Linked List
1. 2 Singly Linked List1. 2 Singly Linked List
1. 2 Singly Linked List
 
1. C Basics for Data Structures Bridge Course
1. C Basics for Data Structures   Bridge Course1. C Basics for Data Structures   Bridge Course
1. C Basics for Data Structures Bridge Course
 
Approximation Algorithms TSP
Approximation Algorithms   TSPApproximation Algorithms   TSP
Approximation Algorithms TSP
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
The stable marriage problem iterative improvement method
The stable marriage problem iterative improvement methodThe stable marriage problem iterative improvement method
The stable marriage problem iterative improvement method
 
Maximum matching in bipartite graphs iterative improvement method
Maximum matching in bipartite graphs   iterative improvement methodMaximum matching in bipartite graphs   iterative improvement method
Maximum matching in bipartite graphs iterative improvement method
 
Knapsack dynamic programming formula top down (1)
Knapsack dynamic programming formula top down (1)Knapsack dynamic programming formula top down (1)
Knapsack dynamic programming formula top down (1)
 
Knapsack dynamic programming formula bottom up
Knapsack dynamic programming formula bottom upKnapsack dynamic programming formula bottom up
Knapsack dynamic programming formula bottom up
 
Huffman tree coding greedy approach
Huffman tree coding  greedy approachHuffman tree coding  greedy approach
Huffman tree coding greedy approach
 
Simplex method
Simplex methodSimplex method
Simplex method
 
Simplex method
Simplex methodSimplex method
Simplex method
 
Multiplication of integers & strassens matrix multiplication subi notes
Multiplication of integers & strassens matrix multiplication   subi notesMultiplication of integers & strassens matrix multiplication   subi notes
Multiplication of integers & strassens matrix multiplication subi notes
 
Multiplication of large integers problem subi notes
Multiplication of large integers  problem  subi notesMultiplication of large integers  problem  subi notes
Multiplication of large integers problem subi notes
 

Recently uploaded

NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...Amil baba
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfAbrahamGadissa
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerapareshmondalnita
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationDr. Radhey Shyam
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageRCC Institute of Information Technology
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGKOUSTAV SARKAR
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdfKamal Acharya
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf884710SadaqatAli
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.PrashantGoswami42
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientistgettygaming1
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxCenterEnamel
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-IVigneshvaranMech
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfKamal Acharya
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdfKamal Acharya
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdfKamal Acharya
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdfAhmedHussein950959
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdfKamal Acharya
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdfKamal Acharya
 

Recently uploaded (20)

NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 

2.2 stack applications Infix to Postfix & Evaluation of Post Fix

  • 1. DATA STRUCTURES Applications of Stacks Dr. P. Subathra subathrakishore@yahoo.com Professor Dept. of Information Technology KAMARAJ College of Engineering & Technology (AUTONOMOUS) Madurai Tamil Nadu India
  • 2. CS8391 – DATA STRUCTURES ONLINE CLASSES – CLASS NO. 14 25.09.2020 (10:00 AM – 12:00 PM)
  • 5. Infix Notation • To add A, B, we write A+B • To multiply A, B, we write A*B • The operators ('+' and '*') go in between the operands ('A' and 'B') • This is "Infix" notation.
  • 6. Prefix Notation • Instead of saying "A plus B", we could say "add A,B " and write + A B • "Multiply A,B" would be written * A B • This is Prefix notation.
  • 7. Postfix Notation • Another alternative is to put the operators after the operands as in A B + and A B * • This is Postfix notation.
  • 8. • The terms infix, prefix, and postfix tell us whether the operators go between, before, or after the operands. Pre A In B Post
  • 9. Parentheses • Evaluate 2+3*5. • + First: (2+3)*5 = 5*5 = 25 • * First: 2+(3*5) = 2+15 = 17 • Infix notation requires Parentheses.
  • 10. What about Prefix Notation? • + 2 * 3 5 = = + 2 * 3 5 = + 2 15 = 17 • * + 2 3 5 = = * + 2 3 5 = * 5 5 = 25 • No parentheses needed!
  • 11. Postfix Notation • 2 3 5 * + = = 2 3 5 * + = 2 15 + = 17 • 2 3 + 5 * = = 2 3 + 5 * = 5 5 * = 25 • No parentheses needed here either!
  • 12. Conclusion: • Infix is the only notation that requires parentheses in order to change the order in which the operations are done.
  • 13. CS314 Stacks 13 Mathematical Calculations • What does 3 + 2 * 4 equal? 2 * 4 + 3? 3 * 2 + 4? • The precedence of operators affects the order of operations. • A mathematical expression cannot simply be evaluated left to right. • A challenge when evaluating a program. • Lexical analysis is the process of interpreting a program. What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 3
  • 14. CONVERSION OF INFIX TO PREFIX AND POSTFIX EXPRESSIONS
  • 15. Fully Parenthesized Expression • A FPE has exactly one set of Parentheses enclosing each operator and its operands. • Which is fully parenthesized? ( A + B ) * C ( ( A + B) * C ) ( ( A + B) * ( C ) )
  • 16. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( ( A + B) * ( C + D ) )
  • 17. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( + A B) * ( C + D ) )
  • 18. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( + AB) * ( C + D ) )
  • 19. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( + AB) * +CD ) )
  • 20. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( + AB) * + CD ) )
  • 21. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: * + AB) + CD ) )
  • 22. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: * + AB) + CD ) ) * + AB)+ CD ) )
  • 23. Infix to Prefix Conversion Now Remove all the Closing Parenthesis: * + AB)+ CD ) ) * + AB+ CD
  • 24. Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: * + A B + C D Order of operands does not change!
  • 25. Infix to Postfix ( ( ( A + B ) * C ) - ( ( D + E ) / F ) )
  • 26. Infix to Postfix ( ( ( A + B ) * C ) - ( ( D + E ) / F ) ) ( ( ( AB + * C ) - ( ( D E + / F ) )
  • 27. Infix to Postfix ( ( ( AB + * C ) - ( ( D E + / F ) ) ( ( ( AB + C * - ( ( D E + / F ) ) ( ( ( AB + C * - ( ( D E + F / ) ( ( ( AB +C * - ( ( D E + F / )
  • 28. Infix to Postfix ( ( ( AB +C * - ( ( D E + F / ) ( ( ( AB +C * - ( ( D E + F / ) ( ( ( AB +C * ( ( D E + F / - ( ( ( AB +C * ( ( D E + F / -
  • 29. Infix to Postfix ( ( ( AB +C * ( ( D E + F / - ( ( ( AB +C * ( ( D E + F / - Remove all open parenthesis AB +C * D E + F / - Postfix Expression: AB +C * D E + F / - • Operand order does not change! • Operators are in order of evaluation
  • 30. Infix to Postfix • Initialize a Stack for operators, output list • Split the input into a list of tokens. • for each token (left to right): if it is operand: append to output if it is '(': push onto Stack if it is ')': pop & append till '(' if it in '+-*/': while peek has precedence ≥ it: pop & append push onto Stack pop and append the rest of the Stack.
  • 31. Operators Precedence & Associativity Table Operator Precedence
  • 32. InfixVect postfixVect ( a + b - c ) * d – ( e + f ) Infix to postfix conversion
  • 33. infixVect postfixVect a + b - c ) * d – ( e + f ) ( stackVect Infix to postfix conversion
  • 34. infixVect postfixVect + b - c ) * d – ( e + f ) ( a Infix to postfix conversionstackVect
  • 35. infixVect postfixVect b - c ) * d – ( e + f ) ( a + Infix to postfix conversionstackVect
  • 36. infixVect postfixVect - c ) * d – ( e + f ) ( a b + Infix to postfix conversionstackVect
  • 37. infixVect postfixVect c ) * d – ( e + f ) ( a b + - Infix to postfix conversionstackVect
  • 38. infixVect postfixVect ) * d – ( e + f ) ( a b + c - Infix to postfix conversionstackVect
  • 39. infixVect postfixVect * d – ( e + f ) a b + c - Infix to postfix conversionstackVect
  • 40. infixVect postfixVect d – ( e + f ) a b + c - * Infix to postfix conversionstackVect
  • 41. infixVect postfixVect – ( e + f ) a b + c - d * Infix to postfix conversionstackVect
  • 42. infixVect postfixVect ( e + f ) a b + c – d * - Infix to postfix conversionstackVect
  • 43. infixVect postfixVect e + f ) a b + c – d * - ( Infix to postfix conversionstackVect
  • 44. infixVect postfixVect + f ) a b + c – d * e - ( Infix to postfix conversionstackVect
  • 45. infixVect postfixVect f ) a b + c – d * e - ( + Infix to postfix conversionstackVect
  • 46. infixVect postfixVect ) a b + c – d * e f - ( + Infix to postfix conversionstackVect
  • 47. infixVect postfixVect a b + c – d * e f + - Infix to postfix conversionstackVect
  • 48. infixVect postfixVect a b + c – d * e f + - Infix to postfix conversionstackVect
  • 49. Infix to Postfix Expression https://www.youtube.com/watch?v=OVFwgYrM Shw
  • 50. Infix to Prefix Expression • https://www.youtube.com/watch?v=sJ0VhIbv Ctc
  • 51. EVALUATION OF POSTFIX EXPRESSION SIMPLE POSTFIX EXPRESSION EVALUATION
  • 52. Evaluation Rule of a Postfix Expression States • While reading the expression from left to right, push the element in the stack if it is an operand. • Pop the two operands from the stack, if the element is an operator and then evaluate it. • Push back the result of the evaluation. • Repeat it till the end of the expression. • Finally the result is in the Stack
  • 53. Example : Evaluate the Postfix Expression • 456*+
  • 54. Evaluation of Postfix Expression https://www.youtube.com/watch?v=uh7fD8WiT 28