#include #include #include using namespace std; #pragma once namespace task7 { struct Node { public: int key; Node* next; }; Node* newNode(int key); Node* rand_list(int size); void print_list(Node* head); bool is_list_empty(Node* head); void push_back(Node* head, int key); void push_forward(Node*& head, int key); void squeeze_in(Node* head, int pos, int key); void behead(Node*& head); int list_size(Node* head); bool equal(Node* head1, Node* head2); bool all_in(Node* head1, Node* head2); bool dupes_present(Node* head); void head_to_end(Node*& head); void end_to_head(Node*& head); void merge(Node* head1, Node* head2); void reversed(Node*& head); bool in_list(Node* head, int key); void remove_dupes(Node*& head); // not implemented int init() { cout << "TASK #7" << endl; setlocale(LC_ALL, "Russian"); int n1; cout << "Длина списка 1: "; cin >> n1; Node* lst1 = newNode(0); cout << "Элементы списка 1: " << endl; for (int i = 0; i < n1; i++) { int num; cin >> num; push_back(lst1, num); } int n2; cout << "Длина списка 2: "; cin >> n2; Node* lst2 = newNode(0); cout << "Элементы списка 2:" << endl; for (int i = 0; i < n2; i++) { int num; cin >> num; push_back(lst2, num); } behead(lst1); behead(lst2); cout << "Списки: \n"; print_list(lst1); print_list(lst2); cout << "\n\n"; if (equal(lst1, lst2)) { cout << "Списки равны\n"; } else { cout << "Списки не равны\n"; } cout << "\n\n"; if (all_in(lst1, lst2)) { cout << "Все элементы списка 1 входят в список 2\n"; } else { cout << "Не все элементы списка 1 входят в список 2\n"; } cout << "\n\n"; if (dupes_present(lst1)) { cout << "В списке 1 есть повторяющиеся элементы"; } else { cout << "В списке 1 нет повторяющихся элементов"; } cout << "\n\n"; print_list(lst1); cout << "Перенесем первый элемент в конец: \n"; head_to_end(lst1); print_list(lst1); cout << "\n\n"; print_list(lst2); cout << "Перенесем последний элемент в начало : \n"; end_to_head(lst2); print_list(lst2); cout << "\n\n"; print_list(lst1); print_list(lst2); cout << "Перенесем все элементы списка 2 в конец списка 1: \n"; merge(lst1, lst2); print_list(lst1); cout << "Развернем список: \n"; reversed(lst1); print_list(lst1); cout << "Уберем повторяющиеся элементы: \n"; remove_dupes(lst1); print_list(lst1); return 0; } Node* newNode(int key) { Node* node = new Node; node->key = key; node->next = nullptr; return node; } Node* rand_list(int size) { Node* head = newNode(rand()); Node* last = newNode(rand()); head->next = last; for (int i = 0; i < size; i++) { Node* node = newNode(i); last->next = node; last = node; } return head->next->next; } void print_list(Node* head) { Node* ptr = head; while (ptr) { cout << ptr->key << " -> "; ptr = ptr->next; } cout << "nullptr" << endl; } bool is_list_empty(Node* head) { bool empty = true; while (head) { empty = false; break; } return empty; } void push_back(Node* head, int key) { Node* ptr = head; while (ptr->next) { ptr = ptr->next; } ptr->next = newNode(key); } void push_forward(Node*& head, int key) { Node* node = newNode(key); node->next = head; head = node; } void squeeze_in(Node* head, int pos, int key) { Node* node_left = head; Node* node_right; Node* node = newNode(key); for (int i = 0; i < pos - 1; i++) { node_left = node_left->next; } node_right = node_left->next->next; node_left->next = node; node->next = node_right; } void behead(Node*& head) { Node* _head = head; head=head->next; delete _head; } int list_size(Node* head) { int num = 0; Node* ptr = head; while (ptr) { ptr = ptr->next; num++; } return num; } bool equal(Node* head1, Node* head2) { if (list_size(head1) != list_size(head2)) { return false; } Node* ptr1 = head1; Node* ptr2 = head2; while (ptr1) { if (ptr1->key != ptr2->key) { return false; } ptr1 = ptr1->next; ptr2 = ptr2->next; } return true; } bool all_in(Node* head1, Node* head2) { Node* ptr1 = head1; while (ptr1) { bool is_in = false; Node* ptr2 = head2; while (ptr2) { if (ptr1->key == ptr2->key) { is_in = true; break; } ptr2 = ptr2->next; } if (!is_in) { return false; } ptr1 = ptr1->next; } return true; } bool dupes_present(Node* head) { Node* ptr1 = head; while (ptr1) { int count = -1; Node* ptr2 = head; while (ptr2) { if (ptr1->key == ptr2->key) { count++; if (count > 1) { return true; } } ptr2 = ptr2->next; } ptr1 = ptr1->next; } return false; } void head_to_end(Node*& head) { Node* _head = head; Node* ptr = head; while (ptr->next) { ptr = ptr->next; } ptr->next = _head; head = head->next; _head->next = NULL; } void end_to_head(Node*& head) { Node* ptr = head; while (ptr->next->next) { ptr = ptr->next; } ptr->next->next = head; head = ptr->next; ptr->next = NULL; } void merge(Node* head1, Node* head2) { Node* ptr = head1; while (ptr->next) { ptr = ptr->next; } Node* ptr2 = head2; while (ptr2) { ptr->next = ptr2; ptr = ptr2; ptr2 = ptr2->next; } } void reversed(Node*& head) { Node* rev = nullptr; Node* ptr = head; while (ptr) { push_forward(rev, ptr->key); ptr = ptr->next; } head = rev; } bool in_list(Node* head, int key) { Node* ptr = head; while (ptr) { if (ptr->key == key) { return true; } ptr = ptr->next; } return false; } void remove_dupes(Node*& head) { Node* buffer = newNode(head->key); Node* result = newNode(head->key); while (head) { if (!in_list(buffer, head->key)) { push_back(result, head->key); push_back(buffer, head->key); } head = head->next; } head = result; } }