0% found this document useful (0 votes)
8 views20 pages

Stack Applications v2 05022017

Stacks are essential in various computer applications such as expression evaluation, recursive function calls, and managing automatic variables. They facilitate the conversion and evaluation of expressions in different notations like infix, postfix, and prefix, with postfix notation being more efficient for machines due to its inherent operator precedence. Additionally, recursion utilizes stacks to maintain function calls, though it can lead to increased memory usage and potential inefficiencies.

Uploaded by

helper bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views20 pages

Stack Applications v2 05022017

Stacks are essential in various computer applications such as expression evaluation, recursive function calls, and managing automatic variables. They facilitate the conversion and evaluation of expressions in different notations like infix, postfix, and prefix, with postfix notation being more efficient for machines due to its inherent operator precedence. Additionally, recursion utilizes stacks to maintain function calls, though it can lead to increased memory usage and potential inefficiencies.

Uploaded by

helper bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Applications of

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:

1. Add a right parenthesis “)” at the end of Postfix


Expression P.
2. Scan P from left to right and repeat steps 3 and
4 for each element of P until “)” is encountered.
3. If an operand is encountered,put it on stack.
4. If an operator is encountered,then:
a) remove the top 2 elements of STACK where A is the
top element and B is the next to top element
b) evaluate B and A
c) Place the result on stack.

5. Set final value equal to top element on stack.


6. Exit
Example
Examples-Do It yourself
P:5,6,2,+,*,12,4,/,-
Answer: 37
7 8 + 3 2 + /
Answer:3
12,7,3,-,/,2,1,5,+,*,+
Answer:15
Conversion from Infix To
Postfix
Algorithm
Let, X is an arithmetic expression written in infix notation.
This algorithm finds the equivalent postfix expression Y.
1. Push “(“onto Stack, and add “)” to the end of X.
2. Scan X from left to right and repeat Step 3 to 6 for each
element of X until the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the
top of Stack) which has the same precedence as or higher
precedence than operator.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the
top of Stack) until a left parenthesis is encountered.
2. Remove the left Parenthesis.
[End of If]
[End of If]
Example: A+ (B*C-(D/E^F)*G)*H, where ^ is an
exponential operator.
More Examples: Do it
Yourself
(a+b)*c+(d-a)
◦ Answer: ab+c*da-+
3+4*5/6
◦ Answer:3 4 5 * 6 / +
(300+23)*(43-21)/(84+7)
◦ Answer: 300 23 + 43 21 - * 84 7 + /
(4+8)*(6-5)/((3-2)*(2+2))
◦ Answer:4 8 + 6 5 - * 3 2 – 2 2 + * /
Recursion
When function is called within the
same function, it is known
as recursion in C. The function
which calls the same function, is
known as recursive function.
Data structure maintained in
case of recursion is stack.
Syntax:
Rules for Recursive
function
Only the user-defined function can be involved
in recursion.Library functions cannot be
involved in recursion because their source
code cannot be viewed.
A recursive function saves return address(in
stack)with the intention to return at proper
location when return to a calling function is
made.
To stop the recursive function,it is necessary to
base the recursion on test condition,and
proper terminating statement such as exit() or
return must be written using the if() statement.
The user-defined function main() can be
invoked recursively.
Advantages of Recursion
Although most problems can be
solved without recursion,but in
some situations,it is must to use
recursion.For eg, a program to
display a list of all files of the
system cannot be solved without
recursion.
Using recursion,the length of a
program can be reduced.
Disadvantages of
Recursion
Requires extra storage space.For
every recursive call,separate
memory is allocated to variables
with the same name.
If the programmer forgets to
specify the exit condition in the
recursive function,the program
will execute out of memory.
Not efficient in execution speed
and time.
Example
#include<stdio.h>
#include<conio.h>
int factorial (int n)
{
if (n == 1)
return 1; /*Terminating condition*/
return (n * factorial (n -1));
}

void main(){
int fact=0;
clrscr();
fact=factorial(5);
printf("\n factorial of 5 is %d",fact);
getch();
}

You might also like