forked from sonumahajan/All_Program_helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTower_of_Hanoi.cpp
154 lines (141 loc) · 3.34 KB
/
Tower_of_Hanoi.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include<iostream>
#include<math.h>
using namespace std;
class stack1{ // self made stack for easy understanding
private:
int arr1[10],top_element_place = -1;
public:
stack1(){
for(int i=0;i<10;i++){
arr1[i] = 0;
}
}
void push(int x){
if(top_element_place < 9){
top_element_place++;
arr1[top_element_place] = x;
}else{
cout << "stack overflow detected !!!" << endl;
}
}
void pop(){
if(top_element_place > -1){
arr1[top_element_place] = 0;
top_element_place--;
}else{
cout << "stack underflow detected !!!" << endl;
}
}
bool is_empty(){
if(top_element_place == -1){
return true;
}else{
return false;
}
}
bool is_full(){
if(top_element_place == 9){
return true;
}else{
return false;
}
}
int count(){
cout << "number of elements in stack is -> " << top_element_place+1 << endl;
return top_element_place+1;
}
int top(){
return arr1[top_element_place];
}
void display(){
for(int i=0;i<top_element_place+1;i++){
cout << arr1[i] << ", ";
}
cout << endl;
}
};
stack1 s1,s2,s3; // stacks declaration
void transferer12(){
int num1 = s1.top();
s2.push(num1);
s1.pop();
}
void transferer13(){
int num1 = s1.top();
s3.push(num1);
s1.pop();
}
void transferer21(){
int num1 = s2.top();
s1.push(num1);
s2.pop();
}
void transferer23(){
int num1 = s2.top();
s3.push(num1);
s2.pop();
}
void transferer31(){
int num1 = s3.top();
s1.push(num1);
s3.pop();
}
void transferer32(){
int num1 = s3.top();
s2.push(num1);
s3.pop();
}
int main(){
int n1,n2;
cout << "Enter the number of discs you want in tower of hanoi (MAX discs is 9) -> ";
cin >> n1;
if(n1>9){
n1 = 9;
}
n2 = pow(2,n1);
for(int i=n1;i>0;i--){
s1.push(i);
}
cout << endl << endl;
cout << "stack 1" << endl;
s1.display();
cout << endl;
cout << "stack 2" << endl;
s2.display();
cout << endl;
cout << "stack 3" << endl;
s3.display();
for(int i=1;i<n2;i++){
if(i%3 == 1){
if(s2.is_empty() || s1.top() < s2.top()){
transferer12();
}else{
transferer21();
}
}
else if(i%3 == 2){
if(s3.is_empty() || s1.top() < s3.top()){
transferer13();
}else{
transferer31();
}
}
else if(i%3 == 0){
if(s3.is_empty() || s3.top() < s2.top()){
transferer32();
}else{
transferer23();
}
}
}
cout << "############################################################" << endl << endl;
cout << "stack 1" << endl;
s1.display();
cout << endl;
cout << "stack 2" << endl;
s2.display();
cout << endl;
cout << "stack 3" << endl;
s3.display();
return 0;
}