Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #ifndef Nagłówek.h
- #define Nagłówek_h
- template <typename T>
- class Element_listy
- {
- public:
- Element_listy();
- ~Element_listy();
- Element_listy *next, *previous;
- T klucz;
- private:
- };
- template <typename T>
- Element_listy<T>::Element_listy()
- {
- }
- template <typename T>
- Element_listy<T>::~Element_listy()
- {
- }
- template <typename T>
- class Lista_dwukierunkowa
- {
- public:
- Element_listy <T> *head, *tail;
- int rozmiar;
- Lista_dwukierunkowa();
- ~Lista_dwukierunkowa();
- void Dodanie_nowego_elementu_na_koncu(const T&klucz)
- {
- Element_listy<T> *Nowy_element = new Element_listy<T>;
- Nowy_element->klucz = klucz;
- if (rozmiar != 0)
- {
- tail->next = Nowy_element;
- Nowy_element->next = nullptr;
- Nowy_element->previous = tail;
- tail = Nowy_element;
- }
- if (rozmiar == 0)
- {
- Nowy_element->next = nullptr;
- Nowy_element->previous = nullptr;
- head = Nowy_element;
- tail = Nowy_element;
- }
- rozmiar += 1;
- }
- void Dodanie_nowego_elementu_na_poczatku(const T&klucz)
- {
- Element_listy<T> *Nowy_element = new Element_listy<T>;
- Nowy_element->klucz = klucz;
- if (rozmiar != 0)
- {
- head->previous = Nowy_element;
- Nowy_element->next = head;
- Nowy_element->previous = nullptr;
- head = Nowy_element;
- }
- if (rozmiar == 0)
- {
- Nowy_element->next = nullptr;
- Nowy_element->previous = nullptr;
- head = Nowy_element;
- tail = Nowy_element;
- }
- rozmiar += 1;
- }
- void Usuniecie_ostatniego_elementu()
- {
- if (rozmiar == 0)
- {
- cout << "Lista nie ma elementu" << endl;
- }
- else
- {
- Element_listy<T> *temp = new Element_listy<T>;
- temp = tail;
- tail = tail->previous;
- //W razie, gdyby lista była pusta, wyzerowujemy także head:
- if (tail == nullptr)
- head = nullptr;
- delete temp;
- }
- rozmiar -= 1;
- }
- void Usuniecie_pierwszego_elementu()
- {
- if (rozmiar == 0)
- {
- cout << "Lista nie ma elementu" << endl;
- }
- else
- {
- Element_listy<T> *temp = new Element_listy<T>;
- temp = head;
- head = head->next;
- //W razie, gdyby lista była pusta, wyzerowujemy także tail:
- if (head == nullptr)
- tail = nullptr;
- delete temp;
- }
- rozmiar -= 1;
- }
- T Zwrocenie_danych_danego_elementu(unsigned n)
- {
- Element_listy<T> *temp = new Element_listy<T>;
- if (n >= rozmiar)
- return NULL;
- else if (n == rozmiar)
- return tail->klucz;
- else
- {
- int counter = 0;
- temp = head;
- while (counter <= n)
- {
- if (counter == n)
- return temp->klucz;
- else
- temp = temp->next;
- counter += 1;
- }
- }
- delete temp;
- }
- void Podmiana_danych_danego_elementu(unsigned n, const T&nowy_klucz)
- {
- Element_listy<T> *temp = new Element_listy<T>;
- if (n >= rozmiar)
- cout << "Lista nie ma takiego indeksu" << endl;
- else
- {
- int counter = 0;
- temp = head;
- while (counter <= n)
- {
- if (counter == n)
- temp->klucz = nowy_klucz;
- else
- temp = temp->next;
- counter += 1;
- }
- }
- }
- Element_listy<T> *Wyszukiwanie_elementu_po_wartosci(const T&szukany_klucz)
- {
- Element_listy<T> *temp = new Element_listy<T>;
- int counter = 0;
- temp = head;
- while (counter <= rozmiar)
- {
- if (temp->klucz == szukany_klucz)
- return temp;
- else
- temp = temp->next;
- counter += 1;
- if (counter >= rozmiar)
- return NULL;
- }
- }
- bool Wyszukanie_i_usuniecie_elementu(const T&szukany_klucz)
- {
- Element_listy<T> *temp = new Element_listy<T>;
- int counter = 0;
- temp = head;
- while (counter < rozmiar)
- {
- if (temp->klucz == szukany_klucz)
- {
- if (counter == 0)
- {
- Usuniecie_pierwszego_elementu();
- //rozmiar -= 1;
- //Udało się usunąć
- return 1;
- }
- else if (counter == rozmiar - 1)
- {
- Usuniecie_ostatniego_elementu();
- //rozmiar -= 1;
- //Udało się usunąć
- return 1;
- }
- else
- {
- Element_listy<T> *temp1 = new Element_listy<T>;
- temp1 = temp->previous;
- temp1->next = temp->next;
- delete temp;
- rozmiar -= 1;
- //Udało się usunąć
- return 1;
- }
- }
- else
- {
- temp = temp->next;
- counter += 1;
- }
- }
- return 0;
- }
- void Dodanie_elementu_w_porzadku(const T&klucz,unsigned indeks)
- {
- Element_listy<T> *temp = new Element_listy<T>;
- Element_listy<T> *temp1 = new Element_listy<T>;
- int counter = 0;
- temp = head;
- while (counter < indeks)
- {
- temp = temp->next;
- counter += 1;
- }
- temp1->klucz = temp->klucz;
- temp1->next = temp->next;
- temp1->previous = temp;
- temp->klucz = klucz;
- temp->next = temp1;
- rozmiar += 1;
- }
- void Czyszczenie_listy()
- {
- /*for (int i=1;i<=rozmiar;i++)
- {
- Usuniecie_pierwszego_elementu();
- }*/
- head = tail = NULL;
- rozmiar = 0;
- }
- void Napisowa_reprezentacja_listy()
- {
- cout << "Elementy listy:" << endl;
- if (rozmiar == 0)
- cout << "Lista jest pusta";
- Element_listy<T> *temp = new Element_listy<T>;
- int counter = 1;
- temp = head;
- while (counter <= rozmiar)
- {
- cout << temp->klucz << ", ";
- temp = temp->next;
- counter += 1;
- }
- cout << endl;
- }
- };
- template <typename T>
- Lista_dwukierunkowa<T>::Lista_dwukierunkowa()
- {
- head = tail = nullptr;
- rozmiar = 0;
- }
- template <typename T>
- Lista_dwukierunkowa<T>::~Lista_dwukierunkowa()
- {
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement