Added my works from 2022

This commit is contained in:
2024-11-21 09:38:09 +03:00
parent 5341eed7ea
commit 42cb45db32
12 changed files with 1137 additions and 0 deletions

175
task6.h Normal file
View File

@@ -0,0 +1,175 @@
#include <iostream>
using namespace std;
#pragma once
namespace task6 {
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); //"всунуть" после элемента
int list_size(Node* head); //узнать размер списка
void behead(Node*& head); //получить список без первого элемента
int init()
{
setlocale(LC_ALL, "Russian");
int n; cout << "Введите размер: "; cin >> n;
Node* lst = rand_list(n);
if (is_list_empty(lst))
{
cout << "Список является пустым." << endl;
return 0;
}
cout << "Список содержит элементы: " << endl;
print_list(lst);
cout << "\n\nДобавим элемент 11111 в начало:" << endl;
push_forward(lst, 11111);
print_list(lst);
cout << "\n\nДобавим элемент 22222 в конец:" << endl;
push_back(lst, 22222);
print_list(lst);
cout << "\n\nДобавим элемент 33333 где-то в середине:" << endl;
squeeze_in(lst, n/2+1, 33333);
print_list(lst);
cout << "\n\nВсего элементов сейчас: " << list_size(lst) << endl;
cout << "\n\nПолучим список без первого элемента с помощью функции обезглавливания:" << endl;
behead(lst);
print_list(lst);
cout << "\n\nДа это жоско";
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-2; i++)
{
Node* node = newNode(rand());
last->next = node;
last = node;
}
return head;
}
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* last = nullptr;
Node* ptr = head;
while (ptr)
{
last = ptr;
ptr = ptr->next;
}
ptr = newNode(key);
last->next = ptr;
}
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;
}
int list_size(Node* head)
{
int num=0;
Node* ptr = head;
while (ptr)
{
ptr = ptr->next;
num++;
}
return num;
}
void behead(Node*& head)
{
/*
⢿⣿⣿⣿⣭⠹⠛⠛⠛⢿⣿⣿⣿⣿⡿⣿⠷⠶⠿⢻⣿⣛⣦⣙⠻⣿
⣿⣿⢿⣿⠏⠀⠀⡀⠀⠈⣿⢛⣽⣜⠯⣽⠀⠀⠀⠀⠙⢿⣷⣻⡀⢿
⠐⠛⢿⣾⣖⣤⡀⠀⢀⡰⠿⢷⣶⣿⡇⠻⣖⣒⣒⣶⣿⣿⡟⢙⣶⣮
⣤⠀⠀⠛⠻⠗⠿⠿⣯⡆⣿⣛⣿⡿⠿⠮⡶⠼⠟⠙⠊⠁⠀⠸⢣⣿
⣿⣷⡀⠀⠀⠀⠀⠠⠭⣍⡉⢩⣥⡤⠥⣤⡶⣒⠀⠀⠀⠀⠀⢰⣿⣿
⣿⣿⡽⡄⠀⠀⠀⢿⣿⣆⣿⣧⢡⣾⣿⡇⣾⣿⡇⠀⠀⠀⠀⣿⡇⠃
⣿⣿⣷⣻⣆⢄⠀⠈⠉⠉⠛⠛⠘⠛⠛⠛⠙⠛⠁⠀⠀⠀⠀⣿⡇⢸
⢞⣿⣿⣷⣝⣷⣝⠦⡀⠀⠀⠀⠀⠀⠀⠀⡀⢀⠀⠀⠀⠀⠀⠛⣿⠈
⣦⡑⠛⣟⢿⡿⣿⣷⣝⢧⡀⠀⠀⣶⣸⡇⣿⢸⣧⠀⠀⠀⠀⢸⡿⡆
⣿⣿⣷⣮⣭⣍⡛⠻⢿⣷⠿⣶⣶⣬⣬⣁⣉⣀⣀⣁⡤⢴⣺⣾⣽⡇
*/
head = head->next;
}
}