SlideShare a Scribd company logo
1 of 14
PRESENTATION
OF
DATA STRUCTURE & ALGORITHM
BY

Mohammad Saeed Farooqi
 INFIX Notation.
 POSTFIX Notation.
 PREFIX Notation.
 OPERATORS PRECEDENCE.
 Evaluating Of POSTFIX Expression Using Stacks.
 Converting INFIX To POSTFIX Using Stacks.
 ALGORITHMS.



OUTLINES

Mohammad Saeed Farooqi (University Of Swat, Pakistan)
 Infix Expression Are Those In Which Operators Are
Written Between Two Operands.
 Infix Expression Is The Common Arithmetic And
Logical Formula Expression.
 Infix Expression Needs Extra Information To Make The
Order Of Evaluation Of The Operators Clear.
 Rules Built Into The Language About Operator
Precedence And Associativity, And Brackets ( ) To Allow
Users To Override These Rules.

 INFIX EXPRESSION
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
 EXAMPLE…
 A * ( B + C ) / D…
 This Expression Is Usually Taken To Mean Something
Like: "First Add B And C Together, Then Multiply The
Result By A, Then Divide By D To Give The Final
Answer.

 INFIX EXPRESSION
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
 POSTFIX Expression Also Known As “Reverse Polish
Expression”
 In These Expressions Operators Are Written After Their
Operands.
 Operators Act On Values Immediately To The Left Of
Them.
 They Do Not Have (Brackets)

 POSTFIX EXPRESSION
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
 EXAMPLE…
 A B C + * D /…
 Operators Act On Values Immediately To The Left Of
Them.
 For Example, The "+" Above Uses The "B" And "C".
 We Can Add (Totally Unnecessary) Brackets To Make
This Easy.
( (A (B C +) *) D /) .

 POSTFIX EXPRESSION
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
 Also Known As “Polish Expression”.
 In These Expressions Operators Are Written Before The
Operands.
 We Have To Scan It From Right-To-Left.
 As For Postfix, Operators Are Evaluated Left-To-Right And
Brackets Are Superfluous.
 Operators Act On The Two Nearest Values On The Right.
 I Have Again Added (Totally Unnecessary) Brackets To Make
This Clear.
(/ (* A (+ B C) ) D)
 PREFIX Notation Is Especially Popular With StackBased Operations Due To Its Innate Ability To Easily
Distinguish Order Of Operations Without The Need For
Parentheses.

 PREFIX EXPRESSION
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
 We Assume The Following Three Levels Of
PRECEDENCE For The Usual Five Binary Operations.
 Highest
Exponentiation ().
 Next Highest Multiplication (*) And Division (/)
Superfluous
 Lowest
Addition (+) And Subtraction (-)
 Without Understanding Operator Precedence You Can
Not Convert Any Expression.

 OPERATOR PRECEDENCE
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
 EVOLUACTION OF POSTFIX EXPRESSION
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
• EvaluatePostfix(exp)
• {
• Create A Stack S.
• for I ← 0 to length of the (exp)-1
{
if(exp[i]) // Is An Operand
push(exp[i]);
else if(exp[i])
// Is An Operator
{
op1←pop();
op2← pop();
result←perform(exp[i],op1,op2);
push(result);
}
}
}

 ALGORITHM FOR POSTFIX EVALUATION
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
Converting Infix To Postfix
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
•
•
•
•
•
•
•

Example: 1*2+3.
Scan: *,+ (Stack) And 12 (Postfix Expression).
Precedence Of * Is Higher Than +.
Thus, Append * to postfix Expression.
+ (Stack) And 12* (Postfix Expression ).
Scan Further, + (Stack ) and 12*3 (Postfix Expression)
There Are No Further Operators, Thus, Postfix Is 12*3+

Converting Infix To Postfix
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
Converting Infix To Prefix
Mohammad Saeed Farooqi (University Of Swat, Pakistan)
• Step 1. Push “)” onto STACK, and add “(“ to end of the A.
• Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A
until the STACK is empty.
• Step 3. If an operand is encountered add it to B.
• Step 4. If a right parenthesis is encountered push it onto STACK.
• Step 5. If an operator is encountered then: a. Repeatedly pop from STACK
and add to B each operator (on the top of STACK) which has same or higher
precedence than the operator. b. Add operator to STACK.
• Step 6. If left parenthesis is encontered then a. Repeatedly pop from the
STACK and add to B (each operator on top of stack until a left parenthesis is
encounterd) b. Remove the left parenthesis.
• Step 7. Exit

Converting Infix To Postfix
Mohammad Saeed Farooqi (University Of Swat, Pakistan)

More Related Content

What's hot

Stack and queue -polish notations
Stack and queue -polish notationsStack and queue -polish notations
Stack and queue -polish notationsjyoti_lakhani
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsAfaq Mansoor Khan
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluationJeeSa Sultana
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stacksahil kumar
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 

What's hot (16)

Stack and queue -polish notations
Stack and queue -polish notationsStack and queue -polish notations
Stack and queue -polish notations
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Mycasestudy
MycasestudyMycasestudy
Mycasestudy
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
 
Stack application
Stack applicationStack application
Stack application
 
Polish
PolishPolish
Polish
 
Assignment6
Assignment6Assignment6
Assignment6
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
DATA STRUCTURE - STACK
DATA STRUCTURE - STACKDATA STRUCTURE - STACK
DATA STRUCTURE - STACK
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 

Similar to Infix to postfix

Similar to Infix to postfix (20)

Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
 
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
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptx
 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
 
Lecture6
Lecture6Lecture6
Lecture6
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsCSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & Expressions
 
Stack Algorithm
Stack AlgorithmStack Algorithm
Stack Algorithm
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
stacks1
stacks1stacks1
stacks1
 
Team 4
Team 4Team 4
Team 4
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
175035 cse lab-05
175035 cse lab-05 175035 cse lab-05
175035 cse lab-05
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
 

Recently uploaded

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

Infix to postfix

  • 1. PRESENTATION OF DATA STRUCTURE & ALGORITHM BY Mohammad Saeed Farooqi
  • 2.  INFIX Notation.  POSTFIX Notation.  PREFIX Notation.  OPERATORS PRECEDENCE.  Evaluating Of POSTFIX Expression Using Stacks.  Converting INFIX To POSTFIX Using Stacks.  ALGORITHMS.  OUTLINES Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 3.  Infix Expression Are Those In Which Operators Are Written Between Two Operands.  Infix Expression Is The Common Arithmetic And Logical Formula Expression.  Infix Expression Needs Extra Information To Make The Order Of Evaluation Of The Operators Clear.  Rules Built Into The Language About Operator Precedence And Associativity, And Brackets ( ) To Allow Users To Override These Rules.  INFIX EXPRESSION Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 4.  EXAMPLE…  A * ( B + C ) / D…  This Expression Is Usually Taken To Mean Something Like: "First Add B And C Together, Then Multiply The Result By A, Then Divide By D To Give The Final Answer.  INFIX EXPRESSION Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 5.  POSTFIX Expression Also Known As “Reverse Polish Expression”  In These Expressions Operators Are Written After Their Operands.  Operators Act On Values Immediately To The Left Of Them.  They Do Not Have (Brackets)  POSTFIX EXPRESSION Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 6.  EXAMPLE…  A B C + * D /…  Operators Act On Values Immediately To The Left Of Them.  For Example, The "+" Above Uses The "B" And "C".  We Can Add (Totally Unnecessary) Brackets To Make This Easy. ( (A (B C +) *) D /) .  POSTFIX EXPRESSION Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 7.  Also Known As “Polish Expression”.  In These Expressions Operators Are Written Before The Operands.  We Have To Scan It From Right-To-Left.  As For Postfix, Operators Are Evaluated Left-To-Right And Brackets Are Superfluous.  Operators Act On The Two Nearest Values On The Right.  I Have Again Added (Totally Unnecessary) Brackets To Make This Clear. (/ (* A (+ B C) ) D)  PREFIX Notation Is Especially Popular With StackBased Operations Due To Its Innate Ability To Easily Distinguish Order Of Operations Without The Need For Parentheses.  PREFIX EXPRESSION Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 8.  We Assume The Following Three Levels Of PRECEDENCE For The Usual Five Binary Operations.  Highest Exponentiation ().  Next Highest Multiplication (*) And Division (/) Superfluous  Lowest Addition (+) And Subtraction (-)  Without Understanding Operator Precedence You Can Not Convert Any Expression.  OPERATOR PRECEDENCE Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 9.  EVOLUACTION OF POSTFIX EXPRESSION Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 10. • EvaluatePostfix(exp) • { • Create A Stack S. • for I ← 0 to length of the (exp)-1 { if(exp[i]) // Is An Operand push(exp[i]); else if(exp[i]) // Is An Operator { op1←pop(); op2← pop(); result←perform(exp[i],op1,op2); push(result); } } }  ALGORITHM FOR POSTFIX EVALUATION Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 11. Converting Infix To Postfix Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 12. • • • • • • • Example: 1*2+3. Scan: *,+ (Stack) And 12 (Postfix Expression). Precedence Of * Is Higher Than +. Thus, Append * to postfix Expression. + (Stack) And 12* (Postfix Expression ). Scan Further, + (Stack ) and 12*3 (Postfix Expression) There Are No Further Operators, Thus, Postfix Is 12*3+ Converting Infix To Postfix Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 13. Converting Infix To Prefix Mohammad Saeed Farooqi (University Of Swat, Pakistan)
  • 14. • Step 1. Push “)” onto STACK, and add “(“ to end of the A. • Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty. • Step 3. If an operand is encountered add it to B. • Step 4. If a right parenthesis is encountered push it onto STACK. • Step 5. If an operator is encountered then: a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or higher precedence than the operator. b. Add operator to STACK. • Step 6. If left parenthesis is encontered then a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd) b. Remove the left parenthesis. • Step 7. Exit Converting Infix To Postfix Mohammad Saeed Farooqi (University Of Swat, Pakistan)