Stack
Stack
/* value definition */
abstract typedef <<eltype>> STACK(eltype);
/* operator definition */
abstract empty(s)
STACK(eltype) s;
postcondition empty == (len(s) == 0);
struct opndstack
{
int items[STACKSIZE];
int top;
};
Prepared by Ramesh singh Saud 27
void main()
{
char postfix[STACKSIZE], ch;
int i, l;
int x;
struct opndstack s;
int op1,op2;
int value;
int result;
s.top=-1;
clrscr();
printf("Enter a valid postfix:");
gets(postfix);
l=strlen(postfix);
for(i=0;i<=l-1;i++)
{
if(isdigit(postfix[i]))
{
x=postfix[i];
push(&s,(int)(x-'0'));
}
Prepared by Ramesh singh Saud 28
Prepared by Ramesh singh Saud 29
Prepared by Ramesh singh Saud 30
Algorithm
1. The infix expression is scanned from left to right one
character at a time.
2. If left parentheses i.e. ‘(’ is encountered, push it to
opstack .
3. If operand is encountered, add operand to postfix string.
4. If operator is encountered, push operator into opstack if the
opstack is empty or if the precedence of the current
operator on top of opstack is smaller than the currently
scanned operator.
5. Whenever right parentheses i.e. ‘)’ is encountered, pop
opstack until a matching left parentheses is found and
cancel both.
6. While opstack is not empty, pop operators from opstack
and add operators to postfix string.
7. Display postfix string.
A A
+ A +
B AB +
* AB +, *
C ABC +, *
ABC* +
ABC*+
( (
A A (
+ A (, +
B AB (, +
) AB+
* AB+ *
C AB+C *
AB+C*
void main()
{
printf(“This is direct recursion. Goes
infinite\n");
main();
}
Prepared by Ramesh singh Saud 40
E.g.
void printline();
void main()
{
printf(“ This is not direct recursion.\n");
printline();
}
void printline()
{
printf("Indirect Recursion. Goes
Infinite\n");
main();
}
Prepared by Ramesh singh Saud 41
To solve a problem using recursive
method, two conditions must be satisfied:
1) Problem should be written or defined in
terms of its previous result.
2) Problem statement must include a
terminating condition, otherwise the
function will never terminate. This means
that there must be an if statement
somewhere in the recursive function to
force the function to return without the
recursive call being executed.
Prepared by Ramesh singh Saud 42
Recursion Iteration
1. A function is called from the definition1. Loops are used to perform repeated
of the same function to do repeated task. task.
2. Recursion is a top-down approach to2. Iteration is a bottom-up approach: it
problem solving: it divides the problem begins from what is known and from
into pieces. this it constructs the solution step-by-
E.g. Computing factorial of a number: step.
long int factorial(int n) E.g. Computing factorial of a number:
{ int fact=1;
if(n==0) return 1; for(i=1;i<=n;i++)
else {
return (n*factorial(n-1)); fact=fact*i;
} }
3. Problem to be solved is defined in terms3. It is not necessary to define a problem
of its previous result to solve a problem in terms of its previous result to solve
using recursion. using iteration. For e.g. “Display your
name 1000 times”
4. In recursion, a function calls to itself 4. In iteration, a function does not call to
until some condition is satisfied. itself.
5. All problems cannot be solved using5. All problems can be solved using
recursion. iteration.
6. Recursion utilizes stack. 6. Iteration
Prepared by Ramesh singh Saud does not utilize stack. 43
Recursion uses stack to keep the successive generations of
local variables and parameters of the function in its
corresponding calls.
This stack is maintained by the C system and is invisible to
the user (programmer).
Each time a recursive function is entered, a new allocation of
its variables is pushed on top of the stack.
Any reference to a local variable or parameter is through the
current top of the stack.
When the function returns, the stack is popped, the top
allocation is freed, and the previous allocation becomes the
current stack top to be used for referencing local variables.
Each time a recursive function returns, it returns to the point
immediately following the point from which it was called.
void main()
{
int pos;
int x;
printf("Enter the position of the nth Fibonacci number:");
scanf("%d", &pos);
x=fibo(pos);
printf("\n The %dth Fibonacci number is:%d", pos, x);
}
2 2
1 1 1 1
•Draw the largest disk node with the largest disk number (disk number starts from 1,
with 1 being the smallest disk number) and put it directly to the destination (A->C).
•Draw its two children with the second largest node number i.e. 2.
•Now A->C can be accomplished only through A->B and B->C, so put A->B as left
child and B->C as right child.
•Again draw two children of second largest node i.e. 2 and think how we can generate
A->B and B->C………………………………..Completed
•Finally perform inorder traversal (left-root-right) of the tree to obtain the appropriate
sequence of steps to solve “Tower of Hanoi”.
•Note: On left side, we always have A and on right side, we always have C.
Prepared by Ramesh singh Saud 59
Write and explain the algorithm for
Tower of Hanoi.