Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "List.h"
- List::List()
- {
- head_ = nullptr;
- tail_ = nullptr;
- }
- void List::clear()
- {
- while (head_ != nullptr)
- {
- Node* temp = head_->next_;
- delete head_;
- head_ = temp;
- }
- tail_ = nullptr;
- }
- List::~List()
- {
- clear();
- }
- List::List(List&& old) noexcept
- {
- clear();
- head_ = old.head_;
- old.head_ = nullptr;
- }
- void List::operator+=(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::operator+(int value)
- {
- if (head_ == nullptr)
- {
- head_ = new Node;
- head_->value_ = value;
- head_->next_ = nullptr;
- }
- else
- {
- Node* newNode = new Node;
- newNode->value_ = value;
- newNode->next_ = head_;
- head_ = newNode;
- }
- }
- std::ostream& operator<< (std::ostream& out, const List& list)
- {
- List::Node* ptr = list.head_;
- while (ptr != nullptr)
- {
- out << ptr->value_ << endl;
- ptr = ptr->next_;
- }
- return out;
- }
- 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_;
- }
- }
Add Comment
Please, Sign In to add comment