Applications of Stack
Applications of Stack
Applications of Stack
• Evaluation of Arithmetic Expressions
• Backtracking
• Delimiter Checking
• Reverse a Data
• Processing Function Calls
Evaluation of Arithmetic Expressions
• A stack is a very effective data structure for evaluating arithmetic
expressions in programming languages. An arithmetic expression
consists of operands and operators.
• In addition to operands and operators, the arithmetic expression may
also include parenthesis like "left parenthesis" and "right
parenthesis".
• Example: A + (B - C)
• To evaluate the expressions, one needs to be aware of the standard
precedence rules for arithmetic expression.
• The precedence rules for the five basic arithmetic operators are:
Evaluation of Arithmetic Expression requires two steps:
• First, convert the given expression into special notation.
• Evaluate the expression in this new notation.
Notations for Arithmetic Expression
There are three notations to represent an arithmetic expression:
• Infix Notation
• Prefix Notation
• Postfix Notation
Infix Notation
• 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.
• The infix notation is a convenient way of writing an expression in which each
operator is placed between the operands.
• Infix expressions can be parenthesized or unparenthesized depending upon the
problem requirement.
• Example: A + B, (C - D) etc.
• All these expressions are in infix notation because the operator comes between the
Prefix Notation
• The prefix notation places the operator before the operands.
This notation was introduced by the Polish mathematician and
hence often referred to as polish notation.
• Example: + A B, -CD etc.
• All these expressions are in prefix notation because the
operator comes before the operands.
Postfix Notation
• The postfix notation places the operator after the operands. This
notation is just the reverse of Polish notation and also known as
Reverse Polish notation.
• Example: AB +, CD+, etc.
• All these expressions are in postfix notation because the operator
comes after the operands.
2. Backtracking
Backtracking is another application of Stack. It is a recursive
algorithm that is used for solving the optimization problem.
Convert Infix to Postfix notation
• Print the operand as they arrive.
• If the stack is empty or contains a left parenthesis on top, push the
incoming operator on to the stack.
• If the incoming symbol is '(', push it on to the stack.
• If the incoming symbol is ')', pop the stack and print the operators until
the left parenthesis is found.
• If the incoming symbol has higher precedence than the top of the stack,
push it on the stack.
• If the incoming symbol has lower precedence than the top of the stack,
pop and print the top of the stack. Then test the incoming operator
against the new top of the stack.
• If the incoming operator has the same precedence with the top of the
stack then use the associativity rules. If the associativity is from left to
right then pop and print the top of the stack then push the incoming
operator. If the associativity is from right to left then push the incoming
operator.
conversion of infix to prefix expression:
First, reverse the infix expression given in the problem.
• Scan the expression from left to right.
• Whenever the operands arrive, print operand.
• If the operator arrives and the stack is found to be empty, then simply push the operator
into the stack.
• If the incoming operator has higher precedence than the TOP of the stack, push the
incoming operator into the stack.
• If the incoming operator has the same precedence with a TOP of the stack, push the
incoming operator into the stack.
• If the incoming operator has lower precedence than the TOP of the stack, pop, and print the
top of the stack. Test the incoming operator against the top of the stack again and pop the
operator from the stack till it finds the operator of a lower precedence or same precedence.
• If the incoming operator has the same precedence with the top of the stack and the
incoming operator is ^, then pop the top of the stack till the condition is true. If the
condition is not true, push the ^ operator.
• When we reach the end of the expression, pop, and print all the operators from the top of
the stack.
• If the operator is ')', then push it into the stack.
• If the operator is '(', then pop all the operators from the stack till it finds ) opening bracket in
the stack.
• If the top of the stack is ')', push the operator on the stack.