Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "List.h"
- using namespace std;
- int main()
- {
- List randomList;
- List userList;
- List mergedList;
- for (int i = 0; i < 5; ++i)
- {
- randomList += rand() % 10;
- }
- cout << "<User list input>" << endl;
- int value = 0;
- for (int i = 0; i < 5; ++i)
- {
- cin >> value;
- userList += value;
- }
- cout << endl << "<Merge list input>" << endl;
- for (int i = 0; i < 5; ++i)
- {
- cin >> value;
- mergedList += value;
- }
- mergedList.merge(userList);
- cout << endl << "Random list:" << endl << randomList << endl;
- cout << "User list:" << endl << userList << endl;
- cout << "Merged list:" << endl << mergedList << endl;
- return 0;
- }
- #pragma once
- #include <iostream>
- using namespace std;
- class List
- {
- private:
- struct Node
- {
- int value_;
- Node* next_;
- Node* prev_;
- Node(int value = 0, Node * next = nullptr, Node* prev = nullptr) : value_(value), next_(next), prev_(prev) {};
- ~Node()
- {
- if (next_ != nullptr)
- delete next_;
- }
- };
- Node* head_ = nullptr;
- Node* tail_ = nullptr;
- void clear();
- public:
- List();
- List(List&& old) noexcept;
- ~List();
- void operator+=(int value);
- friend ostream& operator<< (std::ostream& out, const List& list);
- bool operator==(int value);
- friend List operator&(List value, int value2);
- friend List operator|(List value, int value2);
- void merge(List& other);
- //void push_back(int value);
- //void removeMinimal();
- //void removeRepeats();
- //void removeLess();
- //void reduce();
- //void insert(const List& other);
- //void insertSorted(int value);
- //void joinSorted(const List& other);
- };
- #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+=(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<< (std::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==(int value)
- {
- return true;
- }
- List operator&(List value, int value2)
- {
- List list;
- return list;
- }
- List operator|(List value, int value2)
- {
- List list;
- return list;
- }
- 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