Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4.  
  5. class Kulka
  6. {
  7. int numerT, numerL;
  8. public:
  9. Kulka* next;
  10. Kulka* right;
  11. Kulka* left;
  12. Kulka(int i)
  13. {
  14. numerT = i;
  15. numerL = rand() % 100;
  16. left = right = next = NULL;
  17. }
  18.  
  19.  
  20. void wstaw_l(Kulka* w)
  21. {
  22. if (next == NULL)next = w;
  23. else
  24. {
  25. if (next->numerL > w->numerL)
  26. {
  27. w->next = next;
  28. next = w;
  29. numerT++;
  30. }
  31. else
  32. {
  33. next->wstaw_l(w);
  34. }
  35. }
  36. }
  37. void wstaw_d(Kulka* w)
  38. {
  39. if (numerL > w->numerL)
  40. {
  41. if (left)left->wstaw_d(w);
  42. else left = w;
  43. }
  44. else
  45. {
  46. if (right)right->wstaw_d(w);
  47. else right = w;
  48. }
  49. }
  50.  
  51. void wyswietl_l()
  52. {
  53. Kulka* pom = this;
  54. while (pom)
  55. {
  56. cout << "Lista " << " numerL " << pom->numerL << " numerT " << pom->numerT << endl;
  57. pom = pom->next;
  58. }
  59. }
  60. void wyswietl_d()
  61. {
  62. if (left)left->wyswietl_d();
  63. cout << "Drzewo" << " numerL " << numerL << " numerT " << numerT << endl;
  64. if (right)right->wyswietl_d();
  65. }
  66. int return_numerL() { return numerL; }
  67.  
  68. };
  69. int main()
  70. {
  71. srand(time(NULL));
  72. Kulka* r_l_P = NULL;
  73. Kulka* r_l_N = NULL;
  74.  
  75. for (int i = 0; i < 10; i++)
  76. {
  77. Kulka* w = new Kulka(i);
  78. if (w->return_numerL() % 2 == 0)
  79. {
  80. if(!r_l_P )r_l_P = w;
  81. else
  82. {
  83. if (r_l_P->return_numerL() > w->return_numerL())
  84. {
  85. w->next = r_l_P;
  86. r_l_P = w;
  87. }
  88. else
  89. {
  90. r_l_P->wstaw_l(w);
  91. }
  92. }
  93.  
  94. }
  95. else
  96. {
  97. if (!r_l_N)r_l_N = w;
  98. else
  99. {
  100. if (r_l_N->return_numerL() > w->return_numerL())
  101. {
  102. w->next = r_l_N;
  103. r_l_N= w;
  104. }
  105. else
  106. {
  107. r_l_N->wstaw_l(w);
  108. }
  109. }
  110. }
  111. }
  112.  
  113. r_l_N->wyswietl_l();
  114. cout << "Parzysta ::" << endl;
  115. r_l_P->wyswietl_l();
  116.  
  117. Kulka* r_d = NULL;
  118.  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement