Stack Applications v2 05022017
Stack Applications v2 05022017
Stacks
Applications of Stacks
Stacks are frequently used in a number of
computer applications like
expression evaluation,
Expression can be represented in:
infix
prefix(polish)
postfix(reverse polish)
recursive function calls,(all instances
of the functions are pushed onto stack).
implementing storage area for automatic
variables etc.
Polish Notation(Prefix
Notation)
Its named after Polish mathematician
Jan Lukasiewicz,refers to the notation
in which the operator is placed before
the two operands.
No need of writing parenthesis
Example: +AB,-CD,*EF etc.
Infix- (A+B), (C-D), (E*F)
Example: Evaluate the infix
expressions in polish notation-
(A+B) *C = (+AB)*C=*+ABC
A+(B*C)=A+(*BC)= +A*BC
(A+B)/(C-D)=(+AB)/(-CD)=/+AB-CD
Reverse Polish Notation
(Postfix Notation)
In this the operator is placed after
its two operands.
No need of writing parenthesis
Example: AB+,CD-,EF*,GH/
Precedence:
Exponentiation(^)
Multiplication(*)
and Division(/)
Addition(+) and Subtraction(-)
Note:
The computer usually evaluates
an arithmetic expression written
in infix notation in two steps:
◦ It converts the expression to postfix
notation
◦ It evaluates the postfix expression.
In each step, stack is the main tool
that is used to accomplish the given
task.
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 it is easier to
carry out a postfix expression than an
infix expression.
Evaluation of Postfix
Expression
Algorithm:
void main(){
int fact=0;
clrscr();
fact=factorial(5);
printf("\n factorial of 5 is %d",fact);
getch();
}