Advertisement
Neveles

Untitled

Feb 13th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include "List.h"
  2.  
  3. List::List(){}
  4.  
  5. List::List(List&& old) noexcept
  6. {
  7.     clear();
  8.     head_ = old.head_;
  9.     old.head_ = nullptr;
  10. }
  11.  
  12. void List::clear()
  13. {
  14.     if (head_ != nullptr)
  15.     {
  16.         delete head_;
  17.     }
  18. }
  19.  
  20. List::~List()
  21. {
  22.     clear();
  23. }
  24.  
  25. void List::operator+=(const int value)
  26. {
  27.     if (head_ == nullptr)
  28.     {
  29.         Node* newNode = new Node(value);
  30.         head_ = newNode;
  31.         tail_ = newNode;
  32.         return;
  33.     }
  34.     for (Node* ptr = head_; ptr != nullptr; ptr = ptr->next_)
  35.     {
  36.         if (ptr->value_ == value)
  37.         {
  38.             return;
  39.         }
  40.     }
  41.     if (head_->value_ > value)
  42.     {
  43.         Node* newNode = new Node(value, head_);
  44.         head_ = newNode;
  45.         return;
  46.     }
  47.     Node* ptr = head_;
  48.     while (ptr != nullptr)
  49.     {
  50.         if (ptr->next_ == nullptr)
  51.         {
  52.             Node* newNode = new Node(value);
  53.             ptr->next_ = newNode;
  54.             tail_ = newNode;
  55.             return;
  56.         }
  57.         else if (ptr->next_->value_ > value)
  58.         {
  59.             Node* newNode = new Node(value, ptr->next_);
  60.             ptr->next_ = newNode;
  61.             return;
  62.         }
  63.         else
  64.         {
  65.             ptr = ptr->next_;
  66.         }
  67.     }
  68. }
  69.  
  70. ostream& operator<<(ostream& out, const List& list)
  71. {
  72.     for (List::Node* ptr = list.head_; ptr != nullptr; ptr = ptr->next_)
  73.     {
  74.         out << ptr->value_ << endl;
  75.     }
  76.     return out;
  77. }
  78.  
  79. bool List::operator==(List& other)
  80. {
  81.     Node* otherPtr = other.head_;
  82.     Node* thisPtr = head_;
  83.  
  84.     if ((otherPtr != nullptr && thisPtr == nullptr) || (otherPtr == nullptr && thisPtr != nullptr))
  85.     {
  86.         return false;
  87.     }
  88.     if (otherPtr == nullptr && thisPtr == nullptr)
  89.     {
  90.         return true;
  91.     }
  92.  
  93.     while (otherPtr->next_ != nullptr && thisPtr->next_ != nullptr)
  94.     {
  95.         if (thisPtr->value_ != otherPtr->value_)
  96.         {
  97.             return false;
  98.         }
  99.         thisPtr = thisPtr->next_;
  100.         otherPtr = otherPtr->next_;
  101.     }
  102.  
  103.     if ((otherPtr->next_ != nullptr && thisPtr->next_ == nullptr) || (otherPtr->next_ == nullptr && thisPtr->next_ != nullptr))
  104.     {
  105.         return false;
  106.     }
  107.     else
  108.     {
  109.         return true;
  110.     }
  111. }
  112.  
  113. List operator&(const List& firstList, const List& secondList)
  114. {
  115.     List resultList;
  116.     List::Node* firstPtr = firstList.head_;
  117.     List::Node* secondPtr = secondList.head_;
  118.     while (firstPtr != nullptr)
  119.     {
  120.         while (secondPtr != nullptr)
  121.         {
  122.             if (firstPtr->value_ == secondPtr->value_)
  123.             {
  124.                 resultList += firstPtr->value_;
  125.                 break;
  126.             }
  127.             secondPtr = secondPtr->next_;
  128.         }
  129.         firstPtr = firstPtr->next_;
  130.         secondPtr = secondList.head_;
  131.     }
  132.     return resultList;
  133. }
  134.  
  135. List operator|(const List& firstList, const List& secondList)
  136. {
  137.     List resultList;
  138.     List::Node* firstPtr = firstList.head_;
  139.     List::Node* secondPtr = secondList.head_;
  140.  
  141.     while (firstPtr != nullptr)
  142.     {
  143.         resultList += firstPtr->value_;
  144.         firstPtr = firstPtr->next_;
  145.     }
  146.     while (secondPtr != nullptr)
  147.     {
  148.         resultList += secondPtr->value_;
  149.         secondPtr = secondPtr->next_;
  150.     }
  151.     return resultList;
  152. }
  153.  
  154. void List::merge(List& other)
  155. {
  156.     if (other.head_ != nullptr && head_ != nullptr)
  157.     {
  158.         if (other.head_->value_ < head_->value_)
  159.         {
  160.             Node* temp = head_;
  161.             head_ = other.head_;
  162.             other.head_ = other.head_->next_;
  163.             head_->next_ = temp;
  164.         }
  165.         else if (other.head_->value_ == head_->value_)
  166.         {
  167.             Node* temp = head_;
  168.             temp = other.head_->next_;
  169.             other.head_->next_ = nullptr;
  170.             delete other.head_;
  171.             other.head_ = temp;
  172.         }
  173.         for (Node* ptr = head_, *temp = other.head_->next_; ptr != nullptr && other.head_ != nullptr; ptr = ptr->next_)
  174.         {
  175.             if (ptr->next_ == nullptr)
  176.             {
  177.                 tail_->next_ = other.head_;
  178.                 tail_ = other.head_;
  179.                 other.head_ = other.head_->next_;
  180.             }
  181.             else if (other.head_->value_ == ptr->next_->value_)
  182.             {
  183.                 temp = other.head_->next_;
  184.                 other.head_->next_ = nullptr;
  185.                 delete other.head_;
  186.                 other.head_ = temp;
  187.             }
  188.             else if (other.head_->value_ < ptr->next_->value_)
  189.             {
  190.                 temp = other.head_->next_;
  191.                 other.head_->next_ = ptr->next_;
  192.                 ptr->next_ = other.head_;
  193.                 other.head_ = temp;
  194.             }
  195.         }
  196.     }
  197.     other.tail_ = nullptr;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement