Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int licznik=0;
  10.  
  11.  
  12. int zwroc()
  13. {
  14. int value=rand()%100+101;
  15. return value;
  16. }
  17.  
  18. class lista
  19. {
  20. public:
  21. lista *next;
  22. int value;
  23.  
  24. static void dodaj();
  25. static void show();
  26. static void createeven();
  27. static void addeven(int numer);
  28. static void show_reversed();
  29. static void sorting();
  30. };
  31.  
  32. lista *poczatek=nullptr;
  33. lista *poczatekparz=nullptr;
  34.  
  35.  
  36. void lista::show()
  37. {
  38. lista *pomoc=poczatek;
  39. cout<<"Utworzone elementy: "<<endl;
  40. while(pomoc->next != nullptr)
  41. {
  42. cout<<pomoc->value<<endl;
  43. pomoc=pomoc->next;
  44. }
  45. cout<<pomoc->value<<endl;
  46.  
  47. }
  48.  
  49. void lista::dodaj()
  50. {
  51. lista *kulka=new lista();
  52. licznik++;
  53. kulka->value=zwroc();
  54.  
  55. lista *pomoc=poczatek;
  56.  
  57. if(pomoc==nullptr)
  58. {
  59. poczatek=kulka;
  60. kulka->next=nullptr;
  61. }
  62. else
  63. {
  64. while(pomoc->next != nullptr)
  65. {
  66. pomoc=pomoc->next;
  67. }
  68. pomoc->next=kulka;
  69. kulka->next=nullptr;
  70. }
  71. }
  72.  
  73. void lista::createeven()
  74. {
  75. lista *pomoc=poczatek;
  76. while(pomoc->next != nullptr)
  77. {
  78. if((pomoc->value)%2==0)
  79. {
  80. lista::addeven(pomoc->value);
  81. }
  82. pomoc=pomoc->next;
  83. }
  84. if((pomoc->value)%2==0)
  85. {
  86. lista::addeven(pomoc->value);
  87. }
  88. }
  89.  
  90. void lista::addeven(int numer)
  91. {
  92. lista *kulka=new lista();
  93. kulka->value=numer;
  94. lista *pomoc=poczatekparz;
  95.  
  96. if(pomoc==nullptr)
  97. {
  98. poczatekparz=kulka;
  99. kulka->next=nullptr;
  100. }
  101. else
  102. {
  103. while(pomoc->next != nullptr)
  104. {
  105. pomoc=pomoc->next;
  106. }
  107. pomoc->next=kulka;
  108. kulka->next=nullptr;
  109. }
  110. }
  111.  
  112. void lista::show_reversed()
  113. {
  114. lista *pomoc=poczatek;
  115. int tablica[licznik];
  116. int kolejnosc = 0;
  117.  
  118. while(pomoc->next != nullptr)
  119. {
  120. tablica[kolejnosc]=pomoc->value;
  121. kolejnosc++;
  122. pomoc=pomoc->next;
  123. }
  124. tablica[kolejnosc]=pomoc->value;
  125.  
  126. cout<<"Lista w odwrotnej kolejnosci: "<<endl;
  127. for(int i=licznik-1; i>=0; i--)
  128. {
  129. cout<<tablica[i]<<endl;
  130. }
  131. }
  132.  
  133. void lista::sorting()
  134. {
  135. lista *temp1=poczatek;
  136. lista *temp=nullptr;
  137. if(temp1->next)
  138. if(temp1->next->next)
  139. if(temp1->next->value > temp1->next->next->value)
  140. {
  141. temp1=temp1->next;
  142. temp1->next=temp->next;
  143. temp->next=temp1->next->next;
  144. temp1->next->next=temp;
  145. }
  146. if(temp1->next)
  147. temp1->next->sorting();
  148. }
  149.  
  150.  
  151. int main()
  152. {
  153. srand(time(NULL));
  154.  
  155. for(int i=0; i<10; i++)
  156. {
  157. lista::dodaj();
  158.  
  159. }
  160.  
  161. lista::show();
  162. lista::sorting();
  163. lista::show_reversed();
  164.  
  165.  
  166. return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement