Advertisement
Guest User

Untitled

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