Advertisement
Guest User

Nagłówek.h

a guest
Oct 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #ifndef Nagłówek.h
  4. #define Nagłówek_h
  5. template <typename T>
  6. class Element_listy
  7. {
  8. public:
  9.     Element_listy();
  10.     ~Element_listy();
  11.     Element_listy *next, *previous;
  12.     T klucz;
  13. private:
  14.  
  15. };
  16. template <typename T>
  17. Element_listy<T>::Element_listy()
  18. {
  19. }
  20. template <typename T>
  21. Element_listy<T>::~Element_listy()
  22. {
  23. }
  24.  
  25. template <typename T>
  26. class Lista_dwukierunkowa
  27. {
  28. public:
  29.     Element_listy <T> *head, *tail;
  30.     int rozmiar;
  31.     Lista_dwukierunkowa();
  32.     ~Lista_dwukierunkowa();
  33.     void Dodanie_nowego_elementu_na_koncu(const T&klucz)
  34.     {
  35.         Element_listy<T> *Nowy_element = new Element_listy<T>;
  36.         Nowy_element->klucz = klucz;
  37.         if (rozmiar != 0)
  38.         {
  39.             tail->next = Nowy_element;
  40.             Nowy_element->next = nullptr;
  41.             Nowy_element->previous = tail;
  42.             tail = Nowy_element;
  43.         }
  44.         if (rozmiar == 0)
  45.         {
  46.             Nowy_element->next = nullptr;
  47.             Nowy_element->previous = nullptr;
  48.             head = Nowy_element;
  49.             tail = Nowy_element;
  50.         }
  51.         rozmiar += 1;
  52.     }
  53.     void Dodanie_nowego_elementu_na_poczatku(const T&klucz)
  54.     {
  55.         Element_listy<T> *Nowy_element = new Element_listy<T>;
  56.         Nowy_element->klucz = klucz;
  57.         if (rozmiar != 0)
  58.         {
  59.             head->previous = Nowy_element;
  60.             Nowy_element->next = head;
  61.             Nowy_element->previous = nullptr;
  62.             head = Nowy_element;
  63.         }
  64.         if (rozmiar == 0)
  65.         {
  66.             Nowy_element->next = nullptr;
  67.             Nowy_element->previous = nullptr;
  68.             head = Nowy_element;
  69.             tail = Nowy_element;
  70.         }
  71.         rozmiar += 1;
  72.     }
  73.     void Usuniecie_ostatniego_elementu()
  74.     {
  75.         if (rozmiar == 0)
  76.         {
  77.             cout << "Lista nie ma elementu" << endl;
  78.         }
  79.         else
  80.         {
  81.             Element_listy<T> *temp = new Element_listy<T>;
  82.             temp = tail;
  83.             tail = tail->previous;
  84.             //W razie, gdyby lista była pusta, wyzerowujemy także head:
  85.             if (tail == nullptr)
  86.                 head = nullptr;
  87.             delete temp;
  88.         }
  89.         rozmiar -= 1;
  90.     }
  91.     void Usuniecie_pierwszego_elementu()
  92.     {
  93.         if (rozmiar == 0)
  94.         {
  95.             cout << "Lista nie ma elementu" << endl;
  96.         }
  97.         else
  98.         {
  99.             Element_listy<T> *temp = new Element_listy<T>;
  100.             temp = head;
  101.             head = head->next;
  102.             //W razie, gdyby lista była pusta, wyzerowujemy także tail:
  103.             if (head == nullptr)
  104.                 tail = nullptr;
  105.             delete temp;
  106.         }
  107.         rozmiar -= 1;
  108.     }
  109.     T Zwrocenie_danych_danego_elementu(unsigned n)
  110.     {
  111.         Element_listy<T> *temp = new Element_listy<T>;
  112.         if (n >= rozmiar)
  113.             return NULL;
  114.         else if (n == rozmiar)
  115.             return tail->klucz;
  116.         else
  117.         {
  118.             int counter = 0;
  119.             temp = head;
  120.             while (counter <= n)
  121.             {
  122.                 if (counter == n)
  123.                     return temp->klucz;
  124.                 else
  125.                     temp = temp->next;
  126.                 counter += 1;
  127.             }
  128.         }
  129.         delete temp;
  130.     }
  131.     void Podmiana_danych_danego_elementu(unsigned n, const T&nowy_klucz)
  132.     {
  133.         Element_listy<T> *temp = new Element_listy<T>;
  134.         if (n >= rozmiar)
  135.             cout << "Lista nie ma takiego indeksu" << endl;
  136.         else
  137.         {
  138.             int counter = 0;
  139.             temp = head;
  140.             while (counter <= n)
  141.             {
  142.                 if (counter == n)
  143.                     temp->klucz = nowy_klucz;
  144.                 else
  145.                     temp = temp->next;
  146.                 counter += 1;
  147.             }
  148.         }
  149.     }
  150.     Element_listy<T> *Wyszukiwanie_elementu_po_wartosci(const T&szukany_klucz)
  151.     {
  152.         Element_listy<T> *temp = new Element_listy<T>;
  153.         int counter = 0;
  154.         temp = head;
  155.         while (counter <= rozmiar)
  156.         {
  157.             if (temp->klucz == szukany_klucz)
  158.                 return temp;
  159.             else
  160.                 temp = temp->next;
  161.             counter += 1;
  162.             if (counter >= rozmiar)
  163.                 return NULL;
  164.         }
  165.     }
  166.     bool Wyszukanie_i_usuniecie_elementu(const T&szukany_klucz)
  167.     {
  168.         Element_listy<T> *temp = new Element_listy<T>;
  169.         int counter = 0;
  170.         temp = head;
  171.         while (counter < rozmiar)
  172.         {
  173.             if (temp->klucz == szukany_klucz)
  174.             {
  175.                 if (counter == 0)
  176.                 {
  177.                     Usuniecie_pierwszego_elementu();
  178.                     //rozmiar -= 1;
  179.                     //Udało się usunąć
  180.                     return 1;
  181.                 }
  182.                 else if (counter == rozmiar - 1)
  183.                 {
  184.                     Usuniecie_ostatniego_elementu();
  185.                     //rozmiar -= 1;
  186.                     //Udało się usunąć
  187.                     return 1;
  188.                 }
  189.                 else
  190.                 {
  191.                     Element_listy<T> *temp1 = new Element_listy<T>;
  192.                     temp1 = temp->previous;
  193.                     temp1->next = temp->next;
  194.                     delete temp;
  195.                     rozmiar -= 1;
  196.                     //Udało się usunąć
  197.                     return 1;
  198.                 }
  199.             }
  200.             else
  201.             {
  202.                 temp = temp->next;
  203.                 counter += 1;
  204.             }
  205.         }
  206.         return 0;
  207.     }
  208.     void Dodanie_elementu_w_porzadku(const T&klucz,unsigned indeks)
  209.     {
  210.         Element_listy<T> *temp = new Element_listy<T>;
  211.         Element_listy<T> *temp1 = new Element_listy<T>;
  212.         int counter = 0;
  213.         temp = head;
  214.         while (counter < indeks)
  215.         {
  216.             temp = temp->next;
  217.             counter += 1;
  218.         }
  219.         temp1->klucz = temp->klucz;
  220.         temp1->next = temp->next;
  221.         temp1->previous = temp;
  222.         temp->klucz = klucz;
  223.         temp->next = temp1;
  224.         rozmiar += 1;
  225.     }
  226.     void Czyszczenie_listy()
  227.     {
  228.         /*for (int i=1;i<=rozmiar;i++)
  229.         {
  230.             Usuniecie_pierwszego_elementu();
  231.         }*/
  232.         head = tail = NULL;
  233.         rozmiar = 0;
  234.     }
  235.     void Napisowa_reprezentacja_listy()
  236.     {
  237.         cout << "Elementy listy:" << endl;
  238.         if (rozmiar == 0)
  239.             cout << "Lista jest pusta";
  240.         Element_listy<T> *temp = new Element_listy<T>;
  241.         int counter = 1;
  242.         temp = head;
  243.         while (counter <= rozmiar)
  244.         {
  245.             cout << temp->klucz << ", ";
  246.             temp = temp->next;
  247.             counter += 1;
  248.         }
  249.         cout << endl;
  250.     }
  251. };
  252. template <typename T>
  253. Lista_dwukierunkowa<T>::Lista_dwukierunkowa()
  254. {
  255.     head = tail = nullptr;
  256.     rozmiar = 0;
  257. }
  258. template <typename T>
  259. Lista_dwukierunkowa<T>::~Lista_dwukierunkowa()
  260. {
  261. }
  262.  
  263. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement