Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class List
- {
- private:
- struct Node
- {
- Node()
- {
- next_ = nullptr;
- item_ = 0;
- }
- Node* next_;
- int item_;
- };
- Node* head_;
- public:
- List();
- ~List();
- List operator+(int item);
- List operator+=(int item);
- friend ostream& operator << (ostream& out, const List& list);
- void removeLess();
- void removeMinimal();
- };
- List::List() : head_(nullptr) {};
- List::~List()
- {
- //while (head_)
- //{
- // Node* temp = head_;
- // head_ = head_->next_;
- // delete temp;
- //}
- }
- List List::operator+(int item)
- {
- if (head_ == nullptr)
- {
- head_ = new Node;
- head_->item_ = item;
- head_->next_ = nullptr;
- }
- else
- {
- Node* newNode = new Node;
- newNode->item_ = item;
- newNode->next_ = head_;
- head_ = newNode;
- }
- return *this;
- }
- List List::operator+=(int item)
- {
- if (head_ == nullptr)
- {
- head_ = new Node;
- head_->item_ = item;
- head_->next_ = nullptr;
- }
- else
- {
- Node* current = this->head_;
- while (current->next_ != nullptr)
- {
- current = current->next_;
- }
- current->next_ = new Node;
- current = current->next_;
- current->item_ = item;
- current->next_ = nullptr;
- }
- return *this;
- }
- ostream& operator <<(ostream& out, const List& list)
- {
- List::Node* temp = list.head_;
- while (temp != nullptr)
- {
- out << temp->item_ << endl;
- temp = temp->next_;
- }
- return out;
- }
- void List::removeLess()
- {
- if (head_ != nullptr)
- {
- Node* current = head_;
- Node* processed = head_->next_;
- while (processed != nullptr)
- {
- if (processed->item_ < current->item_)
- {
- current->next_ = processed->next_;
- delete processed;
- }
- else
- {
- current = processed;
- }
- processed = current->next_;
- }
- }
- else cout << endl << "List is empty!" << endl;
- }
- void List::removeMinimal()
- {
- if (head_ != nullptr)
- {
- Node* temp = head_;
- int min = temp->item_;
- while (temp != nullptr)
- {
- if (temp->item_ < min)
- {
- min = temp->item_;
- }
- temp = temp->next_;
- }
- temp = head_;
- while (temp != nullptr)
- {
- if (temp->item_ == min)
- {
- head_ = temp->next_;
- delete temp;
- temp = head_;
- }
- else break;
- }
- temp = head_;
- Node* tempNext = head_->next_;
- while (tempNext != nullptr)
- {
- if (tempNext->item_ == min)
- {
- temp->next_ = tempNext->next_;
- delete tempNext;
- tempNext = temp->next_;
- }
- else
- {
- temp = tempNext;
- tempNext = temp->next_;
- }
- }
- }
- else cout << "List is empty!";
- }
- int main()
- {
- List list;
- int value;
- for (int i = 0; i < 5; ++i)
- {
- cin >> value;
- list += value;
- }
- cout << endl << list << endl;
- //list.removeLess();
- //cout << endl << list << endl;
- list.removeMinimal();
- cout << endl << list << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement