-
Stacks are used mainly in following applications :
1. Function Calling
Programs compiled from high level languages make use of a stack
for the working memory of eachfunction or procedure calling. When
any procedure or function is called, function parameters andaddress
is pushed into the stack and whenever procedure or function
returns, parameters are popped.As a function calls other function,
first its argument, then the return address and finally space for
localvariables is pushed onto a stack. A function call itself, is
known as recursion.
Function a(){ if (condition)
return . .. return b(); }Function b(){ if (condition)
return . .. return a(); }
Note how all the function a and b's environment are found in the
stack. When a is called a secondtime from b, a new set of
parameters is pushed in the stack on invocation of a.
The stack is a region of main memory within which programs
temporarily store data as theyexecute. For example, when a program
sends parameters to a function, the parameters are placed onthe
stack. When the function executes, these parameters are popped from
the stack. When a functioncalls other functions, current contents
of the caller function are pushed onto the stack with the addressof
the instruction just next to the call instruction, this is done so
that after the execution of calledfunction, the compiler can track
back the path from where it is sent to the called function.
Applications of Stack
-
2. To convert an infix expression to postfix
In high level languages, infix notation cannot be used to
evaluate expressions. We must analyze theexpression to determine
the order in which we evaluate it. A common technique is to convert
an infixnotation into postfix notation, then evaluating it.
o Prefix: + a bo Infix: a + bo Postfix: a b +
Infix to postfix conversion can be done easily using stack.
Following are the rules for the infix to postfix conversion of
an arithmetic expression:
Rules:o Operands immediately go directly to outputo Operators
are pushed into the stack (including parenthesis)o Check to see if
stack top operator is less than current operator (Priority of the
operator)o If the top operator is less than, push the current
operator onto stacko If the top operator is greater than the
current, pop top operator and push onto stack,
push current operator onto stacko Priority 2: * /o Priority 1: +
-o Priority 0: (
If we encounter a right parenthesis, pop from stack until we get
matching left parenthesis. Do notoutput parenthesis.
Example 1 :
A + B * C - D / E
Infix Stack(bot->top) Postfixa) A + B * C - D / Eb) + B * C -
D / E Ac) B * C - D / E + Ad) * C - D / E + A Be) C - D / E + * A
Bf) - D / E + * A B C
g) D / E + - A B C *
-
h) / E + - A B C * Di) E + - / A B C * Dj) + - / A B C * D Ek) A
B C * D E / - +
Example 2 :
A * B - ( C + D ) + E
Infix Stack(bot->top) Postfixa) A * B - ( C - D ) + E empty
emptyb) * B - ( C + D ) + E empty Ac) B - ( C + D ) + E * Ad) - ( C
+ D ) + E * A Be) - ( C + D ) + E empty A B *f) ( C + D ) + E - A B
*g) C + D ) + E - ( A B *h) + D ) + E - ( A B * Ci) D ) + E - ( + A
B * Cj) ) + E - ( + A B * C Dk) + E - A B * C D +l) + E empty A B *
C D + -m) E + A B * C D + -n) + A B * C D + - Eo) empty A B * C D +
- E +
3. To evaluate a postfix expression
The expression may be evaluated by making a left to right scan,
stacking operands and evaluatingoperators using the correct number
of operands from the stack and finally placing back the result onto
the stack.
Algorithm for the evaluation of postfix expression :
1. Scan arithmetic expression from left to right and repeat step
2 and 3 until complete expressionis processed
2. If an operand is found push it into stack
3. If an operator is foundi. Remove two top elements of stack,
where X is top element and Y is next-to-topelement.ii. Evaluate X
operator Yiii. Place the result of step 3(ii) into stack at top
-
Example :
Postfix Expression : 1 2 3 + *
Postfix Stack( bot -> top )
a) 1 2 3 + *
b) 2 3 + * 1
c) 3 + * 1 2
d) + * 1 2 3
e) * 1 5 // 5 from 2 + 3
f) 5 // 5 from 1 * 5