SlideShare uma empresa Scribd logo
1 de 10
Application of Stack
Example of Stacks
INFIX, POSTFIX and PREFIX
Infix: A+B-C
Postfix: AB+C-
Prefix: -+ABC
Order of Precedence of Operators
Exponentiation
Multiplication/Division
Addition/Subtraction
Infix: ( (A+B)*C-(D-E) ) $ (F+G)
Conversion to Postfix Expression
( (AB+)*C-(DE-) ) $ (FG+)
( (AB+C*)-(DE-) ) $ (FG+)
(AB+C*DE--) $ (FG+)
AB+C*DE- -FG+$
Exercise: Convert the following to Postfix
( A + B ) * ( C – D)
A $ B * C – D + E / F / (G + H)
Conversion to Prefix Expression
The precedence rules for converting an expression from infix to
prefix are identical. The only change from postfix is that the
operator is placed before the operands rather than after them.
Evaluating a Postfix Expression
Each operator in a postfix string refers to the previous two
operands in the string.
Algorithm to Evaluate a Postfix Expression
opndstk = the empty stack
/* scan the input string reading one
element */
/* at a time into symb */
while (not end of input) {
symb = next input character;
if (symb is an operand)
push(opndstk, symb)
else {
/* symb is an operator */
opnd2 = pop(opndstk);
opnd1 = pop(opndstk);
value = result of applying
symb to opnd1 and opnd2;
push(opndstk, value);
} /* end else */
} /* end while */
return (pop(opndstk));
Example:
Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 +
symb opnd1 opnd2 value opndstk
6 6
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6 5 1 1
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
* 1 7 7 7
2 1 7 7 7,2
$ 7 2 49 49
3 7 2 49 49,3
+ 49 3 52 52
Conversion of Infix Expression to postfix
A+B*C = ABC*+
(A+B)*C = AB+C*
There must be a precedence function.
prcd(op1, op2), where op1 and op2 are characters representing
operators.
This function returns TRUE if op1 has precendence over op2 when op1
appears to the left of op2 in an infix expression without parenthesis.
prcd(op1,op2) returns FALSE otherwise.
For example prcd(‘*’,’+’) and prcd(‘+’,’+’) are TRUE whereas prcd(‘+’,’*’)
is FALSE.
prcd(‘$’,’$’) = FALSE
prcd( ‘(‘ , op) = FALSE for any operator op
prcd( op, ‘(‘ ) = FALSE for any operator op other than ‘)’
prcd( op, ‘)‘ ) = TRUE for any operator op other than ‘(‘
prcd( ‘)‘ ,op ) = undefined for any operator op (an error)
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) &&
prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix
string;
} /* end while */
push(opstk, symb);
} /* end else */
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Example-1: A+B*C
sym
b
Postfix
string
opstk
A A
+ A +
B AB +
* AB + *
C ABC + *
ABC* +
ABC*+
Algorithm to Convert Infix to Postfix
Example-2: (A+B)*C
symb Postfix
string
opstk
( (
A A (
+ A ( +
B AB ( +
) AB+
* AB+ *
C AB+C *
AB+C*
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) &&
prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix
string;
} /* end while */
push(opstk, symb);
} /* end else */
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) &&
prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix
string;
} /* end while */
push(opstk, symb);
} /* end else */
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Example-3: ( (A-(B+C) ) *D ) $ (E+F)
Algorithm to Convert Infix to Postfix
opstk = the empty stack;
while (not end of input) {
symb = next input character;
if (symb is an operand)
add symb to the postfix string
else {
while (!empty(opstk) &&
prcd(stacktop(opstk),symb) ) {
topsymb = pop(opstk);
add topsymb to the postfix
string;
} /* end while */
push(opstk, symb);
} /* end else */
} /* end while */
/* output any remaining operators */
while (!empty(opstk) ) {
topsymb = pop(opstk);
add topsymb to the postfix string;
} /* end while */
Example-3: ( (A-(B+C) ) *D ) $ (E+F)
symb Postfix string opstk
( (
( ((
A A ((
- A ((-
( A ((-(
B AB ((-(
+ AB ((-(+
C ABC ((-(+
) ABC+ ((-
) ABC+- (
* ABC+- (*
D ABC+-D (*
) ABC+-D*
$ ABC+-D* $
( ABC+-D* $(
E ABC+-D*E $(
+ ABC+-D*E $(+
F ABC+-D*EF $(+
) ABC+-D*EF+ $
ABC+-D*EF+$

Mais conteúdo relacionado

Mais procurados (20)

stack
stackstack
stack
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
stack & queue
stack & queuestack & queue
stack & queue
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Stack
StackStack
Stack
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
stack presentation
stack presentationstack presentation
stack presentation
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 

Destaque

Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queuesBuxoo Abdullah
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluationJeeSa Sultana
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stackHaqnawaz Ch
 
Stack Data Structre Introduction and source code
Stack Data Structre Introduction and source codeStack Data Structre Introduction and source code
Stack Data Structre Introduction and source codeSL Coder
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structureeShikshak
 
Notation
NotationNotation
Notationruhatch
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applicationsSaqib Saeed
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methodsPranavSB
 
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
All Pair Shortest Path Algorithm – Parallel Implementation and AnalysisAll Pair Shortest Path Algorithm – Parallel Implementation and Analysis
All Pair Shortest Path Algorithm – Parallel Implementation and AnalysisInderjeet Singh
 
Stack application
Stack applicationStack application
Stack applicationStudent
 

Destaque (20)

Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
 
Stack Data Structre Introduction and source code
Stack Data Structre Introduction and source codeStack Data Structre Introduction and source code
Stack Data Structre Introduction and source code
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Notation
NotationNotation
Notation
 
Stacks fundamentals
Stacks fundamentalsStacks fundamentals
Stacks fundamentals
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
String & its application
String & its applicationString & its application
String & its application
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
All Pair Shortest Path Algorithm – Parallel Implementation and AnalysisAll Pair Shortest Path Algorithm – Parallel Implementation and Analysis
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
 
Stack application
Stack applicationStack application
Stack application
 

Semelhante a Convert Infix Expressions to Postfix Notation

Semelhante a Convert Infix Expressions to Postfix Notation (20)

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
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
DS1.pptx
DS1.pptxDS1.pptx
DS1.pptx
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Circular queues
Circular queuesCircular queues
Circular queues
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data Structure
 
Data structures
Data structures Data structures
Data structures
 
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Computer notes - Postfix
Computer notes  - PostfixComputer notes  - Postfix
Computer notes - Postfix
 
Stack
StackStack
Stack
 
Lecture6
Lecture6Lecture6
Lecture6
 
Stack
StackStack
Stack
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
 
EXPRESSIONS.pptx
EXPRESSIONS.pptxEXPRESSIONS.pptx
EXPRESSIONS.pptx
 

Mais de Ain-ul-Moiz Khawaja (17)

Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Algo>Abstract data type
Algo>Abstract data typeAlgo>Abstract data type
Algo>Abstract data type
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Sorting algos > Data Structures & Algorithums
Sorting algos  > Data Structures & AlgorithumsSorting algos  > Data Structures & Algorithums
Sorting algos > Data Structures & Algorithums
 
Huffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsHuffman > Data Structures & Algorithums
Huffman > Data Structures & Algorithums
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
 
Data Structures & Algorithms
Data Structures & AlgorithmsData Structures & Algorithms
Data Structures & Algorithms
 
Turn over
Turn overTurn over
Turn over
 
Attribution Theories
Attribution TheoriesAttribution Theories
Attribution Theories
 
Attribution Theory
Attribution TheoryAttribution Theory
Attribution Theory
 
Absenteeism
AbsenteeismAbsenteeism
Absenteeism
 
HRM Employee Turnover
HRM Employee TurnoverHRM Employee Turnover
HRM Employee Turnover
 

Último

Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 

Último (20)

YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 

Convert Infix Expressions to Postfix Notation

  • 2. Example of Stacks INFIX, POSTFIX and PREFIX Infix: A+B-C Postfix: AB+C- Prefix: -+ABC Order of Precedence of Operators Exponentiation Multiplication/Division Addition/Subtraction
  • 3. Infix: ( (A+B)*C-(D-E) ) $ (F+G) Conversion to Postfix Expression ( (AB+)*C-(DE-) ) $ (FG+) ( (AB+C*)-(DE-) ) $ (FG+) (AB+C*DE--) $ (FG+) AB+C*DE- -FG+$ Exercise: Convert the following to Postfix ( A + B ) * ( C – D) A $ B * C – D + E / F / (G + H)
  • 4. Conversion to Prefix Expression The precedence rules for converting an expression from infix to prefix are identical. The only change from postfix is that the operator is placed before the operands rather than after them. Evaluating a Postfix Expression Each operator in a postfix string refers to the previous two operands in the string.
  • 5. Algorithm to Evaluate a Postfix Expression opndstk = the empty stack /* scan the input string reading one element */ /* at a time into symb */ while (not end of input) { symb = next input character; if (symb is an operand) push(opndstk, symb) else { /* symb is an operator */ opnd2 = pop(opndstk); opnd1 = pop(opndstk); value = result of applying symb to opnd1 and opnd2; push(opndstk, value); } /* end else */ } /* end while */ return (pop(opndstk)); Example: Postfix Expression: 6 2 3 + - 3 8 2 / + * 2 $ 3 + symb opnd1 opnd2 value opndstk 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2 $ 7 2 49 49 3 7 2 49 49,3 + 49 3 52 52
  • 6. Conversion of Infix Expression to postfix A+B*C = ABC*+ (A+B)*C = AB+C* There must be a precedence function. prcd(op1, op2), where op1 and op2 are characters representing operators. This function returns TRUE if op1 has precendence over op2 when op1 appears to the left of op2 in an infix expression without parenthesis. prcd(op1,op2) returns FALSE otherwise. For example prcd(‘*’,’+’) and prcd(‘+’,’+’) are TRUE whereas prcd(‘+’,’*’) is FALSE. prcd(‘$’,’$’) = FALSE prcd( ‘(‘ , op) = FALSE for any operator op prcd( op, ‘(‘ ) = FALSE for any operator op other than ‘)’ prcd( op, ‘)‘ ) = TRUE for any operator op other than ‘(‘ prcd( ‘)‘ ,op ) = undefined for any operator op (an error)
  • 7. Algorithm to Convert Infix to Postfix opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ push(opstk, symb); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ Example-1: A+B*C sym b Postfix string opstk A A + A + B AB + * AB + * C ABC + * ABC* + ABC*+
  • 8. Algorithm to Convert Infix to Postfix Example-2: (A+B)*C symb Postfix string opstk ( ( A A ( + A ( + B AB ( + ) AB+ * AB+ * C AB+C * AB+C* opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ push(opstk, symb); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */
  • 9. Algorithm to Convert Infix to Postfix opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ push(opstk, symb); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ Example-3: ( (A-(B+C) ) *D ) $ (E+F)
  • 10. Algorithm to Convert Infix to Postfix opstk = the empty stack; while (not end of input) { symb = next input character; if (symb is an operand) add symb to the postfix string else { while (!empty(opstk) && prcd(stacktop(opstk),symb) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ push(opstk, symb); } /* end else */ } /* end while */ /* output any remaining operators */ while (!empty(opstk) ) { topsymb = pop(opstk); add topsymb to the postfix string; } /* end while */ Example-3: ( (A-(B+C) ) *D ) $ (E+F) symb Postfix string opstk ( ( ( (( A A (( - A ((- ( A ((-( B AB ((-( + AB ((-(+ C ABC ((-(+ ) ABC+ ((- ) ABC+- ( * ABC+- (* D ABC+-D (* ) ABC+-D* $ ABC+-D* $ ( ABC+-D* $( E ABC+-D*E $( + ABC+-D*E $(+ F ABC+-D*EF $(+ ) ABC+-D*EF+ $ ABC+-D*EF+$