0% found this document useful (0 votes)
92 views3 pages

Application of Stack

The document discusses recursion and evaluation of expressions using stacks. It provides an example of a recursive factorial function to calculate n!. It also discusses infix, prefix, and postfix notation for expressions and how to evaluate postfix expressions using a stack. Specifically, it involves scanning the expression from left to right, pushing operands to the stack and popping operands to perform operations, pushing the results back to the stack until the final answer is reached. The document proposes writing a program to directly input a postfix expression, evaluate it using a stack, and print the result.

Uploaded by

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

Application of Stack

The document discusses recursion and evaluation of expressions using stacks. It provides an example of a recursive factorial function to calculate n!. It also discusses infix, prefix, and postfix notation for expressions and how to evaluate postfix expressions using a stack. Specifically, it involves scanning the expression from left to right, pushing operands to the stack and popping operands to perform operations, pushing the results back to the stack until the final answer is reached. The document proposes writing a program to directly input a postfix expression, evaluate it using a stack, and print the result.

Uploaded by

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

Applications of stack

Recursion:
n! = n * (n-1)!
If n=0 || n=1, n! = 1.
- It needs to use itself
- It has to end i.e. converge to termination
- It should limit towards termination condition.
Int fact (int n)
{
Int res;
If (n==0 || n==1)
Return 1;
Else
{
Res = n * fact (n-1);
Return res;
}
}
Evaluation of Expressions:
Infix Expressions:
3 + 4 infix expression (where operator is in between two
operands)
34+ - postfix expression (where operator is after operands)
+34 prefix expression (where operator is before operands)
3 + 4 * 5 / 6
= (3 + ((4 * 5) / 6))
= 3 + (4 5 *) / 6
= 3 + ( 4 5 * 6 / )
= ( 3 4 5 * 6 / + )
1. First convert the entire expression in postfix
2. Evaluate the postfix expression in simple order scanning
the expression only once.

Evaluation of postfix expression
3 4 5 * 6 / +
1. Start scanning the expression from left to right
a. When operand, push it to stack
b. When operator, pop two elements from stack,
perform the operation with that operator, push back
the result to stack.
2. Repeat the above step till end of expression. Answer is in
stack.

Expression stack
3 4 5 * 6 / + 3
4 5 * 6 / + 3, 4
5 * 6 / + 3, 4, 5
* 6 / + 3, 20
6 / + 3, 20, 6
/ + 3, 3
+ 6
NULL 6
Write a program which inputs directly the postfix expression,
evaluates and prints the result. That will be stored in array.
Just a function for evaluation needs to be written.

You might also like