0% found this document useful (0 votes)
4 views

Applications of Stack

Uploaded by

vrajpatel.2060
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Applications of Stack

Uploaded by

vrajpatel.2060
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Applications of Stack

 Validity of expression
 Valid Expression Rules:
1) There should not be 2 consecutive OPRANDS
2) There should not be 2 consecutive OPERATORS.
3) Operand must not be followed by opening parenthesis.
4) Opening parenthesis must not be followed by operator.
5) Operator must not be followed by closing parenthesis.
6) Closing parenthesis must not be followed by operator.
7) Last symbol should not be an operator.
 If any of the above condition is become true then expression is not a valid expression.
 For validity of parenthesis consider following rules:
1) Same number of closing and opening brackets.
2) Sequence of opening and closing brackets.

 Infix to Postfix conversion


Algorithm for Infix to Postfix conversion
input: input string, output string, operator_stack
1) Scan one symbol at a time from the input string in Left to Right. Say it as current
symbol.
2) If current symbol is operand then store it into output string.
3) If current symbol is opening bracket ‘(’ then push it into stack.
4) If current symbol is closing bracket ‘)’ then pop all the elements of stack till first ‘(’ is
found.
5) If current symbol is operator and if stack is empty then Push to stack,
else check the priority level of current symbol and top of the stack symbol. If the priority
of top of the stack symbol is
i) greater than current symbol then pop that symbol and append to the output string.
ii) equal to current symbol then
a) if precedence is Left to Right then pop that symbol and append to the output string.
b) if precedence is Right to Left then do nothing.
iii) Less than current symbol then do nothing.
Repeat the same process until you get the symbol on top of the stack whose precedence
level is lower than current symbol. Finally, Push the current symbol to the stack.
6) When null encountered in input string then pop all symbols from stack and append them
to the output string.
Output: Output string contains postfix expression.

Example of Infix to Postfix:


Infix expression: K + L - M*N + (O^P) * W/U/V * T + Q

Current symbol Postfix Expression Stack


K K
+ K +
L KL +
- K L+ -
M K L+ M -
* K L+ M -*
N KL+MN -*
+ KL+MN* - +
( K L + M N *- +(
O KL+MN*-O +(
^ K L + M N* - O +(^
P K L + M N* - O P +(^
) K L + M N* - O P ^ +
* K L + M N* - O P ^ +*
W K L + M N* - O P ^ W +*
/ K L + M N* - O P ^ W * +/
U K L + M N* - O P ^W*U +/
/ K L + M N* - O P ^W*U/ +/
V KL + MN*-OP^W*U/V +/
* KL+MN*-OP^W*U/V/ +*
T KL+MN*-OP^W*U/V/T +*
+ KL+MN*-OP^W*U/V/T* +
KL+MN*-OP^W*U/V/T*+
Q KL+MN*-OP^W*U/V/T*Q +
KL+MN*-OP^W*U/V/T*+Q+

The final postfix expression is KL+MN*-OP^W*U/V/T*+Q+.

 Evaluation of Postfix expression


Algorithm to evaluate postfix expression:
1) First convert infix expression into postfix expression.
2) Start scanning the postfix expression from left to right.
3) If the element is an operand, push it into the stack.
4) If the element is an operator, pop two operands from the stack. The first popped element
becomes the second operand and the second popped element becomes the first operand.
Perform the arithmetic operation. Push the result back to the stack. Repeat the same
process until all the elements are scanned.
5) When the expression ends, the value in the stack is the final answer.

Pseudo code:
while (not end of input)
symbol = input symbol
if (symbol is operand)
push (operand_stack, symbol)
else
operand2 = pop(operand_stack)
operand1 = pop(operand_stack)
result = apply operation specified by symbol on operand1 and operand2
push (operand_stack, result)
return pop(operand_stack)
Example:
Postfix expression : 6 2 3 + - 3 8 2 / + * 2 ^ 3 +
Answer is: 52

 Infix to Prefix conversion


Algorithm for Infix to Prefix conversion
input: input string, output string, operator_stack

1) Reverse the infix expression and call it as new input string.


2) Scan one symbol at a time from the new input string in Left to Right. Say it as current
symbol.
3) If current symbol is operand then store it into output string.
4) If current symbol is opening bracket ‘)’ then push it into stack.
5) If current symbol is closing bracket ‘(’ then pop all the elements of stack till first ‘)’ is
found.
6) If current symbol is operator and if stack is empty then push to stack,
else check the priority level of current symbol and top of the stack symbol. If the priority
of top of the stack symbol is
i) greater than current symbol then pop that symbol and append to the output string.
ii) equal to current symbol then
a) if precedence is Left to Right then do nothing.
b) if precedence is Right to Left then pop that symbol and append to output string.
iii) Less than current symbol then do nothing.
Repeat the same process until you get the symbol on top of the stack whose precedence
level is lower than current symbol. Finally, Push the current symbol to the stack.
7) When null encountered in new input string then pop all symbols from stack and append
them to the output string.
8) Reverse the output string. Call it as new output string.
Output: new output string contains prefix expression.
[Note: Purple color text shows difference of infix to postfix and infix to prefix algorithm]

Example: Convert given infix to prefix using stack-


K + L - M * N + (O^P) * W/U/V * T + Q
Reverse of infix expression: Q + T * V/U/W * ) P^O(+ N*M - L + K

Current symbol Prefix expression Stack


Q Q
+ Q +
T QT +
* QT +*
V QTV +*
/ QTV +*/
U QTVU +*/
/ QTVU +*//
W QTVUW +*//
* QTVUW +*//*
) QTVUW +*//*)
P QTVUWP +*//*)
^ QTVUWP +*//*)^
O QTVUWPO +*//*)^
( QTVUWPO^ +*//*
+ QTVUWPO^*//* ++
N QTVUWPO^*//*N ++
* QTVUWPO^*//*N ++*
M QTVUWPO^*//*NM ++*
- QTVUWPO^*//*NM* ++-
L QTVUWPO^*//*NM*L ++-
+ QTVUWPO^*//*NM*L ++-+
K QTVUWPO^*//*NM*LK ++-+
QTVUWPO^*//*NM*LK+-++
Reverse this expression to obtain the required prefix expression.
Answer: ++-+KL*MN*//*^OPWUVTQ

 Evaluation of Prefix expression


Algorithm to evaluate prefix expression:
1) First convert infix expression into prefix expression.
2) Reverse the prefix expression and start scanning the prefix expression from left to right.
3) If the element is an operand, push it into the stack.
4) If the element is an operator, pop two operands from the stack. The first popped element
becomes the first operand and the second popped element becomes the second operand.
Perform the arithmetic operation. Push the result back to the stack. Repeat the same
process until all the elements are scanned.
5) When the expression ends, the value in the stack is the final answer.
Example: Evaluate given prefix expression: + ^ * - 6 + 2 3 + 3 / 8 2 2 3.
Reverse the given expression: 3 2 2 8 / 3 + 3 2 + 6 - * ^ +

ch Oprand1 Oprand2 Result Oprand_stack


3 3
2 3,2
2 3,2,2
8 3,2,2,8
/ 8 2 4 3,2,4
3 3,2,4,3
+ 3 4 7 3,2,7
3 3,2,7,3
2 3,2,7,3,2
+ 2 3 5 3,2,7,5
6 3,2,7,5,6
- 6 5 1 3,2,7,1
* 1 7 7 3,2,7
^ 7 2 49 3,49
+ 49 3 52 52

Answer: 52

Towers of Hanoi
The Tower of Hanoi is a mathematical game or puzzle. It consists of three towers- source,
axillary and destination. There are N disks of decreasing size (no two disks are of the same
size). Initially all the disks are stacked on the source tower in their decreasing order of size.
The objective of the puzzle is to move all the disks from source tower to the destination tower
with the help of intermediate tower. There are 2 rules of the puzzle:
1) Only one disk can be moved at a time.
2) Bigger disk cannot be placed on the smaller disk.
Consider three towers name A, B and C. A is source tower, C is Destination tower and B is
intermediate tower. So, we want to transfer N disks from tower A to tower C using tower B.
We will perform following Steps to get solution:
1) Move N-1 discs from A to B using C.
2) Move discs N from A to C.
3) Move N-1 discs from B to C using A.

Algorithm:
function move (N, source, intermediate, destination)
If N >0 then
move (N-1, source, destination, intermediate)
place Nth disk from source to destination
move (N-1, intermediate, source, destination)
end if

Working of algorithm:
Consider N=3 and see how algorithm works with recursion
Illustration:
1) N=3
2) N=4

You might also like