You are on page 1of 4

Applications of Stack

Expression Evaluation
Stack is used to evaluate prefix, postfix and infix expressions.

Expression Conversion
An expression can be represented in prefix, postfix or infix notation. Stack can be used
to convert one form of expression to another.

Syntax Parsing
Many compilers use a stack for parsing the syntax of expressions, program blocks etc.
before translating into low level code.

Backtracking
Suppose we are finding a path for solving maze problem. We choose a path and after
following it we realize that it is wrong. Now we need to go back to the beginning of the
path to start with new path. This can be done with the help of stack.

Parenthesis Checking
Stack is used to check the proper opening and closing of parenthesis.

String Reversal
Stack is used to reverse a string. We push the characters of string one by one into
stack and then pop character from stack.
 
Function Call
Stack is used to keep information about the active functions or subroutines.

There are various other applications of stack. I have mentioned some of them.
Comment below if you found any mistake in above article.

Infix Nortation
Infix notation is commonly used in arithmetic formula or statements, the operators are
written in-between their operands.
Let’s assume the below

 Operands are real numbers.


 Permitted operators: +, -, *, /, ^(exponentiation)
 Blanks are permitted in expression.
 Parenthesis are permitted
Example:
A * (B + C) / D

2 * (5 + 3) / 4

Output: 4

Approach: Use Stacks
We will use two stacks

 Operand stack: This stack will be used to keep track of numbers.


 Operator stack: This stack will be used to keep operations (+, -, *, /, ^)
Order of precedence of operations–
1. ^ (Exponential)
2. /*
3. +–
Note: brackets ( ) are used to override these rules.
Let’s define the Process: (will be used for the main algorithm)
1. Pop-out two values from the operand stack, let’s say it is A and B.
2. Pop-out operation from operator stack. let’s say it is ‘+’.
3. Do A + B and push the result to the operand stack.
Algorithm:
Iterate through given expression, one character at a time

1. If the character is an operand, push it to the operand stack.


2. If the character is an operator,
1. If the operator stack is empty then push it to the operator stack.
2. Else If the operator stack is not empty,
 If the character’s precedence is greater than or equal to the
precedence of the stack top of the operator stack, then push the
character to the operator stack.
 If the character’s precedence is less than the precedence of
the stack top of the operator stack then do Process (as explained
above) until character’s precedence is less or stack is not empty.
3. If the character is “(“, then push it onto the operator stack.
4. If the character is “)”, then do Process (as explained above) until the
corresponding “(” is encountered in operator stack. Now just pop out the
“(“.
Postfix Notation (Reverse Polish Notation):
Example: A B+
Operators are used after their operands for example to add 3 and 4, instead of writing
3 + 4 which is infix expression, postfix expression will be 3 4 +. The order of
evaluation of operators is always left-to-right, and brackets cannot be used to change
this order. Postfix expression of example above will be A B C + * D /. Operators act
on values immediately to the left of them. This is also called as Polish postfix
notation or simply postfix notation.
Algorithm

Algorithm (start with an empty stack):


while expression has tokens {
if next token is operand /* e.g., number */
push it on the stack;
else /* next token should be an operator */
pop two operands from stack;
perform operation;
push result of operation on stack;
}
pop the result; /* should be only thing left on stack */

Expression: 5 4 + 8 *
– Step 1: push 5
– Step 2: push 4
– Step 3: pop 4, pop 5, add, push 9
– Step 4: push 8
– Step 5: pop 8, pop 9, multiply, push 72
– Step 6: pop 72 – the result

Prefix Notation (Polish Notation):


Example: + A B
Operators are used before their operands for example to add 3 and 4, instead of
writing 3 + 4 which is infix expression, prefix expression will be + 3 4. The
expressions given above are equivalent to / * A + B C D . Operators are evaluated left
to right. Operators act on the two nearest values on the right. Also known as normal
Polish notation, Polish prefix notation or simply prefix notation
Ex 1: ( 1 - 2 ) * 3 can be written as * - 1 2 3 in prefix notation.

→, V. (2020). Applications of Stack - The Crazy Programmer. Retrieved 21 February 2020, from
https://www.thecrazyprogrammer.com/2016/04/applications-of-stack.html

Evaluation of Infix expressions | Algorithms. (2020). Retrieved 21 February 2020, from


https://algorithms.tutorialhorizon.com/evaluation-of-infix-expressions/

You might also like