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_;
- }
- }
- }
- //void List::operator+=(const int value)
- //{
- // if (head_ == nullptr)
- // {
- // Node* newNode = new Node(value);
- // head_ = newNode;
- // tail_ = newNode;
- // }
- // else if (head_->value_ > value)
- // {
- // Node* newNode = new Node(value, head_);
- // head_ = newNode;
- // }
- // else
- // {
- // for (Node* ptr = head_; head_ != nullptr; ptr = ptr->next_)
- // {
- // if (ptr->next_ != nullptr && ptr->next_->value_ > value)
- // {
- // Node* newNode = new Node(value, ptr->next_);
- // ptr->next_ = newNode;
- // break;
- // }
- // else if (ptr->next_ == nullptr)
- // {
- // Node* newNode = new Node(value);
- // ptr->next_ = newNode;
- // tail_ = newNode;
- // break;
- // }
- // else if (ptr->next_->value_ == value)
- // {
- // break;
- // }
- // }
- // }
- //}
- 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_;
- while (otherPtr != nullptr || thisPtr != nullptr)
- {
- if (thisPtr->value_ != otherPtr->value_)
- {
- return false;
- }
- thisPtr = thisPtr->next_;
- otherPtr = otherPtr->next_;
- }
- return true;
- }
- List operator&(List& firstList, 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_;
- }
- return resultList;
- }
- List operator|(List& firstList, 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)
- {
- Node* ptr = other.head_;
- while (ptr != nullptr)
- {
- *this += ptr->value_;
- ptr = ptr->next_;
- }
- }
- //void List::merge(List& other)
- //{
- // if (other.head_ != nullptr && head_ != nullptr)
- // if (other.head_->value_ < head_->value_)
- // {
- // Node* temp = other.head_->next_;
- // head_->next_ = other.head_;
- // }
- // else
- // for (Node* ptr = head_, *scptr = other.head_; ptr != nullptr && scptr != nullptr; ptr = ptr->next_)
- // {
- // if(ptr->value_ > scptr->value_)
- // }
- //}
- /*void List::push_back(int value)
- {
- Node* newNode = new Node(value);
- if (head_ == nullptr)
- {
- head_ = newNode;
- tail_ = newNode;
- minValue_ = value;
- }
- else {
- tail_->next_ = newNode;
- tail_ = newNode;
- if (value < minValue_)
- minValue_ = value;
- }
- }*/
- /*void List::removeMinimal()
- {
- // удаляет из списка элемент с наименьшим значением (если их несколько, то удалить надо все)
- Node* ptr = head_;
- Node* prevPtr = nullptr;
- int newMinimal = minValue_;
- while (ptr != nullptr)
- {
- if (ptr->value_ == minValue_)
- {
- if (ptr == head_)
- head_ = head_->next_;
- else
- prevPtr->next_ = ptr->next_;
- if (ptr == tail_)
- tail_ = prevPtr;
- Node* temp = ptr->next_;
- delete ptr;
- ptr = temp;
- }
- else
- {
- if (ptr->value_ < newMinimal || newMinimal == minValue_)
- newMinimal = ptr->value_;
- prevPtr = ptr;
- ptr = ptr->next_;
- }
- }
- minValue_ = newMinimal;
- }*/
- /*void List::removeRepeats()
- {
- Node* iNode = head_;
- while (iNode != nullptr)
- {
- Node* jNode = iNode->next_;
- Node* prevNode = iNode;
- while (jNode != nullptr)
- {
- if (iNode->value_ == jNode->value_) {
- prevNode->next_ = jNode->next_;
- Node* temp = jNode->next_;
- delete jNode;
- jNode = temp;
- }
- else
- {
- prevNode = jNode;
- jNode = jNode->next_;
- }
- }
- iNode = iNode->next_;
- }
- }*/
- /*void List::removeLess()
- {
- Node* iNode = head_;
- while (iNode != nullptr)
- {
- Node* jNode = iNode->next_;
- Node* prevNode = iNode;
- while (jNode != nullptr)
- {
- if (iNode->value_ > jNode->value_) {
- prevNode->next_ = jNode->next_;
- Node* temp = jNode->next_;
- delete jNode;
- jNode = temp;
- }
- else
- {
- prevNode = jNode;
- jNode = jNode->next_;
- }
- }
- iNode = iNode->next_;
- }
- }*/
- /*void List::reduce()
- {
- Node* ptr = head_;
- while (ptr != nullptr && ptr->next_ != nullptr)
- {
- ptr->value_ += ptr->next_->value_;
- Node* temp = ptr->next_->next_;
- delete ptr->next_;
- ptr->next_ = temp;
- ptr = temp;
- }
- }*/
- /*void List::insert(const List& other)
- {
- Node* ptr = other.head_;
- while (ptr != nullptr)
- {
- Node* newNode = new Node(ptr->value_);
- if (head_ == nullptr)
- head_ = tail_ = newNode;
- else
- {
- tail_->next_ = newNode;
- tail_ = newNode;
- }
- ptr = ptr->next_;
- }
- if (other.minValue_ < minValue_)
- minValue_ = other.minValue_;
- }*/
- /*void List::insertSorted(int value)
- {
- if (head_ == nullptr)
- {
- Node* newNode = new Node(value);
- head_ = newNode;
- tail_ = newNode;
- minValue_ = value;
- return;
- }
- if (head_->value_ > value)
- {
- Node* newNode = new Node(value, head_);
- head_ = newNode;
- minValue_ = value;
- 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_;
- }
- }*/
- /*void List::joinSorted(const List& other)
- {
- Node* ptr = other.head_;
- while (ptr != nullptr)
- {
- insertSorted(ptr->value_);
- ptr = ptr->next_;
- }
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement