Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tower of Hanoi using Stacks!
- #include<iostream>
- #include<stdlib.h>
- #include <stack>
- #include<algorithm>
- using namespace std;
- void FillTheTower(int disks, stack<int> *source)
- {
- for (int i = disks; i>0; --i) {
- source->push(i);
- }
- }
- void MoveTowerofHanoi(int disk, stack<int> *source, stack<int> *temp, stack<int> *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 disks;
- cout << "Enter the number of disks!" << endl;
- cin >> disks;
- stack<int> *source = new stack<int>;
- FillTheTower(disks, source);
- stack<int> *temp = new stack<int>;
- stack<int> *destination = new stack<int>;
- MoveTowerofHanoi(disks, source, temp, destination);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement