Lab program-5(b)
Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit
operands and operators: +, -, *, /, %, ^
b. Solving Tower of Hanoi problem with n disks
// Towers of Hanoi
#include <stdio.h>
void towers(int n, char S, char AUX, char D);
void main()
{
int n;
printf("Enter the number of disks : ");
scanf("%d", &n);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(n, 'A', ‘B', ‘C');
}
void towers(int n, char S, char AUX, char D)
{
if (n == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", S, D);
return;
}
towers(n- 1, S, D,AUX);
printf("\n Move disk %d from peg %c to peg %c", n, S,D);
towers(n - 1, AUX, S,D);
}
#include <stdio.h>
void towers(int n, char S, char AUX, char D);
void main()
{
int n;
printf("Enter the number of disks : ");
scanf("%d", &n);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(n, 'A', ‘B', ‘C');
}
void towers(int n, char S, char AUX, char D)
{
if (n == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", S, D);
return;
}
towers(n- 1, S, D,AUX);
printf("\n Move disk %d from peg %c to peg %c", n, S,D);
towers(n - 1, AUX, S,D);
}