Some of the applications of stack are:
4.Function call:
- Evaluating arithmetic expression
- Balancing the symbols
- Towers of Hanoi
- Function Calls
1.Evaluating arithmetic expression
There are 3 different ways of representing the algebraic expression.
(a) INFIX NOTATION
(a) INFIX NOTATION
(b) PREFIX NOTATION
(c) POSTFIX NOTATION
(a) INFIX NOTATION
In Infix notation, the arithmetic operator appears between the two operands to which it is being applied.
For example:
A/B+C
(b) PREFIX NOTATION
The arithmetic operator is placed before the two operands to which it applied. Also called as polish notation.
For example:
((A/B)+C) ➜ +/ABC
(c) POSTFIX NOTATION
The arithmetic operator appears directly after the two operands to which it appears. also called Reverse polish notation.
For example:
((A/B)+C) ➜AB/C+
2.Balancing the symbol:
Stack can also be used to check if the given expression has balanced symbol or not.the algorithm is very much useful in compiler.Each time parser reads one character at a time.
- If the character is a opening symbol like '(', '{' or '[' then it is PUSH into the stack.
- When a closing symbol like ')', '}' or ']' then the stack is popped.
- The opening and the closing symbols are then compared.if they match the parsing of the string continues.If they do not match the parser indicate that there is an error on the line.
Towers of Hanoi is one of the example illustrating the recursion technique. the problem is moving a collection of N disks of decreasing size from one pillar to another pillar. the movement of the disk is restricted by the following rules.
- Only one disk could be moved at a time.
- No longer disk could ever reside on a pillar on top of the smaller disk.
- A 3rd pillar could be used as an intermediate to store one or more disks,while they were being moved from source to destination.
4.Function call:
When a call is made to a new function all the variables local to the calling routine need to be saved, otherwise the new function will overwrite the calling routine variables.Similarly the current location address in the routine must be saved .so that the new function knows where to go after it is completed.
No comments:
Post a Comment