Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by alex on 04.05.19.
- //
- #pragma once
- //#ifndef LAB_12_MYLIST_H
- //#define LAB_12_MYLIST_H
- template <typename T>
- class MyList{
- public:
- struct Node{
- T data;
- Node *next, *prev;
- public:
- Node()
- {
- this->next = nullptr;
- this->prev = nullptr;
- }
- Node(T data, Node* next, Node *prev)
- {
- this->data = data;
- this->next = next;
- this->prev = prev;
- }
- };
- int amount;
- Node *head, *tail;
- MyList();
- MyList(const MyList<T> &);
- ~MyList();
- int GetLength();
- MyList<T>& PushBack(T data);
- MyList<T>& PopBack();
- MyList<T>& PushFront(T data);
- MyList<T>& PopFront();
- MyList<T>& Sort();
- MyList<T>& Map(T map_func(T obj));
- MyList<T>& Where(bool where_func(T obj));
- void Swap(Node *first_node, Node *second_node);
- void PrintList();
- };
- template <typename T>
- MyList<T>::MyList()
- {
- this->head = nullptr;
- this->tail = nullptr;
- this->amount = 0;
- };
- template <typename T>
- MyList<T>::MyList(const MyList<T> &my_list) // TODO переделать конструктор копирования
- {
- this->head = nullptr;
- this->tail = nullptr;
- this->amount = 0;
- Node *node = my_list.head;
- while(node != nullptr)
- {
- Node *new_node = new Node();
- new_node->data = node->data;
- new_node->next = nullptr;
- new_node->prev = tail;
- node = node->next;
- }
- amount = my_list.amount;
- };
- template <typename T>
- MyList<T>::~MyList() = default;
- template <typename T>
- MyList<T>& MyList<T>::PushBack(T data)
- {
- Node *new_node = new Node();
- new_node->data = data;
- new_node->next = nullptr;
- new_node->prev = tail;
- if (tail != nullptr) {
- tail->next = nullptr;
- tail->next = new_node;
- }
- else{
- head = new_node;
- }
- tail = new_node;
- amount++;
- return *this;
- };
- template <typename T>
- MyList<T>& MyList<T>::PopBack()
- {
- if (amount == 1)
- {
- head = nullptr;
- tail = nullptr;
- }
- else if (amount == 0)
- {
- return *this;
- }
- else
- {
- tail = tail->prev;
- tail->next = nullptr;
- }
- amount--;
- return *this;
- };
- template <typename T>
- MyList<T>& MyList<T>::PushFront(T data)
- {
- Node *new_node = new Node();
- new_node->data = data;
- new_node->next = head;
- if (head != nullptr) {
- head->prev = new_node;
- }
- else
- {
- tail = new_node;
- }
- head = new_node;
- amount++;
- return *this;
- };
- template <typename T>
- void MyList<T>::PrintList()
- {
- Node *printing_node = this->head;
- std::cout << '{';
- for (int i = 0; i < amount; i++)
- {
- std::cout << printing_node->data;
- if (i == amount -1) break;
- std::cout << ", ";
- printing_node = printing_node->next;
- }
- std::cout << "}\n";
- }
- template <typename T>
- MyList<T>& MyList<T>::PopFront()
- {
- if (amount == 1)
- {
- head = nullptr;
- tail = nullptr;
- }
- else if (amount == 0)
- {
- return *this;
- }
- else
- {
- head = head->next;
- head->prev = nullptr;
- }
- amount--;
- return *this;
- };
- template <typename T>
- MyList<T>& MyList<T>::Sort()
- {
- Node *start_node = head;
- for (int i = 0; i < amount; i++)
- {
- Node* tmp_node = start_node->next;
- Node* min_elem_node = start_node;
- for (int j = i + 1; j < amount; j++)
- {
- if (tmp_node->data < min_elem_node->data) min_elem_node = tmp_node;
- if (j == amount - 1) break;
- tmp_node = tmp_node->next;
- }
- this->Swap(start_node, min_elem_node);
- if (i == amount - 1) break;
- start_node = start_node->next;
- }
- return *this;
- };
- template <typename T>
- int MyList<T>::GetLength()
- {
- return amount;
- }
- template<typename T>
- MyList<T>& MyList<T>::Map(T map_func(T obj))
- {
- Node *map_node = this->head;
- for (int i = 0; i < amount; i++)
- {
- map_node->data = map_func(map_node->data);
- if (i == amount -1) break;
- map_node = map_node->next;
- }
- return *this;
- }
- template<typename T>
- MyList<T>& MyList<T>::Where(bool where_func(T obj))
- {
- MyList<T> new_list = *new MyList<T>();
- Node *where_node = this->head;
- for(int i = 0; i < amount; i++)
- {
- if (where_func(where_node->data))
- {
- new_list.PushBack(where_node->data);
- }
- if (i == amount -1) break;
- where_node = where_node->next;
- }
- return new_list;
- }
- template<typename T>
- void MyList<T>::Swap(MyList::Node *first_node, MyList::Node *second_node)
- {
- if (first_node == second_node) return;
- T tmp_data = first_node->data;
- first_node->data = second_node->data;
- second_node->data = tmp_data;
- }
- //#endif //LAB_12_MYLIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement