-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTower of Hanoi.c
135 lines (110 loc) · 3.26 KB
/
Tower of Hanoi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Author - Srihari K G
Branch - Computer Science and Engineering
Roll No- 210030035
INDIAN INSTITUTE OF TECHNOLOGY
*/
/* TOWER OF HANOI ASSIGNMENT DSA */
#include <stdlib.h>
#include <stdio.h>
struct stacks
{
int temp, top;
int *stackname;
};
int temp;
struct stacks a, b, c;
int isempty(int stack[], int *top, int size) //checks if particular stack is empty
{
if (*top == -1)
return 1;
else
return 0;
}
int isfull(int stack[], int *top, int size) //checks if particular stack is full
{
if (*top == size - 1)
return 1;
else
return 0;
}
void push(int stack[], int size, int *top, int val, char name, FILE *f) //pushes the element to the stack
{
int n;
if (!isfull(stack, top, size))
{
(*top)++;
stack[(*top)] = val;
fprintf(f, "Push disk %d to Stack %c\n", val, name);
}
else
{
printf("Stack is full \n"); //stack overflow
}
}
void pop(int stack[], int size, int *top, char name, FILE *f) //deletes the element from stack
{
if (!isempty(stack, top, size))
{
fprintf(f, "Pop disk %d from Stack %c\n", stack[(*top)], name);
stack[(*top)] = -1;
(*top)--;
}
else
{
printf("Stack is empty\n"); //stack underflow
}
}
int top(int stack[], int *top, int size) //returns the data stored in the position stack
{
if (!isempty(stack, top, size))
{
return stack[(*top)];
}
}
void toh(int n, int size, int source[], int *source_top, char source_name, int aux[], int *aux_top, char aux_name, int dest[], int *dest_top, char dest_name, FILE *f) //computes the tower of hanoi functionality
{
if (n == 0)
{
return;
}
toh(n - 1, size, source, source_top, source_name, dest, dest_top, dest_name, aux, aux_top, aux_name, f);
int k;
k = top(source, source_top, size);
pop(source, size, source_top, source_name, f);
push(dest, size, dest_top, k, dest_name, f);
toh(n - 1, size, aux, aux_top, aux_name, source, source_top, source_name, dest, dest_top, dest_name, f);
}
int main(int argument_from_command_line, char *array_of_command_line_arguments[])
{
FILE *f;
int n, size;
if (argument_from_command_line != 2)
{
printf("\nEnter properly in arguments");
}
n = atoi(array_of_command_line_arguments[1]);
f = fopen("toh.txt", "w");
if (f == NULL)
{
printf("INVALID FILE");
}
a.top = -1;
b.top = -1;
c.top = -1;
size = n;
a.stackname = (int *)malloc(n * sizeof(int)); //allocation of memory space from heap memory
b.stackname = (int *)malloc(n * sizeof(int));
c.stackname = (int *)malloc(n * sizeof(int));
int i;
for (i = n; i > 0; i--)
{
push(a.stackname, size, &a.top, i, 'A', f);
}
// for (i=0;i<size;i++)
// printf("A[%d] = %d B[%d] = %d C[%d] = %d \n",i,a.stackname[i],i,b.stackname[i],i,c.stackname[i]);
toh(n, size, a.stackname, &a.top, 'A', b.stackname, &b.top, 'B', c.stackname, &c.top, 'C', f);
fclose(f); //closes the file
// for (i=0;i<size;i++)
// printf("A[%d] = %d B[%d] = %d C[%d] = %d \n",i,a.stackname[i],i,b.stackname[i],i,c.stackname[i]);
}