Stack ADT is an abstract data type that usually holds a collection of data and enables to perform certain operations on it in a particular order is called LIFO (Last In First Out), which means that the last element that is put into the collection will be the first element to get out.
APPLICATION OF STACK ADT:
Every single software engineer must understand the stacks has many applications starting from memory management to real life and it is a fundamental (and simple) data structure. Generally, one of the applications of Stack is in the conversion of arithmetic expressions in high-level programming languages into machine-readable form.
As our computer system can only understand and work on a binary language, it assumes that an arithmetic operation can take place in two operands only e.g., A+B, C*D, D/A, etc. But in our usual form, an arithmetic expression may consist of more than one operator and two operands e.g. (A+B)*C(D/(J+D)).
Meanwhile,we need to know about the two expressions which occur in the evaluation process of the calculator:
- INFIX EXPRESSION:
It follows the scheme of <operand><operator><operand>. Such an expression is termed infix expression. E.g., A+B
- POSTFIX EXPRESSION:
It follows the scheme of <operand><operand><operator>.
STACK ADT HELPS IN EVALUATION:
- INFIX TO POSTFIX CONVERSION ALGORITHM:
- Push “(“onto Stack, and add “)” to the end of X.
- Scan X from left to right and repeat Steps 3 to 6 for each element of X until the Stack is empty.
- If an operand is encountered, add it to Y.
- If a left parenthesis is encountered, push it onto Stack.
- If an operator is encountered, then:
- Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which has the same precedence as or higher precedence than the operator.
- Add operator to Stack.
- [End of If]
- If a right parenthesis is encountered, then:
- Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a left parenthesis is encountered.
- Remove the left Parenthesis.
- [End of If]
Let’s take an example to better understand the algorithm
Infix Expression: A+ (B*C-(D/E^F)*G)*H, where ^ is an exponential operator.
- POSTFIX TO INFIX CONVERSION ALGORITHM:
1) Add ) to postfix expression.
2) Read postfix expression Left to Right until ) encountered
3) If the operand is encountered, push it onto Stack
4) If the operator is encountered, Pop two elements
i) A -> Top element
ii) B-> Next to Top elements
iii) Evaluate B operator A push B operator A onto Stack
5) Set result = pop
Let’s see an example to better understand the algorithm:
Advantage of Postfix Expression over Infix Expression:
An infix expression is difficult for the machine to know and keep track of precedence of operators. On the other hand, a postfix expression itself determines the precedence of operators (as the placement of operators in a postfix expression depends upon its precedence). Therefore, for the machine, generally it is easier to carry out a postfix expression than an infix expression.
If you were aware of Stack data structure and thinking that it is useless in real life, well, here is the real-life use case for you. I hope this will be useful to understand the concepts of the stack as well as the evaluation process and algorithms of the calculator.