Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template<typename T>
- class List {
- public:
- struct Node { // двусвязный список состоит из узлов
- T value; // узел хранит информативную часть
- Node* next; // указатель на следующий узел в списке
- Node* prev; // указатель на предыдущий узел
- };
- List() : size(0), head(nullptr), last(nullptr) {}
- List(const List& other) : size(other.size), head(nullptr), last(nullptr) {
- if (other.head != nullptr) {
- head = new Node{ other.head->value, nullptr, nullptr };
- Node* temp = head;
- Node* other_temp = other.head;
- while (other_temp->next != nullptr) {
- other_temp = other_temp->next;
- temp->next = new Node{ other_temp->value, nullptr, temp };
- temp = temp->next;
- }
- last = temp;
- }
- }
- List(List&& other) : size(other.size), head(other.head), last(other.last) {
- other.size = 0;
- other.head = nullptr;
- other.last = nullptr;
- }
- List& operator=(const List& other) {
- if (this != &other) {
- Clear();
- if (other.head != nullptr) {
- head = new Node{ other.head->value, nullptr, nullptr };
- Node* temp = head;
- Node* other_temp = other.head;
- while (other_temp->next != nullptr) {
- other_temp = other_temp->next;
- temp->next = new Node{ other_temp->value, nullptr, temp };
- temp = temp->next;
- }
- last = temp;
- size = other.size;
- }
- }
- return *this;
- }
- List& operator=(List&& other) {
- if (this != &other) {
- Clear();
- size = other.size;
- head = other.head;
- last = other.last;
- other.size = 0;
- other.head = nullptr;
- other.last = nullptr;
- }
- return *this;
- }
- void Push_front(const T& value) {
- if (size == 0) {
- last = head = new Node{ value, nullptr, nullptr };
- size++;
- return;
- }
- Node* temp = new Node{ value, head, nullptr };
- head->prev = temp;
- head = temp;
- size++;
- }
- void Push_back(const T& value) {
- if (size == 0) {
- last = head = new Node{ value, nullptr, nullptr };
- size++;
- return;
- }
- Node* temp = new Node{ value, nullptr, last };
- last->next = temp;
- last = temp;
- size++;
- }
- void Pop_front() {
- if (size > 0) {
- if (size == 1) {
- delete head;
- last = head = nullptr;
- size--;
- return;
- }
- head = head->next;
- delete head->prev;
- head->prev = nullptr;
- size--;
- }
- }
- void Pop_back() {
- if (size > 0) {
- if (size == 1) {
- delete head;
- last = head = nullptr;
- size--;
- return;
- }
- last = last->prev;
- delete last->next;
- last->next = nullptr;
- size--;
- }
- }
- void Print() {
- Node* temp = head;
- while (temp != nullptr) {
- cout << temp->value << " ";
- temp = temp->next;
- }
- cout << endl;
- }
- T& Front() {
- if (head != nullptr) {
- return head->value;
- }
- }
- T& Front() const {
- if (head != nullptr) {
- return head->value;
- }
- }
- T& Back() {
- if(last != nullptr) {
- return last->value;
- }
- }
- T& Back() const {
- if (last != nullptr) {
- return last->value;
- }
- }
- int Size() const {
- return size;
- }
- void Clear() {
- while (head != nullptr) {
- Pop_front();
- }
- }
- ~List() {
- Clear();
- }
- private:
- int size = 0;
- Node* head = nullptr;
- Node* last = nullptr;
- };
- int main() {
- List<int> list1;
- for (int i = 0; i < 10; i++) {
- list1.Push_front(i + 1);
- list1.Print();
- }
- cout << list1.Size() << endl;
- for (int i = 0; i < 3; i++) {
- list1.Pop_front();
- list1.Print();
- }
- cout << list1.Size() << endl;
- cout << "Front: " << list1.Front() << " Back: " << list1.Back() << endl;
- list1.Push_back(10);
- list1.Print();
- list1.Pop_back();
- list1.Print();
- cout << "list2: " << endl;
- List<int> list2(move(list1));
- list2.Print();
- cout << "list1: " << endl;
- list1.Print();
- }
- //Задание: реализовать конструктор перемещения, перемещающий оператор присваивания
Advertisement
Add Comment
Please, Sign In to add comment