Advertisement
Paszta

Lista Jednokierunkowa

Jun 8th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. struct element {
  8. int data;
  9. element* next;
  10. };
  11. struct lista {
  12. element* head;
  13. element* tail;
  14. int el_count;
  15. };
  16.  
  17. lista create_list ();
  18. void add_tail(lista &l, int r_d);
  19. void add_head(lista &l, int r_d);
  20. void add_position(lista &l, int r_d, int posit);
  21. void delete_tail(lista &l);
  22. void delete_head(lista &l);
  23. void delete_position(lista &l, int posit);
  24. void delete_list(lista &l);
  25. void show_list(lista l);
  26. void show_on_position(lista l, int posit);
  27.  
  28.  
  29. int main()
  30. {
  31.     lista l = create_list();
  32.     int wybor, posit;
  33.     cout << "Podaj pozycje" << endl;
  34.     cin >> posit;
  35.     srand(time(NULL));
  36.     do{
  37. cout << "Podaj wybor:" << endl;
  38. cout << "1. Dodawanie elementu o losowej wartosci na koniec listy " << endl;
  39. cout << "2. Dodawanie elementu o losowej wartosci na poczatek listy " << endl;
  40. cout << "3. Dodawanie elementu o losowej wartosci na pozycje o podanym numerze " << endl;
  41. cout << "4. Usuniecie elementu z konca listy " << endl;
  42. cout << "5. Usuniecie elementu z poczatku listy" << endl;
  43. cout << "6. Usuniecie elementu z podanej pozycji" << endl;
  44. cout << "7. Wyswietlenie elementu o podanym numerze" << endl;
  45. cout << "8. Wyswietlenie calej listy" << endl;
  46. cout << "9. Usuniecie wszystkich elementow listy wraz ze zwolnieniem pamieci" << endl;
  47. cout << "10. Wyjscie" << endl;
  48. cout << "Odpowiedz:" << endl;
  49. cin >> wybor;
  50. switch(wybor){
  51. case 1: add_tail(l, rand()); break;
  52. case 2: add_head(l, rand()); break;
  53. case 3: add_position(l, rand(), posit); break;
  54. case 4: delete_tail(l); break;
  55. case 5: delete_head(l); break;
  56. case 6: delete_position(l, posit); break;
  57. case 7: show_on_position(l, posit); break;
  58. case 8: show_list(l); break;
  59. case 9: delete_list(l); break;
  60. case 10: cout << "Wyszedles z progrmau" << endl;  break;
  61. default : cout << "bledny wybor" << endl; break;
  62.  
  63.  
  64. }
  65.     } while (wybor != 10);
  66.     return 0;
  67. }
  68.  
  69. lista create_list (){
  70. lista new_list;
  71. new_list.head = nullptr;
  72. new_list.tail = nullptr;
  73. new_list.el_count = 0;
  74. return new_list;}
  75.  
  76. void add_tail(lista &l, int r_d){
  77. cout << "Lista biezaca:" << endl;
  78. show_list(l);
  79. cout << "Numer ktory zostanie dodany: " << r_d << endl;
  80. element* n_tail = new element;
  81. n_tail -> data = r_d;
  82. n_tail -> next = nullptr;
  83.  
  84. if(l.tail != nullptr)
  85.     l.tail -> next = n_tail;
  86. else
  87.     l.head = n_tail;
  88. l.tail = n_tail;
  89. l.el_count ++;
  90. cout << "Lista po zmianie:" << endl;
  91. show_list(l);
  92. }
  93.  
  94. void add_head(lista &l, int r_d){
  95. cout << "Lista biezaca:" << endl;
  96. show_list(l);
  97. cout << "Numer ktory zostanie dodany: " << r_d << endl;
  98. element* n_head = new element;
  99. n_head -> data = r_d;
  100. n_head -> next = l.head;
  101. l.head = n_head;
  102.  
  103. if(l.tail == nullptr)
  104.     l.tail == n_head;
  105. l.el_count ++;
  106. cout << "Lista po zmianie:" << endl;
  107. show_list(l);
  108. }
  109.  
  110. void add_position(lista &l, int r_d, int posit){
  111. cout << "Lista biezaca:" << endl;
  112. show_list(l);
  113. cout << "Numer ktory zostanie dodany: " << r_d << endl;
  114. if(posit <0 or posit > l.el_count) return;
  115. if(posit == 0)
  116.     {
  117.     add_head(l, r_d);
  118.     return;
  119.     }
  120. if(posit == l.el_count)
  121.     {
  122.     add_tail(l, r_d);
  123.     return;
  124.     }
  125. element* temp = l.head;
  126. for(int i=0; i < posit -1; i++)
  127.     temp = temp -> next;
  128. element* n_e = new element;
  129. n_e -> data = r_d;
  130. n_e -> next = temp -> next;
  131. temp -> next = n_e;
  132. l.el_count ++;
  133. cout << "Lista po zmianie:" << endl;
  134. show_list(l);
  135. }
  136.  
  137.  
  138. void delete_tail(lista &l){
  139. cout << "Lista biezaca:" << endl;
  140. show_list(l);
  141. if(l.tail == nullptr) return;
  142. element* temp = l.tail;
  143. if(l.el_count == 1)
  144.     {
  145.     l.tail == nullptr;
  146.     l.head == nullptr;
  147.     }
  148. else{
  149. element * tempor = l.head;
  150. for (int i =1; i< l.el_count -1; i++)
  151.     {
  152.     tempor = tempor -> next;
  153.     }
  154. l.tail = tempor;
  155. l.tail -> next = nullptr;
  156.     }
  157. l.el_count --;
  158. delete temp;
  159. cout << "Lista po zmianie:" << endl;
  160. show_list(l);
  161. }
  162.  
  163. void delete_head(lista &l){
  164. if(l.tail == nullptr) return;
  165. cout << "Lista biezaca:" << endl;
  166. show_list(l);
  167. element* temp = l.head;
  168. l.head = l.head -> next;
  169. delete temp;
  170. if(l.head  == nullptr)
  171.     l.tail = nullptr;
  172. cout << "Element z poczatku zostal usuniety" << endl;
  173. l.el_count--;
  174. cout << "Lista po zmianie:" << endl;
  175. show_list(l);
  176. }
  177.  
  178. void delete_position(lista &l, int posit){
  179. if (posit < 0 or posit > l.el_count)
  180.         {
  181.             cout << "Podano niewlasciwa pozycje" << endl;
  182.         return;
  183.         }
  184. cout << "Lista biezaca:" << endl;
  185. show_list(l);
  186. if (posit == 0)
  187.     {
  188.     delete_head(l);
  189.     return;
  190.     }
  191. if(posit == l.el_count)
  192.     {
  193.     delete_tail(l);
  194.     return;
  195.     }
  196. element* temp = l.head;
  197. for (int i =1; i< l.el_count -1; i++)
  198.     {
  199.     temp = temp -> next;
  200.     }
  201. element* temp_delete = temp -> next;
  202. temp -> next = temp_delete -> next;
  203. delete temp_delete;
  204. l.el_count --;
  205. cout << "Lista po zmianie:" << endl;
  206. show_list(l);
  207. }
  208.  
  209. void delete_list(lista &l){
  210. while(l.head != nullptr){
  211.     delete_head(l);
  212. }
  213. }
  214.  
  215. void show_list(lista l){
  216.     element* temp = l.head;
  217. while (temp)
  218.     {
  219.     cout << temp -> data << endl;
  220.     temp = temp -> next;
  221.     l.el_count --;
  222.     }
  223. }
  224.  
  225. void show_on_position(lista l, int posit)
  226. {
  227. int c = 0;
  228. if (posit < 0 or posit > l.el_count)
  229.         {
  230.             cout << "Podano niewlasciwa pozycje" << endl;
  231.         return;
  232.         }
  233. element* temp = l.head;
  234. while(l.el_count != 0){
  235.     c++;
  236.     if (c == posit) cout << temp -> data << endl;
  237.     temp = temp -> next;
  238.     l.el_count --;
  239.             }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement