際際滷

際際滷Share a Scribd company logo
Applications of Stack
The following Applications of stack are:
1. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
2. Stack is used to evaluate a postfix expression.
3. Stack is used to convert an infix expression into postfix/prefix form.
4. In recursion, all intermediate arguments and return values are stored on the processors stack.
5. During a function call the return address and arguments are pushed onto a stack and on return they
are popped off.
Expression evaluation and conversion
 The most frequent application of stacks is in the evaluation of arithmetic
expressions. An arithmetic expression is made of operands, operators.
 Operand is the quantity on which a mathematical operation is performed. Operand
may be a variable like x, y, z or a constant like 5, 4, 6 etc. Operator is a symbol
which signifies a mathematical or logical operation between the operands.
Examples of familiar operators include +, -, *, /, ^ etc.
 When high-level programming languages came into existence, one of the major
difficulties faced by computer scientists was to generate machine language
instructions that could properly evaluate any arithmetic expression. A complex
assignment statement such as
Might have several meanings, and even if the meanings were uniquely defied, it is
still difficult to generate a correct and reasonable instruction sequence. The fist
problem in understanding the meaning of an expression is to decide the order in
which the operations are to be carried out. This demands that every language must
uniquely define such an order.
Another example:
To calculate this expression for values 4, 3, 7 for A, B, C respectively we must
follow certain in order to have the right result :
 The answer is not correct; multiplication is to be done before the addition, because
multiplication has higher precedence over addition. This means that an expression
is calculated according to the operators precedence not the order as they look like.
 Thus expression A + B * C can be interpreted as A + (B * C). Using this
alternative method we can convey to the computer that multiplication has higher
precedence over addition.
 To fix the order of evaluation, assign each operator a priority, and evaluated from
left to right.
 Let us consider the expression
 By using priorities rules, the expression X is rewritten as
 We manually evaluate the innermost expression first, followed by the next
parenthesized inner expression, which produces the result.
 Another application of stack is calculation of postfix expression. There are
basically three types of notation for an expression (mathematical expression; An
expression is defined as the number of operands or data items combined with
several operators.)
1. Infix notation
2. Prefix notation
3. Postfix notation
 Infix
The infix notation is what we come across in our general mathematics, where the
operator is written in-between the operands. For example: The expression to add
two numbers A and B is written in infix notation as:
 Prefix
The prefix notation is a notation in which the operator(s) is written before the
operands. The same expression when written in prefix notation looks like:
 Postfix
In the postfix notation the operator(s) are written after the operands, so it
is called the postfix notation (post means after), it is also known as suffix
notation. The above expression if written in postfix expression looks like:
A + B * C Infix Form
A + (B * C) Parenthesized expression
A + (B C *) Convert the multiplication
A (B C *) + Convert the addition
A B C * + Postfix form
The rules to be remembered during infix to postfix or prefix conversion are:
1. Parenthesize the expression starting from left to right.
2. During parenthesizing the expression, the operands associated with operator having higher
precedence are first parenthesized. For example in the above expression B * C is parenthesized
first before A + B.
3. The sub-expression (part of expression), which has been converted into postfix, is to be treated as
single operand.
4. Once the expression is converted to postfix form, remove the parenthesis.
Exercise
A * B + C / D
(A * B) + (C / D) Parenthesized expression
(A B*) + (C / D) Convert the multiplication
(A B*) + (C D/) Convert the division
(A B*) (C D/) + Convert the addition
A B*C D/ + Postfix form
Converting infix to prefix expression
Example:
A * B + C / D Infix Form
(A * B) + (C/D) Parenthesized expression
(*A B) + (C/D) Convert Multiplication
(*A B) + (/CD ) Convert Division
+(*A B) (/CD ) Convert addition
+*A B /CD Prefix form
Note: the precedence rules for converting an expression from infix to prefix is
identical to postfix. The only change from postfix conversion is that the operator is
placed before the operands rather than after them.
Conversion from infix to postfix (another method):
Procedure to convert from infix expression to postfix expression is as follows:
1. Scan the infix expression from left to right.
2. a) If the scanned symbol is left parenthesis, push it onto the stack.
b) If the scanned symbol is an operand, then place directly in the postfix expression
(output).
c) If the symbol scanned is a right parenthesis, then go on popping all the items from the
stack and place them in the postfix expression till we get the matching left parenthesis.
d) If the scanned symbol is an operator, then go on removing all the operators from the
stack and place them in the postfix expression, if and only if the precedence of the
operator which is on the top of the stack is greater than (or greater than or equal) to the
precedence of the scanned operator and push the scanned operator onto the stack
otherwise, push the scanned operator onto the stack.
The three important features of postfix expression are:
1. The operands maintain the same order as in the equivalent infix expression.
2. The parentheses are not needed to designate the expression unambiguously.
3. While evaluating the postfix expression the priority of the operators is no longer relevant.
Example: Convert the following infix expression to the equivalent postfix notation.
(30+23)*(43-21)/(84+7)
Need for prefix and postfix expression
The evaluation of an infix expression using a computer needs proper code
generation by the compiler without any ambiguity and is difficult because of various
aspects such as the operators priority . This problem can be overcome by writing or
converting the infix expression to an alternate notation such as the prefix or the
postfix.
((A - (B + C)) * (C+ D / B )) ^ B + C Infix expression
A B C + - C D B / + * B ^ C + Postfix expression
Lets:
A = 6
B = 2
C = 3
D = 8
6 2 3 + - 3 8 2 / + * 2 ^ 3 + Postfix
Evaluate the following expression in postfix
6 2 3 + - 3 8 2 / + * 2 ^ 3 +

More Related Content

Similar to Applications of Stack (Data Structure).pdf (20)

Prefix and PostFIx presentation for DSA .pptx
Prefix and PostFIx presentation for DSA .pptxPrefix and PostFIx presentation for DSA .pptx
Prefix and PostFIx presentation for DSA .pptx
rtiwary190801
Stack application in infix to prefix expression
Stack application in infix to prefix expressionStack application in infix to prefix expression
Stack application in infix to prefix expression
deepalishinkar1
data structure stack appplication in python
data structure stack appplication in pythondata structure stack appplication in python
data structure stack appplication in python
deepalishinkar1
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Saikrishna Tanguturu
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
operators and arithmatic expression in C Language
operators and arithmatic expression in C Languageoperators and arithmatic expression in C Language
operators and arithmatic expression in C Language
ParamesswariNataraja
How to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdfHow to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdf
arihantmobilepoint15
Data Structures And Algorithms(stacks queues)
Data Structures And Algorithms(stacks queues)Data Structures And Algorithms(stacks queues)
Data Structures And Algorithms(stacks queues)
lahariit406
The New Yorker cartoon premium membership of the
The New Yorker cartoon premium membership of theThe New Yorker cartoon premium membership of the
The New Yorker cartoon premium membership of the
shubhamgupta7133
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
ecomputernotes
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
DrkhanchanaR
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
Neeru Mittal
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
NARESH GUMMAGUTTA
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
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptx
mrizwan38
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
ssusere3b1a2
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
RockyIslam5
Coper in C
Coper in CCoper in C
Coper in C
thirumalaikumar3
Prefix and PostFIx presentation for DSA .pptx
Prefix and PostFIx presentation for DSA .pptxPrefix and PostFIx presentation for DSA .pptx
Prefix and PostFIx presentation for DSA .pptx
rtiwary190801
Stack application in infix to prefix expression
Stack application in infix to prefix expressionStack application in infix to prefix expression
Stack application in infix to prefix expression
deepalishinkar1
data structure stack appplication in python
data structure stack appplication in pythondata structure stack appplication in python
data structure stack appplication in python
deepalishinkar1
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Saikrishna Tanguturu
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
operators and arithmatic expression in C Language
operators and arithmatic expression in C Languageoperators and arithmatic expression in C Language
operators and arithmatic expression in C Language
ParamesswariNataraja
How to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdfHow to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdf
arihantmobilepoint15
Data Structures And Algorithms(stacks queues)
Data Structures And Algorithms(stacks queues)Data Structures And Algorithms(stacks queues)
Data Structures And Algorithms(stacks queues)
lahariit406
The New Yorker cartoon premium membership of the
The New Yorker cartoon premium membership of theThe New Yorker cartoon premium membership of the
The New Yorker cartoon premium membership of the
shubhamgupta7133
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
ecomputernotes
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
DrkhanchanaR
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
Neeru Mittal
Lect-5 & 6.pptx
Lect-5 & 6.pptxLect-5 & 6.pptx
Lect-5 & 6.pptx
mrizwan38
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
ssusere3b1a2
Lecture_04.2.pptx
Lecture_04.2.pptxLecture_04.2.pptx
Lecture_04.2.pptx
RockyIslam5

Recently uploaded (20)

Introductory f 23233224242342455Lec..pptx
Introductory f 23233224242342455Lec..pptxIntroductory f 23233224242342455Lec..pptx
Introductory f 23233224242342455Lec..pptx
khaledsameh950
Classroom Management that will tmotivate
Classroom Management that will tmotivateClassroom Management that will tmotivate
Classroom Management that will tmotivate
SherifsStorytime
01_BasicTechniquesTools.pptx "Malware creeps unseen, corrupting data and cont...
01_BasicTechniquesTools.pptx "Malware creeps unseen, corrupting data and cont...01_BasicTechniquesTools.pptx "Malware creeps unseen, corrupting data and cont...
01_BasicTechniquesTools.pptx "Malware creeps unseen, corrupting data and cont...
rohayiw496
Nitro Pro Crack Enterprise Free Download
Nitro Pro Crack Enterprise Free DownloadNitro Pro Crack Enterprise Free Download
Nitro Pro Crack Enterprise Free Download
kortez3
HARAYA CO EVENTS MANAGEMENT by FloBach 2
HARAYA CO EVENTS MANAGEMENT by FloBach 2HARAYA CO EVENTS MANAGEMENT by FloBach 2
HARAYA CO EVENTS MANAGEMENT by FloBach 2
Haraya Co by Florylyn
Simple Style Thesis Defense Pink variant by 際際滷sgo.pptx
Simple Style Thesis Defense Pink variant by 際際滷sgo.pptxSimple Style Thesis Defense Pink variant by 際際滷sgo.pptx
Simple Style Thesis Defense Pink variant by 際際滷sgo.pptx
josmaryguerraur
Sacrament of Confirmation& HolySpirit.pptx
Sacrament of Confirmation& HolySpirit.pptxSacrament of Confirmation& HolySpirit.pptx
Sacrament of Confirmation& HolySpirit.pptx
ShayneMcst
Ramadhan 1 Large.pdfRamadhan 1 Large.pdf
Ramadhan 1 Large.pdfRamadhan 1 Large.pdfRamadhan 1 Large.pdfRamadhan 1 Large.pdf
Ramadhan 1 Large.pdfRamadhan 1 Large.pdf
SuryaDharma65
7 Tips To Take Your Design To The Next Level!
7 Tips To Take Your Design To The Next Level!7 Tips To Take Your Design To The Next Level!
7 Tips To Take Your Design To The Next Level!
kritika598289
Smart Farming Technology Pitch Deck Presentation Template
Smart Farming Technology Pitch Deck Presentation TemplateSmart Farming Technology Pitch Deck Presentation Template
Smart Farming Technology Pitch Deck Presentation Template
際際滷sBrain
General+System+Theory_+Foundations,+Development,+Applications.pptx
General+System+Theory_+Foundations,+Development,+Applications.pptxGeneral+System+Theory_+Foundations,+Development,+Applications.pptx
General+System+Theory_+Foundations,+Development,+Applications.pptx
rcadp2018
water_cycle.ppt vishana ppt 55555566666
water_cycle.ppt vishana ppt 55555566666water_cycle.ppt vishana ppt 55555566666
water_cycle.ppt vishana ppt 55555566666
patelvishana99
edit power------------oooppppppppppppppoint.pptx
edit power------------oooppppppppppppppoint.pptxedit power------------oooppppppppppppppoint.pptx
edit power------------oooppppppppppppppoint.pptx
sarikasharma627282
L湛dica didactica (Report finale residenza Diego Alatorre Go_Innovation a Casa...
L湛dica didactica (Report finale residenza Diego Alatorre Go_Innovation a Casa...L湛dica didactica (Report finale residenza Diego Alatorre Go_Innovation a Casa...
L湛dica didactica (Report finale residenza Diego Alatorre Go_Innovation a Casa...
Casa Netural
HARAYA CO EVENTS MANAGEMENT by FloBach 3
HARAYA CO EVENTS MANAGEMENT by FloBach 3HARAYA CO EVENTS MANAGEMENT by FloBach 3
HARAYA CO EVENTS MANAGEMENT by FloBach 3
Haraya Co by Florylyn
Wedding Planners Social Media Strategy by 際際滷sgo.pptx
Wedding Planners Social Media Strategy by 際際滷sgo.pptxWedding Planners Social Media Strategy by 際際滷sgo.pptx
Wedding Planners Social Media Strategy by 際際滷sgo.pptx
TesyaMamonto
periodic properties NDA.pptx to become better
periodic properties NDA.pptx to become betterperiodic properties NDA.pptx to become better
periodic properties NDA.pptx to become better
steveparker9990sp
The CLEAR Brands CLEAR EFX Branding Guide
The CLEAR Brands CLEAR EFX Branding GuideThe CLEAR Brands CLEAR EFX Branding Guide
The CLEAR Brands CLEAR EFX Branding Guide
Adriana Yankey
Planning Unit No.9 Kharadi Development Plan for Kharadi
Planning Unit No.9 Kharadi Development Plan for KharadiPlanning Unit No.9 Kharadi Development Plan for Kharadi
Planning Unit No.9 Kharadi Development Plan for Kharadi
DanishPathan7
HARAYA CO EVENTS MANAGEMENT by FloBach 01
HARAYA CO EVENTS MANAGEMENT by FloBach 01HARAYA CO EVENTS MANAGEMENT by FloBach 01
HARAYA CO EVENTS MANAGEMENT by FloBach 01
Haraya Co by Florylyn
Introductory f 23233224242342455Lec..pptx
Introductory f 23233224242342455Lec..pptxIntroductory f 23233224242342455Lec..pptx
Introductory f 23233224242342455Lec..pptx
khaledsameh950
Classroom Management that will tmotivate
Classroom Management that will tmotivateClassroom Management that will tmotivate
Classroom Management that will tmotivate
SherifsStorytime
01_BasicTechniquesTools.pptx "Malware creeps unseen, corrupting data and cont...
01_BasicTechniquesTools.pptx "Malware creeps unseen, corrupting data and cont...01_BasicTechniquesTools.pptx "Malware creeps unseen, corrupting data and cont...
01_BasicTechniquesTools.pptx "Malware creeps unseen, corrupting data and cont...
rohayiw496
Nitro Pro Crack Enterprise Free Download
Nitro Pro Crack Enterprise Free DownloadNitro Pro Crack Enterprise Free Download
Nitro Pro Crack Enterprise Free Download
kortez3
HARAYA CO EVENTS MANAGEMENT by FloBach 2
HARAYA CO EVENTS MANAGEMENT by FloBach 2HARAYA CO EVENTS MANAGEMENT by FloBach 2
HARAYA CO EVENTS MANAGEMENT by FloBach 2
Haraya Co by Florylyn
Simple Style Thesis Defense Pink variant by 際際滷sgo.pptx
Simple Style Thesis Defense Pink variant by 際際滷sgo.pptxSimple Style Thesis Defense Pink variant by 際際滷sgo.pptx
Simple Style Thesis Defense Pink variant by 際際滷sgo.pptx
josmaryguerraur
Sacrament of Confirmation& HolySpirit.pptx
Sacrament of Confirmation& HolySpirit.pptxSacrament of Confirmation& HolySpirit.pptx
Sacrament of Confirmation& HolySpirit.pptx
ShayneMcst
Ramadhan 1 Large.pdfRamadhan 1 Large.pdf
Ramadhan 1 Large.pdfRamadhan 1 Large.pdfRamadhan 1 Large.pdfRamadhan 1 Large.pdf
Ramadhan 1 Large.pdfRamadhan 1 Large.pdf
SuryaDharma65
7 Tips To Take Your Design To The Next Level!
7 Tips To Take Your Design To The Next Level!7 Tips To Take Your Design To The Next Level!
7 Tips To Take Your Design To The Next Level!
kritika598289
Smart Farming Technology Pitch Deck Presentation Template
Smart Farming Technology Pitch Deck Presentation TemplateSmart Farming Technology Pitch Deck Presentation Template
Smart Farming Technology Pitch Deck Presentation Template
際際滷sBrain
General+System+Theory_+Foundations,+Development,+Applications.pptx
General+System+Theory_+Foundations,+Development,+Applications.pptxGeneral+System+Theory_+Foundations,+Development,+Applications.pptx
General+System+Theory_+Foundations,+Development,+Applications.pptx
rcadp2018
water_cycle.ppt vishana ppt 55555566666
water_cycle.ppt vishana ppt 55555566666water_cycle.ppt vishana ppt 55555566666
water_cycle.ppt vishana ppt 55555566666
patelvishana99
edit power------------oooppppppppppppppoint.pptx
edit power------------oooppppppppppppppoint.pptxedit power------------oooppppppppppppppoint.pptx
edit power------------oooppppppppppppppoint.pptx
sarikasharma627282
L湛dica didactica (Report finale residenza Diego Alatorre Go_Innovation a Casa...
L湛dica didactica (Report finale residenza Diego Alatorre Go_Innovation a Casa...L湛dica didactica (Report finale residenza Diego Alatorre Go_Innovation a Casa...
L湛dica didactica (Report finale residenza Diego Alatorre Go_Innovation a Casa...
Casa Netural
HARAYA CO EVENTS MANAGEMENT by FloBach 3
HARAYA CO EVENTS MANAGEMENT by FloBach 3HARAYA CO EVENTS MANAGEMENT by FloBach 3
HARAYA CO EVENTS MANAGEMENT by FloBach 3
Haraya Co by Florylyn
Wedding Planners Social Media Strategy by 際際滷sgo.pptx
Wedding Planners Social Media Strategy by 際際滷sgo.pptxWedding Planners Social Media Strategy by 際際滷sgo.pptx
Wedding Planners Social Media Strategy by 際際滷sgo.pptx
TesyaMamonto
periodic properties NDA.pptx to become better
periodic properties NDA.pptx to become betterperiodic properties NDA.pptx to become better
periodic properties NDA.pptx to become better
steveparker9990sp
The CLEAR Brands CLEAR EFX Branding Guide
The CLEAR Brands CLEAR EFX Branding GuideThe CLEAR Brands CLEAR EFX Branding Guide
The CLEAR Brands CLEAR EFX Branding Guide
Adriana Yankey
Planning Unit No.9 Kharadi Development Plan for Kharadi
Planning Unit No.9 Kharadi Development Plan for KharadiPlanning Unit No.9 Kharadi Development Plan for Kharadi
Planning Unit No.9 Kharadi Development Plan for Kharadi
DanishPathan7
HARAYA CO EVENTS MANAGEMENT by FloBach 01
HARAYA CO EVENTS MANAGEMENT by FloBach 01HARAYA CO EVENTS MANAGEMENT by FloBach 01
HARAYA CO EVENTS MANAGEMENT by FloBach 01
Haraya Co by Florylyn

Applications of Stack (Data Structure).pdf

  • 1. Applications of Stack The following Applications of stack are: 1. Stack is used by compilers to check for balancing of parentheses, brackets and braces. 2. Stack is used to evaluate a postfix expression. 3. Stack is used to convert an infix expression into postfix/prefix form. 4. In recursion, all intermediate arguments and return values are stored on the processors stack. 5. During a function call the return address and arguments are pushed onto a stack and on return they are popped off.
  • 2. Expression evaluation and conversion The most frequent application of stacks is in the evaluation of arithmetic expressions. An arithmetic expression is made of operands, operators. Operand is the quantity on which a mathematical operation is performed. Operand may be a variable like x, y, z or a constant like 5, 4, 6 etc. Operator is a symbol which signifies a mathematical or logical operation between the operands. Examples of familiar operators include +, -, *, /, ^ etc. When high-level programming languages came into existence, one of the major difficulties faced by computer scientists was to generate machine language instructions that could properly evaluate any arithmetic expression. A complex assignment statement such as
  • 3. Might have several meanings, and even if the meanings were uniquely defied, it is still difficult to generate a correct and reasonable instruction sequence. The fist problem in understanding the meaning of an expression is to decide the order in which the operations are to be carried out. This demands that every language must uniquely define such an order.
  • 4. Another example: To calculate this expression for values 4, 3, 7 for A, B, C respectively we must follow certain in order to have the right result : The answer is not correct; multiplication is to be done before the addition, because multiplication has higher precedence over addition. This means that an expression is calculated according to the operators precedence not the order as they look like. Thus expression A + B * C can be interpreted as A + (B * C). Using this alternative method we can convey to the computer that multiplication has higher precedence over addition.
  • 5. To fix the order of evaluation, assign each operator a priority, and evaluated from left to right.
  • 6. Let us consider the expression By using priorities rules, the expression X is rewritten as We manually evaluate the innermost expression first, followed by the next parenthesized inner expression, which produces the result. Another application of stack is calculation of postfix expression. There are basically three types of notation for an expression (mathematical expression; An expression is defined as the number of operands or data items combined with several operators.) 1. Infix notation 2. Prefix notation 3. Postfix notation
  • 7. Infix The infix notation is what we come across in our general mathematics, where the operator is written in-between the operands. For example: The expression to add two numbers A and B is written in infix notation as: Prefix The prefix notation is a notation in which the operator(s) is written before the operands. The same expression when written in prefix notation looks like: Postfix In the postfix notation the operator(s) are written after the operands, so it is called the postfix notation (post means after), it is also known as suffix notation. The above expression if written in postfix expression looks like:
  • 8. A + B * C Infix Form A + (B * C) Parenthesized expression A + (B C *) Convert the multiplication A (B C *) + Convert the addition A B C * + Postfix form The rules to be remembered during infix to postfix or prefix conversion are: 1. Parenthesize the expression starting from left to right. 2. During parenthesizing the expression, the operands associated with operator having higher precedence are first parenthesized. For example in the above expression B * C is parenthesized first before A + B. 3. The sub-expression (part of expression), which has been converted into postfix, is to be treated as single operand. 4. Once the expression is converted to postfix form, remove the parenthesis.
  • 9. Exercise A * B + C / D (A * B) + (C / D) Parenthesized expression (A B*) + (C / D) Convert the multiplication (A B*) + (C D/) Convert the division (A B*) (C D/) + Convert the addition A B*C D/ + Postfix form
  • 10. Converting infix to prefix expression Example: A * B + C / D Infix Form (A * B) + (C/D) Parenthesized expression (*A B) + (C/D) Convert Multiplication (*A B) + (/CD ) Convert Division +(*A B) (/CD ) Convert addition +*A B /CD Prefix form Note: the precedence rules for converting an expression from infix to prefix is identical to postfix. The only change from postfix conversion is that the operator is placed before the operands rather than after them.
  • 11. Conversion from infix to postfix (another method): Procedure to convert from infix expression to postfix expression is as follows: 1. Scan the infix expression from left to right. 2. a) If the scanned symbol is left parenthesis, push it onto the stack. b) If the scanned symbol is an operand, then place directly in the postfix expression (output). c) If the symbol scanned is a right parenthesis, then go on popping all the items from the stack and place them in the postfix expression till we get the matching left parenthesis. d) If the scanned symbol is an operator, then go on removing all the operators from the stack and place them in the postfix expression, if and only if the precedence of the operator which is on the top of the stack is greater than (or greater than or equal) to the precedence of the scanned operator and push the scanned operator onto the stack otherwise, push the scanned operator onto the stack. The three important features of postfix expression are: 1. The operands maintain the same order as in the equivalent infix expression. 2. The parentheses are not needed to designate the expression unambiguously. 3. While evaluating the postfix expression the priority of the operators is no longer relevant.
  • 12. Example: Convert the following infix expression to the equivalent postfix notation. (30+23)*(43-21)/(84+7)
  • 13. Need for prefix and postfix expression The evaluation of an infix expression using a computer needs proper code generation by the compiler without any ambiguity and is difficult because of various aspects such as the operators priority . This problem can be overcome by writing or converting the infix expression to an alternate notation such as the prefix or the postfix. ((A - (B + C)) * (C+ D / B )) ^ B + C Infix expression A B C + - C D B / + * B ^ C + Postfix expression Lets: A = 6 B = 2 C = 3 D = 8 6 2 3 + - 3 8 2 / + * 2 ^ 3 + Postfix
  • 14. Evaluate the following expression in postfix 6 2 3 + - 3 8 2 / + * 2 ^ 3 +