Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include"stack.h"
- using namespace std;
- void FillTheTower(int disks, Stack *source)
- {
- for (int i = disks; i>0; --i) {
- source->push(i);
- }
- }
- void MoveTowerofHanoi(int disk, Stack *source, Stack *temp, Stack *destination)
- {
- if (disk == 1)
- {
- destination->push(source->top());
- source->pop();
- }
- else
- {
- MoveTowerofHanoi(disk - 1, source, destination, temp);
- destination->push(source->top());
- source->pop();
- MoveTowerofHanoi(disk - 1, temp, source, destination);
- }
- }
- int main()
- {
- int diskCount;
- cout << "Disk number:" << endl;
- cin >> diskCount;
- Stack *source = new Stack;
- FillTheTower(diskCount, source);
- Stack *temp = new Stack;
- Stack *destination = new Stack;
- cout << "Source:" << endl;
- source->print();
- cout << "Destination:" << endl;
- destination->print();
- MoveTowerofHanoi(diskCount, source, temp, destination);
- cout << "Source:" << endl;
- source->print();
- cout << "Destination:" << endl;
- destination->print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement