forked from ArsalanKhairani/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTowersOfHanoi.h
58 lines (52 loc) · 1.39 KB
/
TowersOfHanoi.h
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
/*******************************************************************************
* Copyright(c) Arsalan Khairani, 2014 *
* Towers Of Hanoi *
* *
* Recursive algorithm for solving towers of hanoi. Using Linked-list as towers*
*******************************************************************************/
#include <iostream>
#include "ListStack.h"
using namespace std;
ListStack<string> TowerA;
ListStack<string> TowerB;
ListStack<string> TowerC;
int steps = 0;
void DisplayTowers()
{
cout << "\n\n\n\t\t\t\tStep " << steps++;
cout << "\nTower A:\n";
TowerA.DisplayListStack();
cout << "\nTower B:\n";
TowerB.DisplayListStack();
cout << "\nTower C:\n";
TowerC.DisplayListStack();
}
void Solve(int height, ListStack<string>* Beg, ListStack<string>* Aux, ListStack<string>* End)
{
if (height == 1)
{
End->Push(Beg->Pop());
DisplayTowers();
return;
}
Solve(height-1,Beg, End, Aux);
End->Push(Beg->Pop());
DisplayTowers();
Solve(height-1,Aux, Beg, End);
return;
}
void Input()
{
int height;
cout << "\nEnter height of tower: ";
cin >> height;
for (int i=height; i>0; i--)
{
string ent;
for (int j=1; j<=i; j++)
ent += "z";
TowerA.Push(ent);
}
DisplayTowers();
Solve(height, &TowerA, &TowerB, &TowerC);
}