Application of Stack - Expressions
Application of Stack - Expressions
When the operator is written in between the operands, then it is known as infix notation.
Operand does not have to be always a constant or a variable; it can also be an expression
itself.
For example,
(p + q) * (r + s)
In the above expression, both the expressions of the multiplication operator are the
operands, i.e., (p + q), and (r + s) are the operands.
In the above expression, there are three operators. The operands for the first plus operator
are p and q, the operands for the second plus operator are r and s. While performing
the operations on the expression, we need to follow some set of rules to evaluate
the result. In the above expression, addition operation would be performed on the two
expressions, i.e., p+q and r+s, and then the multiplication operation would be performed.
If there is only one operator in the expression, we do not require applying any rule. For
example, 5 + 2; in this expression, addition operation can be performed between the two
operands (5 and 2), and the result of the operation would be 7.
If there are multiple operators in the expression, then some rule needs to be followed to
evaluate the expression.
4+6*2
If the plus operator is evaluated first, then the expression would look like:
10 * 2 = 20
If the multiplication operator is evaluated first, then the expression would look like:
4 + 12 = 16
The above problem can be resolved by following the operator precedence rules. In the
algebraic expression, the order of the operator precedence is given in the below table:
Operators Symbols
Parenthesis ( ), {}, [ ]
Exponents ^
The first preference is given to the parenthesis; then next preference is given to the
exponents. In the case of multiple exponent operators, then the operation will be applied
from right to left.
For example:
2^2^3 = 2 ^ 8
= 256
After exponent, multiplication, and division operators are evaluated. If both the
operators are present in the expression, then the operation will be applied from left
to right.
The next preference is given to addition and subtraction. If both the operators are
available in the expression, then we go from left to right.
The operators that have the same precedence termed as operator associativity. If we go
from left to right, then it is known as left-associative. If we go from right to left, then it is
known as right-associative.
Postfix Expression
The postfix expression is an expression in which the operator is written after the operands.
For example, the postfix expression of infix notation ( 2+3) can be written as 23+.
o In postfix expression, operations are performed in the order in which they have
written from left to right.
o It does not any require any parenthesis.
o We do not need to apply operator precedence rules and associativity rules.
o Scan the expression from left to right until we encounter any operator.
o Perform the operation
o Replace the expression with its computed value.
o Repeat the steps from 1 to 3 until no more operators exist.
Infix expression: 2 + 3 * 4
We will start scanning from the left most of the expression. The multiplication operator is
an operator that appears first while scanning from left to right. Now, the expression would
be:
Expression = 2 + 34*
= 2 + 12
Again, we will scan from left to right, and the expression would be:
Expression = 2 12 +
= 14
Input Stack
34*+ 2 Push 3
4*+ 32 Push 4
*+ 432 Pop 4 and 3, and perform 4*3 = 12. Push 12 into the stack.
+ 12 2 Pop 12 and 2 from the stack, and perform 12+2 = 14. Push 14 into the
stack.
Input Stack
*2 5 * + 43 Pop 3 and 4 from the stack and perform 3*4 = 12. Push 12 into the
stack.
25*+ 12 Push 2
5*+ 2 12 Push 5
*+ 5 2 12 Pop 5 and 2 from the stack and perform 5*2 = 10. Push 10 into the
stack.
+ 10 12 Pop 10 and 12 from the stack and perform 10+12 = 22. Push 22
into the stack.
1. Read a character
2. If the character is a digit, convert the character into int and push the integer into
the stack.
3. If the character is an operator,
o Pop the elements from the stack twice obtaining two operands.
o Perform the operation
o Push the result into the stack.
K K
+ +
L + KL
- - K L+
M - K L+ M
* -* K L+ M
N -* KL+MN
+ + K L + M N*
K L + M N* -
( +( 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+