Files
ARPZ_s1_pr6/task7.h

396 lines
8.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
#include <string>
#include <cmath>
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); //пишет, что список пуст, если подкинуть nullptr
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); //переносит элементы списка 2 в конец списка 1
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");
cout << "В конце задания 6 ничего не предвещало беды\n";
int n1; cout << "Введите размер списка 1: "; cin >> n1;
Node* lst1 = newNode(0);
cout << "Введите числа:" << 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 << "Введите числа:" << endl;
for (int i = 0; i < n2; i++)
{
int num; cin >> num;
push_back(lst2, num);
}
behead(lst1);
behead(lst2);
cout << "\n\nИмеем два списка: \n";
print_list(lst1);
print_list(lst2);
cout << "\n\n";
if (equal(lst1, lst2))
{
cout << "Списки совпадают";
}
else
{
cout << "Списки не равны";
}
cout << "\n\n";
if (all_in(lst1, lst2))
{
cout << "Все элементы первого списка есть во втором";
}
else
{
cout << "Первый список имеет элементы, которых нет во втором";
}
cout << "\n\n";
if (dupes_present(lst1))
{
cout << "В списке есть дубликаты";
}
else
{
cout << "В списке нет дубликатов";
}
cout << "\n\n";
print_list(lst1);
cout << "Перенесем первый элемент списка 1 в конец : \n";
head_to_end(lst1);
print_list(lst1);
cout << "\n\n";
print_list(lst2);
cout << "Перенесем последний элемент списка 2 в начало : \n";
end_to_head(lst2);
print_list(lst2);
cout << "\n\n";
print_list(lst1);
print_list(lst2);
cout << "Соединим два списка: \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;
}
}