Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. #include "List.h"
  2. #include <iostream>
  3.  
  4. //Default Constructor
  5. List::List()
  6. {
  7.     /*std::cout << "Default List Constructor called" << std::endl;*/
  8.     head = nullptr;
  9.     tail = nullptr;
  10.     size = 0;
  11. }
  12.  
  13. //Copy Constructor
  14. List::List(const List& other): size(0), head(nullptr), tail(nullptr)
  15. {
  16.     /*std::cout << "Copy List Constructor called" << std::endl;*/
  17.  
  18.     Node* temp = other.head;
  19.     while (size != other.size)
  20.     {
  21.         add(temp->getValue());
  22.         temp = temp->getNext();
  23.     }
  24. }
  25.  
  26. int List:: first() const
  27. {
  28.     return head->getValue();
  29. }
  30.  
  31.  
  32. // Generates new dynamically allocated Nodes
  33. //Links the list
  34.  
  35. void List::add(int value)
  36. {
  37.     if (size > 0 && size !=1)
  38.     {
  39.         Node* ptr = new Node{ value, tail, nullptr };
  40.         tail->setNext(ptr);
  41.         tail = ptr;
  42.     }
  43.     else if (size == 1)
  44.     {
  45.         Node* ptr = new Node{ value, tail, nullptr };
  46.         tail = ptr;    
  47.         head->setNext(ptr);
  48.     }
  49.     else
  50.     {
  51.         Node* ptr = new Node{ value, nullptr, nullptr };
  52.         head = ptr;
  53.         tail = ptr;
  54.     }
  55.     ++size;
  56.    
  57. }
  58. //Not used
  59. void List::addAll(const List& other)
  60. {
  61.     head = other.head;
  62.     tail = other.tail;
  63. }
  64.  
  65. //Releases resources for the first Node in the list
  66. void List::removeFirst()
  67. {
  68.     Node* temp = head->getNext();
  69.    
  70.     if (head != nullptr)
  71.     {
  72.         delete head;
  73.         head = temp;
  74.         --size;
  75.     }
  76.    
  77. }
  78.  
  79. //Removes and releases resources for all Nodes in the list
  80. //One by one
  81. void List::removeAll()
  82. {
  83.    
  84.     while (size!=0)
  85.     {  
  86.         removeFirst();
  87.     }
  88.        
  89. }
  90.  
  91. size_t List::getSize() const
  92. {
  93.     return size;
  94. }
  95.  
  96. bool List::isEmpty() const
  97. {
  98.     return !(size > 0);
  99. }
  100.  
  101. //Not used
  102.  List List::getReversed(List l)
  103. {
  104.      return l;
  105. }
  106.  
  107.  //Takes care for the output
  108.  std::string List:: toString() const
  109.  {
  110.      std::string out;
  111.      Node* tempHead = head;
  112.  
  113.      while (true)
  114.      {
  115.          if (tempHead != nullptr)
  116.          {
  117.              std::string temp = std::to_string(tempHead->getValue());
  118.              temp.append(" ");
  119.              tempHead = tempHead->getNext();
  120.              out.append(temp);
  121.          }
  122.          else
  123.          {
  124.              break;
  125.          }
  126.          
  127.      }
  128.      return out;
  129.  }
  130.  
  131. //OPERATOR OVERLOAD Calls the add function
  132. List& List::operator<<(const int& value)
  133. {
  134.     add(value);
  135.     return *this;
  136. }
  137.  
  138. //Not used
  139. List& List:: operator<<(const List& other)
  140. {
  141.     return *this;
  142. }
  143.  
  144. //ASSIGMENT OPERATOR - same as copy c-tor
  145. List& List::operator=(const List& other)
  146. {
  147.     /*std::cout << "Ass opp List called" << std::endl;*/
  148.     if (other.size == 0)
  149.     {
  150.         this->size = 0;
  151.         head = nullptr;
  152.         tail = nullptr;
  153.         return *this;
  154.     }
  155.     size = 0;
  156.    
  157.     Node* temp = other.head;
  158.     while (size != other.size)
  159.     {
  160.         add(temp->getValue());
  161.         temp = temp->getNext();
  162.     }
  163.    
  164.    
  165.     return*this;
  166. }
  167.  
  168. List::~List()
  169. {
  170.     /*std::cout << "Destructor List called" << std::endl;*/
  171.     this->removeAll();
  172.    
  173. }
  174.  
  175. //NODE CONSTRUCTOR
  176. List::Node::Node(int value, Node* prev, Node* next):
  177.     value(value), prev(prev), next(next)
  178. {}
  179.  
  180. int List::Node::getValue() const
  181. {
  182.     return this->value;
  183. }
  184.  
  185. void List::Node::setValue(int value)
  186. {
  187.     this->value = value;
  188. }
  189.  
  190.  
  191. List::Node* List::Node::getNext() const
  192. {
  193.     if (this != nullptr) {
  194.         if (this->next != nullptr)
  195.         {
  196.             return this->next;
  197.         }      
  198.     }
  199.     return nullptr;
  200. }
  201.  
  202.  
  203. void List::Node::setNext(Node* next)
  204. {
  205.     this->next = next;
  206. }
  207.  
  208. List::Node* List::Node::getPrev() const
  209. {
  210.     return this->prev;
  211. }
  212.  
  213. void List::Node::setPrev(Node* prev)
  214. {
  215.     this->prev = prev;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement