Advertisement
Guest User

Listy

a guest
Jan 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. #pragma once
  2.  
  3. struct node {
  4.     int value;
  5.     node *next;
  6. };
  7.  
  8. void push_front(node*& head, int value)     //Dodanie na początku
  9. {
  10.     node* temp = new node;      //Tworzymy nowy wskaźnik
  11.     temp->value = value;        //przypisujemy wartość
  12.     temp->next = head;          //Przypisujemy wskaźnik na head
  13.     head = temp;                //ustawiamy na głowyny naszego tempa
  14. }
  15.  
  16. void push_last(node*& head, int value)      //Dodanie na końcu
  17. {
  18.     node* temp = new node;      //Tworzenie nowego wskaźnika
  19.     temp->value = value;        //Przypisanie wartosci do wskaznika
  20.  
  21.     node* last = head;          //Stworzenie nowego wskaznika KOPIA HEAD
  22.     while (last->next != NULL)  //Tak długo dopóki nie dojdziemy do ostatniego elementu
  23.         last = last->next;
  24.     last->next = temp;          //Przypisanie do wskaźnika ostatniego elementu wskaźnik na NOWY OSTATNI  
  25.     temp->next = NULL;          //Przypisanie NOWEMU OSTATNIEMU wskaźnika NULL
  26. }
  27.  
  28. void push_behind_x(node*& head, int x, int add_value)       //Dodanie za wskazanym x (przy założeniu, że list i wskazywany x istnieje)
  29. {
  30.     node* temp = new node;      //Stworzenie nowego wskaznika
  31.     temp->value = add_value;    //Przypisanie wartosci
  32.  
  33.     node* find_x = head;        //Zrobienie kopi wskaznika head
  34.     while (find_x->value == x)  //Tak długo dopóki nieznajdziemy odpowiadającej nam wartosci, po której chcemy wstawić
  35.         find_x = find_x->next;  //Po znaleznieniu wychodzimy. Powiedzmy, że mamy ciąg 1 -> 2 -> 3 -> 4.  
  36.     temp->next = find_x->next;  //Nasz p wskazuje na x=2. Czyli teraz wskaznik z 2 (->3) kopiujemy do tempa (temp -> 3)
  37.     find_x->next = temp;        //A na wskazniku 2 ustawiamy wskaznik na tempa (2 -> temp -> 3)
  38. }
  39.  
  40. void show(node* head)               //Wyświetlenie lusty
  41. {
  42.     node* temp = head;          //Tworzymy kopie wskaznika (w celu edycji Kopi, a zachowaniu pierwszego wskaznika)
  43.     while (temp != NULL)        //Tak dlugo dopóki nie dojdziemy do "pustej" zmiennej
  44.     {
  45.         std::cout << temp->value << " ";
  46.         temp = temp->next;      //Przechodzimy do kolejnej liczby
  47.     }
  48. }
  49.  
  50.  
  51. void pop_last(node*& head)          //Usunięcie ostatniego
  52. {
  53.     node* temp = head;          //Kopia wskaźnika head, na którym będziemy operować
  54.  
  55.     if (temp == NULL)           //Sprawdzenie, czy lista istneje
  56.         std::cout << "Lista nie istnieje!\n";
  57.     else if (temp->next == NULL) //Sprawdzenie czy lista jest jedno elementowa
  58.     {
  59.         head = NULL;
  60.         delete temp;
  61.     }
  62.     else
  63.     {
  64.         while (temp->next != NULL)
  65.             temp = temp->next;              //łapie ostatni element
  66.  
  67.         node* front_temp = head;
  68.         while (front_temp->next != temp)
  69.             front_temp = front_temp->next;  //Łapie przedostarni element
  70.  
  71.         front_temp->next = NULL;            //W przedostatnim elemencie zamienia wskaznik na NULL
  72.         delete temp;                        //Usuwa ostatni
  73.  
  74.     }
  75. }
  76.  
  77. void pop_first(node*& head)         //Usuwanie pierwszego elementu
  78. {
  79.     node* temp = head;          //Kopia wskaznika 1(head)(temp)->2->3
  80.     head = head->next;          //Ustawienie  head na kolejny element 1 -> 2(head) -> 3
  81.     delete temp;                //Usunięcie pierwszego elementu 1(temp - > DELETE) -> 2(head) ->3
  82. }
  83.  
  84. void pop_behind_x(node*& head, int x)       //Usunięcie za wskazanym x
  85. {
  86.     node* temp = head;
  87.     if (temp == NULL)
  88.         std::cout << "Lista nie istnieje, nie ma co usuwac!\n";
  89.     else
  90.     {
  91.         int check = 0;
  92.         while (temp != NULL)
  93.         {
  94.  
  95.             if (temp->value == x)
  96.             {
  97.                 node* front_temp = temp;
  98.                 temp = temp->next;
  99.                 if (temp->next != NULL)
  100.                     front_temp->next = temp->next;
  101.                 else
  102.                     front_temp->next = NULL;
  103.                 check++;
  104.             }
  105.  
  106.             temp = temp->next;
  107.         }
  108.         if (check == 0)
  109.             std::cout << "Podanego elementu nie znaleziono!\n";
  110.     }
  111. }
  112.  
  113. void listy()
  114. {
  115.     node * head = nullptr;
  116.  
  117.     push_front(head, 7);
  118.     push_front(head, 8);
  119.     push_front(head, 9);
  120.     push_front(head, 10);
  121.     show(head);
  122.     std::cout << "\n\nUsuniecie ostaniego:\n";
  123.     pop_last(head);
  124.     show(head);
  125.     std::cout << "\n\nDodanie elementu na koniec:\n";
  126.     push_last(head, 11);
  127.     show(head);
  128.     std::cout << "\n\nUsuniecie pierwszego elementu:\n";
  129.     pop_first(head);
  130.     show(head);
  131.     std::cout << "\n\nDodanie elementu na poczatek:\n";
  132.     push_front(head, 69);
  133.     show(head);
  134.     std::cout << "\n\nDodanie elementu po x = 8 :\n";
  135.     push_behind_x(head, 8, 17);
  136.     show(head);
  137.     std::cout << "\n\nUsuniecie elementu po x = 8 :\n";
  138.     pop_behind_x(head, 17);
  139.     show(head);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement