TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
tower_of_hanoi.cpp
Go to the documentation of this file.
1
6
#include <iostream>
7
11
struct
tower
{
13
int
values
[10];
15
int
top
;
16
};
17
19
void
show(
const
struct
tower
*
const
F,
const
struct
tower
*
const
T,
20
const
struct
tower
*
const
U) {
21
std::cout <<
"\n\n\tF : "
;
22
for
(
int
i = 0; i < F->
top
; i++) {
23
std::cout << F->
values
[i] <<
"\t"
;
24
}
25
std::cout <<
"\n\tU : "
;
26
for
(
int
i = 0; i < U->
top
; i++) {
27
std::cout << U->
values
[i] <<
"\t"
;
28
}
29
std::cout <<
"\n\tT : "
;
30
for
(
int
i = 0; i < T->
top
; i++) {
31
std::cout << T->
values
[i] <<
"\t"
;
32
}
33
}
34
39
void
mov
(
tower
*From,
tower
*To) {
40
--From->
top
;
41
To->
values
[To->
top
] = From->
values
[From->
top
];
42
++To->
top
;
43
}
44
52
void
TH
(
int
n,
tower
*From,
tower
*Using,
tower
*To) {
53
if
(n == 1) {
54
mov
(From, To);
55
show(From, To, Using);
56
}
else
{
57
TH
(n - 1, From, To, Using);
58
mov
(From, To);
59
show(From, To, Using);
60
TH
(n - 1, Using, From, To);
61
}
62
}
63
65
int
main
() {
66
struct
tower
F, U, T;
67
68
F.
top
= 0;
69
U.
top
= 0;
70
T.
top
= 0;
71
72
int
no;
73
74
std::cout <<
"\nEnter number of discs : "
;
75
std::cin >> no;
76
77
for
(
int
i = no; i > 0; i--) {
78
F.
values
[F.
top
++] = i;
79
}
80
81
show(&F, &T, &U);
82
TH
(no, &F, &U, &T);
83
84
return
0;
85
}
tower
Definition
tower_of_hanoi.cpp:11
tower::values
int values[10]
Values in the tower.
Definition
tower_of_hanoi.cpp:13
tower::top
int top
top tower ID
Definition
tower_of_hanoi.cpp:15
TH
void TH(int n, tower *From, tower *Using, tower *To)
Definition
tower_of_hanoi.cpp:52
main
int main()
Definition
tower_of_hanoi.cpp:65
mov
void mov(tower *From, tower *To)
Definition
tower_of_hanoi.cpp:39
others
tower_of_hanoi.cpp
Generated by
1.12.0