Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "Nodes.h"
  5. #include <windows.h>
  6.  
  7. using namespace std;
  8.  
  9. class Stack{
  10. private:
  11. ListNode *first;
  12. public:
  13. Stack(){
  14. first = NULL;
  15. }
  16. ListNode* getFirst(){
  17. return(first);
  18. }
  19.  
  20. void Insert(int v) {
  21. ListNode *temp,*curr;
  22. if(first == NULL){
  23. first = new ListNode(v,NULL);
  24. }
  25. else {
  26. curr=first;
  27. while(curr->getNext()!=NULL) {
  28. curr=curr->getNext();
  29. }
  30. curr->setNext(new ListNode(v,NULL));
  31. }
  32. }
  33. void Remove(){
  34. int v;
  35. ListNode *temp,*curr,*temp1;
  36. curr = first;
  37. while(curr->getNext() !=NULL) {
  38. curr=curr->getNext();
  39. }
  40. curr->setNext(NULL);
  41. }
  42. };
  43.  
  44.  
  45. class Tower{
  46. private:
  47. Stack s1, s2, s3;
  48. ListNode *curr;
  49. public:
  50. Tower(int v){
  51. for(int i = 0;i<v;i++){
  52. s1.Insert(v-i);}
  53. if(v==1){move1(s1, s2);}
  54. if(v==2){move2(s1, s2, s3);}
  55. if(v==3){move3(s1, s2, s3);}
  56. if(v==4){move4(s1, s2, s3);}
  57. if(v==5){move5(s1, s2, s3);}
  58.  
  59. }
  60.  
  61.  
  62. void move1(Stack&s, Stack&e){
  63. int temp;
  64. ListNode *curr= s.getFirst();
  65. while(curr->getNext() !=NULL) {
  66. curr=curr->getNext();
  67. }
  68. temp = curr->getData();
  69. s.Remove();
  70. e.Insert(temp);
  71. print();
  72. cout<<endl;
  73.  
  74. Sleep(1000);
  75. }
  76.  
  77. void move2(Stack s,Stack e,Stack t){
  78. move1(s,t);
  79. move1(s,e);
  80. move1(t,e);
  81. }
  82. void move3(Stack s,Stack e,Stack t){
  83. move2(s,t,e);
  84. move1(s,e);
  85. move2(t,e,s);
  86. }
  87. void move4(Stack s,Stack e,Stack t){
  88. move3(s,t,e);
  89. move1(s,e);
  90. move3(t,e,s);
  91. }
  92. void move5(Stack s,Stack e,Stack t){
  93. move4(s,t,e);
  94. move1(s,e);
  95. move4(t,e,s);
  96. }
  97.  
  98.  
  99. void print(){
  100. cout<<"S1:";
  101. curr = s1.getFirst();
  102. while(curr!=NULL){
  103. cout<<curr->getData()<<" ";
  104. curr = curr->getNext();
  105. }cout<<endl;
  106. cout<<"S2:";
  107. curr = s2.getFirst();
  108. while(curr!=NULL){
  109. cout<<curr->getData()<<" ";
  110. curr = curr->getNext();
  111. }cout<<endl;
  112. cout<<"S3:";
  113. curr = s3.getFirst();
  114. while(curr!=NULL){
  115. cout<<curr->getData()<<" ";
  116. curr = curr->getNext();
  117. }
  118.  
  119. }
  120.  
  121.  
  122.  
  123. };
  124.  
  125. int main(){
  126. int Size,v;
  127. cout<<"Enter The Tower Size: ";
  128. cin>>Size;
  129. Tower t(Size);
  130. t.print();
  131.  
  132. return 0;
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement