Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include "List.h"
  4.  
  5. List::List()
  6. {
  7.     size = 0;
  8.     firstElement = nullptr;
  9. }
  10.  
  11. void List::addToBeginning(ListElement* element)
  12. {
  13.     // Jeśli przekazano null jako arguemnt nie rób nic.
  14.     if (!element)
  15.     {
  16.         return;
  17.     }
  18.  
  19.     // Dodaje element listy na początek poprzez poniżej napisane przypisania.
  20.     element->next = this->firstElement;
  21.     this->firstElement = element;
  22.     size++;
  23. }
  24.  
  25. void List::addToBeginning(int key)
  26. {
  27.     // Usunalem check na niezaallokowanie pamięci, jestem leniwy.
  28.     List::addToBeginning(new ListElement(key));
  29. }
  30.  
  31. void List::addToEnd(ListElement* element) {
  32.     // Jeśli chcemy wstawić null na koniec listy(który już tam jest) to nie ma po co przechodzić do końca listy
  33.     if (!element)
  34.     {
  35.         return;
  36.     }
  37.     // Jeśli nie żadnych elementów na liście metoda ta działa jak addToBeginning
  38.     if (!firstElement)
  39.     {
  40.         addToBeginning(element);
  41.     }
  42.     else
  43.     {
  44.         //Przechodzi do ostatniego elementu listy i ustawia jego wskaznik "next" na argument.
  45.         ListElement* current = firstElement;
  46.         while (current->next)
  47.         {
  48.             current = current->next;
  49.         }
  50.         current->next = element;
  51.         size++;
  52.     }
  53. }
  54.  
  55.  
  56. void List::addToEnd(int key) {
  57.     // W przypadku gdy nie można allokować pamięci operator new
  58.     // rzuca wyjątek bad_alloc co kończy program
  59.     List::addToEnd(new ListElement(key));
  60. }
  61.  
  62. ListElement* List::getElement(int key) {
  63.     ListElement* current = firstElement;
  64.     while(current)
  65.     {
  66.         if (current->getKey() == key)
  67.         {
  68.             return current;
  69.         }
  70.         current = current->next;
  71.     }
  72.     return nullptr;
  73.  
  74. }
  75.  
  76. //Duplikacja kodu z metody wyżej.
  77. bool List::contains(int key) {
  78.     return (List::getElement(key));
  79. }
  80.  
  81. //Dupilkacja kodu z metod wyżej
  82. void List::removeKey(int key)
  83. {
  84.     //Jeśli lista jest pusta nie możemy usunąć żadnego z jej elementów.
  85.     if (firstElement == nullptr)
  86.     {
  87.         return;
  88.     }
  89.     ListElement* current = firstElement;
  90.     ListElement* prev = nullptr;
  91.     // Nie podoba mi się ta pętla. Kandydat do refaktoryzacji.
  92.     while (current)
  93.     {
  94.         if (current->getKey() == key)
  95.         {
  96.             if (!prev)
  97.             {
  98.                 firstElement = current->next;
  99.                 delete current;
  100.             }
  101.             else
  102.             {
  103.                 prev->next = current->next;
  104.                 delete current;
  105.             }
  106.             size--;
  107.             break;
  108.         }
  109.         prev = current;
  110.         current = current->next;
  111.     }
  112. }
  113.  
  114. void List::clearAll() {
  115.     // Jeśli lista jest pusta kończymy działanie funkcji.
  116.     if (!firstElement)
  117.     {
  118.         return;
  119.     }
  120.  
  121.     ListElement* current = firstElement;
  122.     ListElement* deleteMe = nullptr;
  123.     //Ta pętla ustawia wsakaźnik current na ostatni element listy.
  124.     while (current -> next)
  125.     {
  126.         deleteMe = current;
  127.         current = current->next;
  128.         delete deleteMe;
  129.     }
  130.     firstElement = nullptr;
  131.     size = 0;
  132. }
  133.  
  134. void List::printList()
  135. {
  136.     std::cout << "List = [";
  137.     ListElement* current = firstElement;
  138.     while (current)
  139.     {
  140.         std::cout << current->getKey();
  141.         if (current->next)
  142.         {
  143.             std::cout << ", ";
  144.         }
  145.         current = current -> next;
  146.     }
  147.     std::cout << "] size =" << size << std::endl;
  148. }
  149.  
  150. // LAB 6
  151.  
  152. List& List::operator+=(ListElement& element) {
  153.     this->addToEnd(&element);
  154.     return *this;
  155. }
  156.  
  157. List& List::operator+=(int key)
  158. {
  159.     this->addToEnd(key);
  160.     return *this;
  161. }
  162.  
  163. // !tu nie jestem pewien czy chodzi mu o to ale raczej tak :D
  164. // !bo alterantywą moze być usunięcie dokłądanie tego elementu z listy(po torzsamości czyli adresie w pamięci a nie wartości ale to raczej mało logiczne :D
  165. // Bo wtedy jeśli lista zawiera 5 i zrobiłbyś lista-=new ListElement(5); to tej piątki nie usunie ale trzeba się będzie zapytać.
  166. List& List::operator-=(ListElement& element)
  167. {
  168.     this->removeKey(element.getKey());
  169.     return *this;
  170. }
  171.  
  172. List& List::operator-=(int key)
  173. {
  174.     this->removeKey(key);
  175.     return *this;
  176. }
  177.  
  178. bool List::operator==(List& otherList)
  179. {
  180.     // jeśli listy mają różny rozmiar to są różne
  181.     if (this->size != otherList.size)
  182.     {
  183.         return false;
  184.     }
  185.     // Jeśli obie listy są puste to są równe
  186.     if (!firstElement)
  187.     {
  188.         return true;
  189.     }
  190.     else
  191.     {
  192.         //Przechodzimy przez wszystkie elementy listy po lewej stronie(this) i sprawdzamy czy druga lista zawiera ten element dla każdego elementu
  193.         //jeśli chociaż jeden nie znajduje się w liście otherList to zwracamy false
  194.         ListElement* current = firstElement;
  195.         while (current->next)
  196.         {
  197.             if (!otherList.contains(current->getKey()))
  198.             {
  199.                 return false;
  200.             }
  201.         }
  202.     }
  203.     return true;
  204. }
  205.  
  206. bool List::operator!=(List& otherList)
  207. {
  208.     return this != &otherList;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement