You are on page 1of 4

Applications of Stack

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 each
function or procedure calling. When any procedure or function is called, function parameters and
address 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 local
variables 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 second
time 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 they
execute. For example, when a program sends parameters to a function, the parameters are placed on
the stack. When the function executes, these parameters are popped from the stack. When a function
calls other functions, current contents of the caller function are pushed onto the stack with the address
of the instruction just next to the call instruction, this is done so that after the execution of called
function, the compiler can track back the path from where it is sent to the called function.

2.

To convert an infix expression to postfix

In high level languages, infix notation cannot be used to evaluate expressions. We must analyze the
expression to determine the order in which we evaluate it. A common technique is to convert an infix
notation into postfix notation, then evaluating it.
o Prefix: + a b
o Infix:
a+b
o 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 output

Operators are pushed into the stack (including parenthesis)

Check to see if stack top operator is less than current operator (Priority of the operator)

If the top operator is less than, push the current operator onto stack

If the top operator is greater than the current, pop top operator and push onto stack,

push current operator onto stack


o

Priority 2: * /

Priority 1: + -

Priority 0: (

If we encounter a right parenthesis, pop from stack until we get matching left parenthesis. Do not
output parenthesis.

Example 1 :
A+B*C-D/E
Infix

Stack(bot->top)

Postfix

a) A + B * C - D / E
b) + B * C - D / E

c)

B*C-D/E

d)

*C-D/E

AB

e)

C-D/E

+*

AB

f)

-D/E

+*

ABC

g)

D/E

+-

ABC*

h)

/E

+-

ABC*D

i)

+-/

ABC*D

j)

+-/

ABC*DE

k)

ABC*DE/-+

Example 2 :
A* B - ( C + D ) + E
Infix

Stack(bot->top)

Postfix

a)

A*B-(C-D)+E

empty

empty

b)

*B-(C+D)+E

empty

c)

B-(C+D)+E

d)

-(C+D)+E

AB

e)

-(C+D)+E

empty

AB*

f)

(C+D)+E

AB*

g)

C+D)+E

-(

AB*

h)

+D)+E

-(

AB*C

i)

D)+E

-(+

AB*C

j)

)+E

-(+

AB*CD

k)

+E

AB*CD+

l)

+E

empty

AB*CD+-

m)

AB*CD+-

n)

AB*CD+-E

o)

empty

AB*CD+-E+

3.

To evaluate a postfix expression

The expression may be evaluated by making a left to right scan, stacking operands and evaluating
operators using the correct number of operands from the stack and finally placing back the result on
to 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 expression
is processed

2.

If an operand is found push it into stack

3.

If an operator is found
i.
Remove two top elements of stack, where X is top element and Y is next-to-top
element.
ii.
Evaluate X operator Y
iii.
Place the result of step 3(ii) into stack at top

Example :
Postfix Expression : 1 2 3 + *
Postfix

Stack( bot -> top )

a)

123+*

b)

23+*

c)

3+*

d)

+*

e)

f)

1
12
123
15
5

// 5 from 2 + 3
// 5 from 1 * 5

You might also like