Advertisement
Guest User

List.cpp

a guest
Aug 15th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 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(other.head)
  30.     , tail(other.tail)
  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. }
  65.  
  66. void List::removeFirst() {
  67.     Node* oldHead = head;
  68.     head = head->getNext();
  69.     delete oldHead;
  70.     oldHead = nullptr;
  71. }
  72.  
  73. void List::removeAll() {
  74.     while (head != nullptr) {
  75.         removeFirst();
  76.     }
  77. }
  78.  
  79. size_t List::getSize() const {
  80.     return size;
  81. }
  82.  
  83. bool List::isEmpty() const {
  84.     if (head != nullptr) {
  85.         return false;
  86.     }
  87.     return true;
  88. }
  89.  
  90. static List getReversed(List l) {
  91.  
  92. }
  93.  
  94. std::string List::toString() const {
  95.     std::ostringstream stream;
  96.     Node* it = head;
  97.     while (it != nullptr) {
  98.         stream << it->getValue() << " ";
  99.         it = it->getNext();
  100.     }
  101.     return stream.str();
  102. }
  103.  
  104. List& List::operator<<(const int& value) {
  105.     this->add(value);
  106.     return *this;
  107. }
  108.  
  109. List& List::operator<<(const List& other) {
  110.     this->addAll(other);
  111.     return *this;
  112. }
  113.  
  114. List& List::operator=(const List& other) {
  115.     this->head = other.head;
  116.     this->tail = other.tail;
  117.     this->size = other.size;
  118.  
  119.     return *this;
  120. }
  121.  
  122. List::~List() {
  123.    
  124.     /*
  125.     Node* it = head;
  126.     while (it != nullptr) {
  127.         Node* nextIt = it->getNext();
  128.         delete it;
  129.         it = nextIt;
  130.     }
  131.     head = tail = nullptr;
  132.     */
  133.    
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement