Advertisement
MeehoweCK

Untitled

Mar 21st, 2023
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. // main.cpp
  2. #include "head.h"
  3.  
  4. int main()
  5. {
  6.     Lista* lista = new Lista;       // wywołuje się konstruktor listy
  7.     lista->dodaj_element(5);
  8.     lista->dodaj_element(10);
  9.     lista->dodaj_element(15);
  10.     lista->wyswietl_liste();
  11.     lista->removeByIndex(4);
  12.     lista->wyswietl_liste();
  13.     lista->usun_liste();
  14.  
  15.     return 0;
  16. }
  17.  
  18. // head.h
  19. #ifndef HEAD_H_INCLUDED
  20. #define HEAD_H_INCLUDED
  21.  
  22. #include <iostream>
  23.  
  24. using namespace std;
  25.  
  26. struct Element
  27. {
  28.     int wartosc;
  29.     Element* next;
  30. };
  31.  
  32. struct Lista
  33. {
  34.     Element* head;
  35.     Lista();
  36.     void dodaj_element(int value);
  37.     void wyswietl_liste();
  38.     void usun_ostatni_element();
  39.     void usun_liste();
  40.     void removeByIndex(unsigned index);
  41. };
  42.  
  43. #endif // HEAD_H_INCLUDED
  44.  
  45. // Lista.cpp
  46. #include "head.h"
  47.  
  48. /*struct Element
  49. {
  50.     int wartosc;
  51.     Element* next;
  52. };
  53.  
  54. struct Lista
  55. {
  56.     Element* head;
  57.     Lista();
  58.     void dodaj_element(int value);
  59.     void wyswietl_liste();
  60.     void usun_ostatni_element();
  61.     void usun_liste();
  62.     void removeByIndex(unsigned index);
  63. };*/
  64.  
  65. Lista::Lista() : head(nullptr) {}   // konstruktor listy
  66.  
  67. void Lista::dodaj_element(int value)
  68. {
  69.     Element* nowy = new Element;
  70.  
  71.     // wypełnienie danymi
  72.     nowy->wartosc = value;
  73.     nowy->next = nullptr;
  74.  
  75.     if(head == nullptr)     // dodajemy pierwszy element
  76.     {
  77.         head = nowy;
  78.         return;
  79.     }
  80.  
  81.     // dodajemy na kolejną pozycję
  82.     Element* temp = head;
  83.  
  84.     // wędrujemy na koniec listy
  85.     while(temp->next != nullptr)
  86.         temp = temp->next;
  87.  
  88.     // dodajemy jako ostatni element
  89.     temp->next = nowy;
  90. }
  91.  
  92. void Lista::wyswietl_liste()
  93. {
  94.     if(head == nullptr)
  95.     {
  96.         cout << "Lista jest pusta :(\n";
  97.         return;
  98.     }
  99.     Element* temp = head;
  100.     while(temp != nullptr)
  101.     {
  102.         cout << temp->wartosc << '\t';
  103.         temp = temp->next;
  104.     }
  105.     cout << endl;
  106. }
  107.  
  108. void Lista::usun_ostatni_element()
  109. {
  110.     // usuwamy pierwszy element
  111.     if(head->next == nullptr)
  112.     {
  113.         delete head;
  114.         head = nullptr;
  115.         return;
  116.     }
  117.  
  118.     // lista ma więcej niż jeden element
  119.     Element* temp = head;
  120.  
  121.     while(temp->next->next != nullptr)
  122.         temp = temp->next;
  123.     delete temp->next;
  124.     temp->next = nullptr;
  125. }
  126.  
  127. void Lista::usun_liste()
  128. {
  129.     while(head != nullptr)
  130.         usun_ostatni_element();
  131. }
  132.  
  133. void Lista::removeByIndex(unsigned index)
  134. {
  135.     if(head == nullptr)
  136.         return;
  137.     // usuwamy pierwszy element listy
  138.     if(index == 0)
  139.     {
  140.         Element* temp = head;
  141.         head = temp->next;
  142.         delete temp;
  143.         return;
  144.     }
  145.  
  146.     // usuwamy inny element niż pierwszy
  147.     unsigned j = 0;
  148.     Element* temp = head;
  149.  
  150.     while(temp != nullptr)
  151.     {
  152.         //cout << j << '\t' << temp->wartosc << endl;
  153.         if((j + 1) == index) break;
  154.         temp = temp->next;
  155.         ++j;
  156.     }
  157.     if(temp != nullptr)     // zabezpieczenie, aby nie usuwać czegoś, czego nie ma
  158.     {
  159.         // usuwamy ostatni element listy
  160.         if(temp->next->next == nullptr)
  161.         {
  162.             delete temp->next;
  163.             temp->next = nullptr;
  164.             return;
  165.         }
  166.         // usuwamy środkowy element listy
  167.         Element* usuwany = temp->next;
  168.         temp->next = temp->next->next;
  169.         delete usuwany;
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement