Advertisement
Guest User

Untitled

a guest
Aug 16th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <sstream>
  2.  
  3. #include "List.h"
  4.  
  5. // NODE
  6. List::Node::Node(int value, Node* prev, Node* next)
  7.     : value(value)
  8.     , prev(prev)
  9.     , next(next) {
  10. }
  11.  
  12. int List::Node::getValue() const { return this->value; }
  13. void List::Node::setValue(int value) { this->value = value; }
  14.  
  15. List::Node* List::Node::getNext() const { return next; }
  16. void List::Node::setNext(Node* next) { this->next = next; }
  17.  
  18. List::Node* List::Node::getPrev() const { return prev; }
  19. void List::Node::setPrev(Node* prev) { this->prev = prev; }
  20.  
  21. // LIST
  22. List::List()
  23.     : head(nullptr)
  24.     , tail(nullptr)
  25.     , size(0) {
  26. }
  27.  
  28. List::List(const List& other)
  29.     : head(size ? new Node(other.head->getValue(), other.head->getPrev(), other.head->getNext()) : nullptr)
  30.     , tail(size ? new Node(other.tail->getValue(), other.tail->getPrev(), other.tail->getNext()) : nullptr)
  31.     , size(other.size) {
  32. }
  33.  
  34. int List::first() const {
  35.     return head->getValue();
  36. }
  37.  
  38. void List::add(int value) {
  39.     Node* it = tail;
  40.     if (it == nullptr) {
  41.         Node* newNode = new Node(value, nullptr, nullptr);
  42.         head = tail = newNode;
  43.         size++;
  44.         return;
  45.     }
  46.     Node* currTail = tail;
  47.     Node* newTail = new Node(value, currTail, nullptr);
  48.     tail = newTail;
  49.     currTail->setNext(tail);
  50.     size++;
  51.  
  52. }
  53.  
  54. void List::addAll(const List& other) {
  55.     auto ptrOther = &other;
  56.     if (ptrOther->isEmpty()) {
  57.         return;
  58.     }
  59.     Node* it = other.head;
  60.     while (it != other.tail) {
  61.         this->add(it->getValue());
  62.         it = other.head->getNext();
  63.     }
  64.     this->size = other.size;
  65. }
  66.  
  67. void List::removeFirst() {
  68.     Node* oldHead = head;
  69.     head = head->getNext();
  70.     delete oldHead;
  71.     oldHead = nullptr;
  72.     size--;
  73. }
  74.  
  75. void List::removeAll() {
  76.     while (head != nullptr) {
  77.         removeFirst();
  78.     }
  79. }
  80.  
  81. size_t List::getSize() const {
  82.     return size;
  83. }
  84.  
  85. bool List::isEmpty() const {
  86.     if (head != nullptr) {
  87.         return false;
  88.     }
  89.     return true;
  90. }
  91.  
  92. static List getReversed(List l) {
  93.  
  94. }
  95.  
  96. std::string List::toString() const {
  97.     std::ostringstream stream;
  98.     Node* it = head;
  99.     while (it != nullptr) {
  100.         stream << it->getValue() << " ";
  101.         it = it->getNext();
  102.     }
  103.     return stream.str();
  104. }
  105.  
  106. List& List::operator<<(const int& value) {
  107.     this->add(value);
  108.     return *this;
  109. }
  110.  
  111. List& List::operator<<(const List& other) {
  112.     this->addAll(other);
  113.     return *this;
  114. }
  115.  
  116. List& List::operator=(const List& other) {
  117.     if (this != &other) {
  118.         this->size = other.size;
  119.        
  120.         Node* newHead = new Node(other.head->getValue(), other.head->getPrev(), other.head->getNext()); // allocate
  121.         if (this->head != nullptr) {
  122.             delete this->head;  // deallocate
  123.         }
  124.         this->head = newHead;
  125.  
  126.         Node* newTail = new Node(other.tail->getValue(), other.tail->getPrev(), other.tail->getNext()); // allocate
  127.         if (this->tail != nullptr) {
  128.             delete this->tail;  // deallocate
  129.         }
  130.         this->tail = newTail;
  131.  
  132.  
  133.     }
  134.     return *this;
  135. }
  136.  
  137. List::~List() {
  138.     Node* it = head;
  139.     while (it != nullptr) {
  140.         Node* nextIt = it->getNext();
  141.         delete it;
  142.         it = nextIt;
  143.     }
  144.     head = tail = nullptr;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement