Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "List.h"
- List::List(){}
- List::List(List&& old) noexcept
- {
- clear();
- head_ = old.head_;
- old.head_ = nullptr;
- }
- void List::clear()
- {
- if (head_ != nullptr)
- {
- delete head_;
- }
- }
- List::~List()
- {
- clear();
- }
- void List::operator+=(const int value)
- {
- if (head_ == nullptr)
- {
- Node* newNode = new Node(value);
- head_ = newNode;
- tail_ = newNode;
- return;
- }
- for (Node* ptr = head_; ptr != nullptr; ptr = ptr->next_)
- {
- if (ptr->value_ == value)
- {
- return;
- }
- }
- if (head_->value_ > value)
- {
- Node* newNode = new Node(value, head_);
- head_ = newNode;
- return;
- }
- Node* ptr = head_;
- while (ptr != nullptr)
- {
- if (ptr->next_ == nullptr)
- {
- Node* newNode = new Node(value);
- ptr->next_ = newNode;
- tail_ = newNode;
- return;
- }
- else if (ptr->next_->value_ > value)
- {
- Node* newNode = new Node(value, ptr->next_);
- ptr->next_ = newNode;
- return;
- }
- else
- {
- ptr = ptr->next_;
- }
- }
- }
- ostream& operator<<(ostream& out, const List& list)
- {
- for (List::Node* ptr = list.head_; ptr != nullptr; ptr = ptr->next_)
- {
- out << ptr->value_ << endl;
- }
- return out;
- }
- bool List::operator==(List& other)
- {
- Node* otherPtr = other.head_;
- Node* thisPtr = head_;
- if ((otherPtr != nullptr && thisPtr == nullptr) || (otherPtr == nullptr && thisPtr != nullptr))
- {
- return false;
- }
- if (otherPtr == nullptr && thisPtr == nullptr)
- {
- return true;
- }
- while (otherPtr->next_ != nullptr && thisPtr->next_ != nullptr)
- {
- if (thisPtr->value_ != otherPtr->value_)
- {
- return false;
- }
- thisPtr = thisPtr->next_;
- otherPtr = otherPtr->next_;
- }
- if ((otherPtr->next_ != nullptr && thisPtr->next_ == nullptr) || (otherPtr->next_ == nullptr && thisPtr->next_ != nullptr))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- List operator&(const List& firstList, const List& secondList)
- {
- List resultList;
- List::Node* firstPtr = firstList.head_;
- List::Node* secondPtr = secondList.head_;
- while (firstPtr != nullptr)
- {
- while (secondPtr != nullptr)
- {
- if (firstPtr->value_ == secondPtr->value_)
- {
- resultList += firstPtr->value_;
- break;
- }
- secondPtr = secondPtr->next_;
- }
- firstPtr = firstPtr->next_;
- secondPtr = secondList.head_;
- }
- return resultList;
- }
- List operator|(const List& firstList, const List& secondList)
- {
- List resultList;
- List::Node* firstPtr = firstList.head_;
- List::Node* secondPtr = secondList.head_;
- while (firstPtr != nullptr)
- {
- resultList += firstPtr->value_;
- firstPtr = firstPtr->next_;
- }
- while (secondPtr != nullptr)
- {
- resultList += secondPtr->value_;
- secondPtr = secondPtr->next_;
- }
- return resultList;
- }
- void List::merge(List& other)
- {
- if (other.head_ != nullptr && head_ != nullptr)
- {
- if (other.head_->value_ < head_->value_)
- {
- Node* temp = head_;
- head_ = other.head_;
- other.head_ = other.head_->next_;
- head_->next_ = temp;
- }
- else if (other.head_->value_ == head_->value_)
- {
- Node* temp = head_;
- temp = other.head_->next_;
- other.head_->next_ = nullptr;
- delete other.head_;
- other.head_ = temp;
- }
- for (Node* ptr = head_, *temp = other.head_->next_; ptr != nullptr && other.head_ != nullptr; ptr = ptr->next_)
- {
- if (ptr->next_ == nullptr)
- {
- tail_->next_ = other.head_;
- tail_ = other.head_;
- other.head_ = other.head_->next_;
- }
- else if (other.head_->value_ == ptr->next_->value_)
- {
- temp = other.head_->next_;
- other.head_->next_ = nullptr;
- delete other.head_;
- other.head_ = temp;
- }
- else if (other.head_->value_ < ptr->next_->value_)
- {
- temp = other.head_->next_;
- other.head_->next_ = ptr->next_;
- ptr->next_ = other.head_;
- other.head_ = temp;
- }
- }
- }
- other.tail_ = nullptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement