0% found this document useful (0 votes)
10 views12 pages

Applications of Stack

The document outlines the applications of stack data structures, including converting algebraic expressions between infix, prefix, and postfix forms, evaluating postfix expressions, and balancing parentheses in compilers. It explains the rules for converting infix expressions to postfix and prefix notations, along with examples of each. Additionally, it provides a sample evaluation of a postfix expression.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Applications of Stack

The document outlines the applications of stack data structures, including converting algebraic expressions between infix, prefix, and postfix forms, evaluating postfix expressions, and balancing parentheses in compilers. It explains the rules for converting infix expressions to postfix and prefix notations, along with examples of each. Additionally, it provides a sample evaluation of a postfix expression.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Applications of

Stack
• Converting algebraic expressions from
one form to another. E.g. Infix to
Postfix,
Prefix, Infix to to Infix, Prefix Postfix,
Prefix
to Postfix to Infix and
Postfix to prefix.
• Evaluation of Postfix expression.
• Parenthesis Balancing in Compilers.
• Depth First Search Traversal of
Graph.
• RecursiveApplications.
Algebraic
Expressions
• Infix: It is the form of an arithmetic expression in which
we fix (place) the arithmetic operator in between the two
operands. E.g.: (A + B) * (C - D)
• Prefix: It is the form of an arithmetic notation in which
we fix (place) the arithmetic operator before (pre) its
two operands. The prefix notation is called as polish
notation. E.g.: * + A B – C D
• Postfix: It is the form of an arithmetic expression in
which we fix (place) the arithmetic operator after (post)
its two operands. The postfix notation is called as suffix
notation and is also referred to reverse polish notation.
E.g: A B + C D - *
• Rules for the conversion from infix to postfix
expression
1.Print the operand as they arrive.
2.If the stack is empty or contains a left parenthesis on top, push the incoming operator on
to the stack.
3.If the incoming symbol is '(', push it on to the stack.
4.If the incoming symbol is ')', pop the stack and print the operators until the left parenthesis
is found.
5.If the incoming symbol has higher precedence than the top of the stack, push it on the
stack.
6.If the incoming symbol has lower precedence than the top of the stack, pop and print the
top of the stack. Then test the incoming operator against the new top of the stack.
7.If the incoming operator has the same precedence with the top of the stack then use the
associativity rules. If the associativity is from left to right then pop and print the top of the
stack then push the incoming operator. If the associativity is from right to left then push
the incoming operator.
8.At the end of the expression, pop and print all the operators of the stack.
The final postfix expression of infix expression(K + L - M*N + (O^P) * W/U/V
* T + Q) is KL+MN*-OP^W*U/V/T*+Q+.
Conversion from Infix to
Postfix
Convert the following infix expression A + B * C – D / E * H into itsequivalentpostfix
expression.
Conversion from Infix to Prefix
•First, reverse the infix expression given in the problem.
•Scan the expression from left to right.
•Whenever the operands arrive, print them.
•If the operator arrives and the stack is found to be empty, then simply push the
operator into the stack.
•If the incoming operator has higher precedence than the TOP of the stack, push the
incoming operator into the stack.
•If the incoming operator has the same precedence with a TOP of the stack, push the
incoming operator into the stack.
•If the incoming operator has lower precedence than the TOP of the stack, pop, and
print the top of the stack. Test the incoming operator against the top of the stack
again and pop the operator from the stack till it finds the operator of a lower
precedence or same precedence.
•If the incoming operator has the same precedence with the top of the stack and the
incoming operator is ^, then pop the top of the stack till the condition is true. If the
condition is not true, push the ^ operator.
•Whe
•n we reach the end of the expression, pop, and print all the operators from the top of
the stack.
•If the operator is ')', then push it into the stack.
Conversion from Infix to Prefix
Conversion from Infix to Prefix
Conversion from Infix to Prefix
Evaluation of Postfix Expression
Postfix expression: 6 5 2 3 + 8 * + 3 +
*

You might also like