Files
ARPZ_s1_pr6/task6.h

175 lines
4.2 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>
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;
}
}