0% found this document useful (0 votes)
248 views4 pages

Implement The Tower of Hanoi Problem in C Using Stack

Uploaded by

aryakshat3
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)
248 views4 pages

Implement The Tower of Hanoi Problem in C Using Stack

Uploaded by

aryakshat3
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

Question) Implement the Tower of Hanoi problem in C using Stack.

You should
properly enumerate the atomic operations/steps. Vary the number of discs to be
shifted.

#include <stdio.h>

void prnt(int n, int src[], int aux[], int dst[]);


void move(int n, int src[], int dst[], char s, char d, int aux[], int
other[]);
void hanoi(int n, int src[], int aux[], int dst[], char s, char a, char
d);

void prnt(int n, int src[], int aux[], int dst[]) {


printf("A: ");
for (int i = 0; i < n; i++)
if (src[i] != 0) printf("%d ", src[i]);
printf("\n");

printf("B: ");
for (int i = 0; i < n; i++)
if (aux[i] != 0) printf("%d ", aux[i]);
printf("\n");

printf("C: ");
for (int i = 0; i < n; i++)
if (dst[i] != 0) printf("%d ", dst[i]);
printf("\n\n");
}

void move(int n, int src[], int dst[], char s, char d, int aux[], int
other[]) {
int i = 0;
while (i < n && src[i] == 0) i++;
if (i == n) return;

int dsk = src[i];


src[i] = 0;

int j = 0;
while (j < n && dst[j] == 0) j++;
if (j == 0) dst[j] = dsk;
else dst[j - 1] = dsk;

printf("Move %d from %c to %c\n", dsk, s, d);


prnt(n, src, aux, dst);
}

void hanoi(int n, int src[], int aux[], int dst[], char s, char a, char d)
{
if (n == 1) {
move(n, src, dst, s, d, aux, src);
return;
}

hanoi(n - 1, src, dst, aux, s, d, a);


move(n, src, dst, s, d, aux, src);
hanoi(n - 1, aux, src, dst, a, s, d);
}

int main() {
int n;
printf("Enter num of disks: ");
scanf("%d", &n);

int src[n], aux[n], dst[n];


for (int i = 0; i < n; i++) {
src[i] = n - i;
aux[i] = 0;
dst[i] = 0;
}

printf("\nInit State:\n");
prnt(n, src, aux, dst);
hanoi(n, src, aux, dst, 'A', 'B', 'C');
return 0;
}
Write a program to evaluate postfix expressions using Stack in C. Here is an
example: 12, 7, 3, -, /, 2, 1, 5, +, *, +, $ ; treat $ as the end of input.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
typedef struct {
int arr[MAX];
int top;
} Stack;
void push(Stack *s, int val) {
if (s->top == MAX - 1) {
printf("Stack overflow\n");
return;
}
s->arr[++(s->top)] = val;
}
int pop(Stack *s) {
if (s->top == -1) {
printf("Stack underflow\n");
exit(1);
}
return s->arr[(s->top)--];
}
int evaluatePostfix(char *expr) {
Stack s;
[Link] = -1;
char *token = strtok(expr, ",");
while (token) {
while (*token == ' ') token++; // Skip leading spaces
if (*token == '$') break; // Stop processing when encountering $
if (isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
push(&s, atoi(token));
} else if (*token == '+' || *token == '-' || *token == '*' || *token ==
'/') {
int b = pop(&s);
int a = pop(&s);
switch (*token) {
case '+': push(&s, a + b); break;
case '-': push(&s, a - b); break;
case '*': push(&s, a * b); break;
case '/':
if (b == 0) {
printf("Error: Division by zero\n");
exit(1);
}
push(&s, a / b);
break;
}
}
token = strtok(NULL, ",");
}
return pop(&s);
}
int main() {
char expr[100];
printf("Enter postfix expression (comma-separated): ");
scanf("%[^\n]", expr);
printf("Result: %d\n", evaluatePostfix(expr));
return 0;
}

You might also like