Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include "List.h"
- List::List()
- {
- size = 0;
- firstElement = nullptr;
- }
- void List::addToBeginning(ListElement* element)
- {
- // Jeśli przekazano null jako arguemnt nie rób nic.
- if (!element)
- {
- return;
- }
- // Dodaje element listy na początek poprzez poniżej napisane przypisania.
- element->next = this->firstElement;
- this->firstElement = element;
- size++;
- }
- void List::addToBeginning(int key)
- {
- // Usunalem check na niezaallokowanie pamięci, jestem leniwy.
- List::addToBeginning(new ListElement(key));
- }
- void List::addToEnd(ListElement* element) {
- // Jeśli chcemy wstawić null na koniec listy(który już tam jest) to nie ma po co przechodzić do końca listy
- if (!element)
- {
- return;
- }
- // Jeśli nie żadnych elementów na liście metoda ta działa jak addToBeginning
- if (!firstElement)
- {
- addToBeginning(element);
- }
- else
- {
- //Przechodzi do ostatniego elementu listy i ustawia jego wskaznik "next" na argument.
- ListElement* current = firstElement;
- while (current->next)
- {
- current = current->next;
- }
- current->next = element;
- size++;
- }
- }
- void List::addToEnd(int key) {
- // W przypadku gdy nie można allokować pamięci operator new
- // rzuca wyjątek bad_alloc co kończy program
- List::addToEnd(new ListElement(key));
- }
- ListElement* List::getElement(int key) {
- ListElement* current = firstElement;
- while (current)
- {
- if (current->getKey() == key)
- {
- return current;
- }
- current = current->next;
- }
- return nullptr;
- }
- //Duplikacja kodu z metody wyżej.
- bool List::contains(int key) {
- return (List::getElement(key));
- }
- //Dupilkacja kodu z metod wyżej
- void List::removeKey(int key)
- {
- //Jeśli lista jest pusta nie możemy usunąć żadnego z jej elementów.
- if (firstElement == nullptr)
- {
- return;
- }
- ListElement* current = firstElement;
- ListElement* prev = nullptr;
- // Nie podoba mi się ta pętla. Kandydat do refaktoryzacji.
- while (current)
- {
- if (current->getKey() == key)
- {
- if (!prev)
- {
- firstElement = current->next;
- delete current;
- }
- else
- {
- prev->next = current->next;
- delete current;
- }
- size--;
- break;
- }
- prev = current;
- current = current->next;
- }
- }
- void List::clearAll() {
- // Jeśli lista jest pusta kończymy działanie funkcji.
- if (!firstElement)
- {
- return;
- }
- ListElement* current = firstElement;
- ListElement* deleteMe = nullptr;
- //Ta pętla ustawia wsakaźnik current na ostatni element listy.
- while (current->next)
- {
- deleteMe = current;
- current = current->next;
- delete deleteMe;
- }
- firstElement = nullptr;
- size = 0;
- }
- void List::printList()
- {
- std::cout << this->toString();
- }
- // LAB 6
- string List::toString() const
- {
- std::ostringstream stm;
- stm << "List = [";
- ListElement* current = firstElement;
- while (current)
- {
- stm << current->getKey();
- if (current->next)
- {
- stm << ", ";
- }
- current = current->next;
- }
- stm << "] size =" << size << std::endl;
- return stm.str();
- }
- List& List::operator+=(ListElement& element) {
- this->addToEnd(&element);
- return *this;
- }
- List& List::operator+=(int key)
- {
- this->addToEnd(key);
- return *this;
- }
- // !tu nie jestem pewien czy chodzi mu o to ale raczej tak :D
- // !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
- // 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ć.
- List& List::operator-=(ListElement& element)
- {
- this->removeKey(element.getKey());
- return *this;
- }
- List& List::operator-=(int key)
- {
- this->removeKey(key);
- return *this;
- }
- bool List::operator==(List& otherList)
- {
- // jeśli listy mają różny rozmiar to są różne
- if (this->size != otherList.size)
- {
- return false;
- }
- // Jeśli obie listy są puste to są równe
- if (!firstElement)
- {
- return true;
- }
- else
- {
- //Przechodzimy przez wszystkie elementy listy po lewej stronie(this) i sprawdzamy czy druga lista zawiera ten element dla każdego elementu
- //jeśli chociaż jeden nie znajduje się w liście otherList to zwracamy false
- ListElement* current = firstElement;
- while (current->next)
- {
- if (!otherList.contains(current->getKey()))
- {
- return false;
- }
- }
- }
- return true;
- }
- bool List::operator!=(List& otherList)
- {
- return this != &otherList;
- }
- std::ostream& operator<<(std::ostream& output, const List& list)
- {
- output << list.toString();
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement