Advertisement
Guest User

Untitled

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