Advertisement
meta1211

Untitled

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