Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. class Samochod{
  7. private:
  8. int cena;
  9. public:
  10. Samochod *left, *right, *up, *next;
  11.  
  12. Samochod()
  13. {
  14. cena = rand() % 100 + 5;
  15. left = right = up = next = NULL;
  16. //cout << "new car. price = " << cena << endl;
  17. }
  18. Samochod* wstaw_l(Samochod *w)
  19. {
  20. if(!this)
  21. return w;
  22. if(w -> cena < this -> cena)
  23. {
  24. w -> next = this;
  25. return w;
  26. }
  27. if(!next)
  28. next = w;
  29. else
  30. if(w -> cena > next -> cena)
  31. next -> wstaw_l(w);
  32. else
  33. {
  34. w -> next = next;
  35. this -> next = w;
  36. }
  37. return this;
  38. }
  39.  
  40. int return_price(){return cena;}
  41.  
  42. void print_l()
  43. {
  44. cout << "a car. price = " << cena << " ;)) " << endl;
  45. if(next)
  46. next -> print_l();
  47. }
  48.  
  49. int size_l(int i = 1)
  50. {
  51. if(next)
  52. i = next -> size_l(++i);
  53. return i;
  54. }
  55.  
  56. Samochod * wstaw_d(Samochod *&w){
  57. if(!this)
  58. return w;
  59. if(w -> cena < this -> cena)
  60. {
  61. if(left)
  62. left -> wstaw_d(w);
  63. else
  64. {
  65. left = w;
  66. //w = w -> next;
  67. // left -> next = NULL;
  68. }
  69. }
  70. else
  71. {
  72. if(right)
  73. right -> wstaw_d(w);
  74. else
  75. {
  76. right = w;
  77. //w = w -> next;
  78. //right -> next = NULL;
  79. }
  80. }
  81. return this;
  82. }
  83.  
  84. Samochod *l_d(Samochod *& root_l) // przenosimy tylko parzysta cene
  85. {
  86.  
  87. Samochod *root_d = NULL;
  88. if(root_l -> cena % 2 == 0)
  89. {
  90. //cout << "e h ";
  91. root_d = root_d -> wstaw_d(root_l);
  92. root_l = root_l -> next;
  93. root_d -> next = NULL;
  94. if(root_l)
  95. {
  96.  
  97. while(root_l -> cena % 2 == 0)
  98. {
  99.  
  100. //Samochod *pom = root_l;
  101. root_d = root_d -> wstaw_d(root_l);
  102.  
  103. root_l = root_l -> next;
  104. //pom -> next = NULL;
  105.  
  106. if(!root_l)
  107. {
  108. cout << "THIS" ;
  109. return this;
  110.  
  111. }
  112. }
  113. if(root_l -> next)
  114. {
  115.  
  116. Samochod * pom = root_l;
  117. Samochod * pom2 = root_l -> next;
  118. while(pom2)
  119. {
  120. if(pom2 -> cena % 2 == 0)
  121. {
  122. pom -> next = pom2 -> next;
  123. pom2 -> next = NULL;
  124. root_d -> wstaw_d(pom2);
  125. pom2 = pom -> next;
  126. }
  127. else
  128. {
  129. pom = pom2;
  130. pom2 = pom2 -> next;
  131. }
  132. }
  133. }
  134. }
  135.  
  136. }
  137. else
  138. {
  139. if(root_l -> next)
  140. {
  141. Samochod *pom = root_l;
  142. Samochod *pom2 = root_l -> next;
  143. while(pom2)
  144. {
  145. //cout << "e h ";
  146.  
  147. if(pom2 -> cena % 2 == 0)
  148. {
  149. pom -> next = pom2 -> next;
  150. pom2 -> next = NULL;
  151. root_d = root_d -> wstaw_d(pom2);
  152. pom2 = pom -> next;
  153. }
  154. else
  155. {
  156. pom = pom2;
  157.  
  158. pom2 = pom -> next;
  159. }
  160. }
  161. }
  162. }
  163. //cout << " lista drzewo korzen " << root_d;
  164.  
  165. return root_d;
  166. }
  167.  
  168. void print_d(){
  169. //cout << " left - " << left << " right - " << right << endl;
  170. if(left)
  171. left -> print_d();
  172.  
  173. cout << " drzewko || cena = " << cena << endl;
  174.  
  175. if(right)
  176. right -> print_d();
  177. }
  178.  
  179. int size_d(int i = 1)
  180. {
  181. if(!this)
  182. return 0;
  183. if(left)
  184. i = left -> size_d(++i);
  185. if(right)
  186. i = right -> size_d(++i);
  187.  
  188. return i;
  189.  
  190. }
  191.  
  192. };
  193.  
  194.  
  195.  
  196. int main(){
  197.  
  198. Samochod *root = NULL;
  199. //cout << "root = " << root -> return_price() << endl;
  200.  
  201. srand(time(NULL));
  202. cout << endl << endl;
  203. for(int i = 0; i < 500; i++)
  204. {
  205. root = root -> wstaw_l(new Samochod);
  206. }
  207. cout << endl << endl;
  208. //cout << "root = " << root -> return_price() << endl;
  209. //cout << endl << "LIST : : LENGTH: " << root -> size_l()<< endl;
  210. //root -> print_l();
  211.  
  212. cout << endl << endl;
  213. for(int i = 0; i < 500; i++)
  214. {
  215. root = root -> wstaw_l(new Samochod);
  216. }
  217.  
  218. cout << endl << endl;
  219. cout << endl << "LIST : : LENGTH: " << root -> size_l() << endl;
  220. root -> print_l();
  221.  
  222. Samochod * root_d = NULL;
  223. root_d = root_d -> l_d(root);
  224. cout << " lista po drzewie i jej size = " << root_d -> size_d()<< endl;
  225.  
  226.  
  227. cout << endl << endl;
  228.  
  229. root_d -> print_d();
  230. cout << " lista po drzewie i jej size = " << root -> size_l()<< endl;
  231.  
  232. root -> print_l();
  233.  
  234.  
  235. return 0;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement