Advertisement
meta1211

Hanoi towers

Apr 27th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. //Tower of Hanoi using Stacks!
  2. #include<iostream>
  3. #include<stdlib.h>
  4. #include <stack>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. void FillTheTower(int disks, stack<int> *source)
  9. {
  10. for (int i = disks; i>0; --i) {
  11. source->push(i);
  12. }
  13. }
  14.  
  15. void MoveTowerofHanoi(int disk, stack<int> *source, stack<int> *temp, stack<int> *destination)
  16. {
  17. if (disk == 1)
  18. {
  19. destination->push(source->top());
  20. source->pop();
  21. }
  22. else
  23. {
  24. MoveTowerofHanoi(disk - 1, source, destination, temp);
  25. destination->push(source->top());
  26. source->pop();
  27. MoveTowerofHanoi(disk - 1, temp, source, destination);
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. int disks;
  34. cout << "Enter the number of disks!" << endl;
  35. cin >> disks;
  36. stack<int> *source = new stack<int>;
  37. FillTheTower(disks, source);
  38. stack<int> *temp = new stack<int>;
  39. stack<int> *destination = new stack<int>;
  40. MoveTowerofHanoi(disks, source, temp, destination);
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement