Sie sind auf Seite 1von 17

Application of Stacks

 Reversing a Line
 Parenthesis Matching – to match left and right parenthesis
in an expression.
 Converting Infix arithmetic expression to Postfix or Prefix
form.
 Evaluation of a Postfix or Prefix arithmetic expression.
 Execution of Nested and Recursive function calls.
(will be discussed in Grade 12)
 Used in Backtracking algorithms like Solving a maze
Problem that involves finding the correct path in a maze.
To reach the final destination, there are several paths. After
following a certain path, if the path chosen is wrong, it is
possible to return to the beginning of that path with the
help of stacks and again trace the correct path.
Infix and Postfix Expressions
An infix expression is one in which the operator is
placed in between the two operands. Eg : a + b.

The infix form of representation is inefficient from


the compiler point of view because repeated scanning
is required from left to right for the evaluation of
expression.

Hence in the postfix form eg : a b + where the


operator is placed after its operands is found to be an
efficient way of representation.
An infix expression can be converted into a postfix expression
by the following steps :
1. Fully parenthesise the infix expression according to the
desired evaluation order.
2. Replace the right hand parenthesis by its
corresponding embedded operator.
3. While writing the final postfix expression, omit
parenthesis.
Example 1) : (a+b) * c – d
= (((a+b)*c)–d)
= ab+c*d–
Exercise : ( ( ( x * y - a * b ) – d / f ) + a ) * b
Answer : x y * a b * - d f / - a + b *
Convert the following Infix Expression to Postfix
1.a+b*c-d/e*f
2.(a+b*c-d)/(e*f)
3.a+b-d/x
4.a*(b+(c+d)*(e+f)/g)*h
Algorithm for conversion from Infix Expression to
Postfix Expression
 Let the infix expression X end with a ;
 The Infix expression is scanned from left to right.
 A stack of size N (which must be the length of the Infix
String) is used, to hold operators and left parenthesis,
temporarily.
 The Postfix Expression Y is constructed from left to
right using the operands from X and the operators that
are popped out from the Stack.
 Algorithm
1.Read a character from the infix expression X.

2. If the scanned character is an operand then add it to Y


and go to step 1.

3.If the scanned character is ‘ ( ‘ then PUSH the


element on stack and go to step 1.

4. If the scanned character is ‘ ) ‘, then


-repeatedly pop an element from the stack, add it to
Y if the element is not ‘ ( ‘ .
-continue till ‘ ( ‘ is encountered. Do not add the
left parenthesis to Y.
Go to step1.
5. If the scanned character is an Operator and the stack is
empty, push the character in the stack.

6. If the scanned character is an Operator and the stack is


not empty, then compare the precedence of the character
with the element on the top of the stack.

7.If the scanned character has higher precedence than the


element on top of the stack then Push the character in
the Stack.
8. On the other hand if the element in the stack has got
higher or equal precedence than the scanned
character, then do the following:

a) POP the element from stack


b) Print the popped element [if it is not ‘ ( ‘ ].

Repeat steps (a) and (b) as long as the stack is not


empty and the element on top of the stack has
higher or equal precedence than the scanned
character.
9. Push the current operator into the stack and go to
step 1.

10. If the scanned character is a semicolon then POP


an element from stack and print it. Repeat the
process until the stack is empty.

11. End
Conversion from infix to postfix :
( X *Y–A) – ( D /F +A) ;
Element Action Stack Output
( Push (
X Print ( X
* Push (* X
Y Print (* XY
- Pop, Print ( XY*
Push (-
A Print (- XY*A
) Pop,Print, ( XY*A-
Pop Empty

- Push - XY*A-
( Push -( XY*A-
D Print -( XY*A-D
/ Push -(/ XY*A-D
F Print -(/ XY*A-DF

+ Pop, Print -( XY*A-DF/


Push -(+
A Print -(+ XY*A-DF/A

) Pop, Print - ( XY*A-DF/A+


Pop -
; Pop & Print Empty XY*A-DF/A+-
Convert the following Infix Expression to Postfix
Expression using Stacks

1. a+((b+c)+(d+e)*f)/g
2. (a-b)*(c/d)+e
3. ((a+b)*c/d+e^f)/g
Evaluation of a Postfix expression
Let the postfix expression end with a ;
Algorithm:
1. Read an element from the infix expression.
2. If element is an operand then PUSH the element to stack and
go to step 1.
3. If element is an operator then POP operand 1 from stack
POP operand 2 from stack
Perform the desired operation
Push the result to stack
Go to step 1.
4. If element is a “;” then POP the result from stack and print result
else go to step 1.
5. End.
Evaluate : 10 15 - 25 5 / + ;
Element Action Stack
10 Push 10
15 Push 10 15
- Pop(15) Pop(10)
Compute (10 – 15) Push( -5) -5
25 Push -5 25
5 Push -5 25 5
/ Pop(5) Pop(25)
Compute ( 25/ 5) Push ( 5 ) -5 5
+ Pop (5) Pop(-5)
Compute (-5+5) Push( 0 ) 0
; Pop result = 0 empty
Evaluate the Postfix Expression given
below
 6 12 2 * 3 / 5 3 * - +

Note: The variables have been substituted


with actual values to enable evaluation

Das könnte Ihnen auch gefallen