0% found this document useful (0 votes)
141 views

DSA Lab Program - 5

The document describes two C programs: 1) A program to evaluate postfix expressions with single digit operands and basic operators. It uses a stack to evaluate the expression based on operator precedence. 2) A program to solve the Tower of Hanoi problem by moving all disks from the source peg to the destination peg using a temporary peg, as described by the recursive algorithm. It prints the moves and total number of moves required.

Uploaded by

kashafaznain5
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)
141 views

DSA Lab Program - 5

The document describes two C programs: 1) A program to evaluate postfix expressions with single digit operands and basic operators. It uses a stack to evaluate the expression based on operator precedence. 2) A program to solve the Tower of Hanoi problem by moving all disks from the source peg to the destination peg using a temporary peg, as described by the recursive algorithm. It prints the moves and total number of moves required.

Uploaded by

kashafaznain5
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/ 4

Program - 5

5. Develop a Program in C for the following Stack Applications

5a. Evaluation of Suffix expression with single digit operands and


operators: +, -, *, /, %, ^

Algorithm:
Step 1: Start.
Step 2: Read the postfix/suffix expression.
Step 3: Evaluate the postfix expression based on the precedence of the operator.
Step 4: Stop.

Program code:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>

double compute(char symbol, double op1, double op2)


{
switch(symbol)
{
case '+' : return op1+op2;
case '-' : return op1-op2;
case '*' : return op1*op2;
case '/' : return op1/op2;
case '$' :
case '^' :return pow(op1,op2);
default : return 0;
}
}

int main()
{
double s[20],res,op1,op2;
int top=-1,i;
char postfix[20],symbol;
printf("Enter the postfix expression : \n");

1
Program - 5

gets(postfix);
for(i=0;i<strlen(postfix); i++)
{
symbol=postfix[i];
if(isdigit(symbol))
{
s[++top]=symbol-'0';
}
else
{
op2=s[top--];
op1=s[top--];
res= compute(symbol,op1,op2);
s[++top]=res;
}
}
res=s[top--];
printf("\n The result is : %f\n",res);
return 0;
}

Output 1:
Insert a postfix notation: 22^32*+
Result: 10

Output 2:
Insert a postfix notation: 45^98*+
Result: 1096

Output 3:
Insert a postfix notation: 23+
Result: 5

Output 4:
Insert a postfix notation: 13-
Result:

2
Program - 5

5b. Solving Tower of Hanoi problem with n disks.

Algorithm:
Step 1: Start.
Step 2: Read N number of discs.
Step 3: Move all the discs from source to destination by using temp rod.
Step 4: Stop.

Program code:
#include<stdio.h>
#include<math.h>

void tower(int n, int source, int temp, int destination)


{
if(n==0)
return;
tower(n-1, source, destination, temp);
printf("\n Move disc %d from %c to %c \n", n, source, destination);
tower(n-1, temp, source, destination);
}

int main()
{
int n;
printf("Enter the number of discs : \n ");
scanf("%d",&n);
tower(n,'A','B','C');
printf("Total number of disc moves are %d ", (int)pow(2,n)-1);
return 0;
}

Output 1:
Enter the number of disks: 2
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Total numbers of disc moves are 3

3
Program - 5

Output 2:
Enter the number of disks: 3
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
Total numbers of disc moves are 7

Output 3:
Enter the number of disks: 4
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 3 from peg A to peg B
Move disk 1 from peg C to peg A
Move disk 2 from peg C to peg B
Move disk 1 from peg A to peg B
Move disk 4 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 2 from peg B to peg A
Move disk 1 from peg C to peg A
Move disk 3 from peg B to peg C
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Total numbers of disc moves are 15

You might also like