Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions 6.Lists/Task_1/Sorted-list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int value;
struct Node* next;
} Node;

Node* head = NULL;

// Функция для вставки значения
void add_value(int value) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->value = value;
new_node->next = NULL;

if (head == NULL || head->value >= value) {
new_node->next = head;
head = new_node;
return;
}

Node* current = head;
while (current->next != NULL && current->next->value < value) {
current = current->next;
}

new_node->next = current->next;
current->next = new_node;
}

// Функция удаления значения
void remove_value(int value) {
if (head == NULL) {
printf("Список пуст\n");
return;
}

Node* temp;
if (head->value == value) {
temp = head;
head = head->next;
free(temp);
printf("Значение %d удалено\n", value);
return;
}

Node* current = head;
while (current->next != NULL && current->next->value != value) {
current = current->next;
}

if (current->next == NULL) {
printf("Значение %d не найдено\n", value);
return;
}

temp = current->next;
current->next = temp->next;
free(temp);
printf("Значение %d удалено\n", value);
}

void print_list() {
if (head == NULL) {
printf("Список пуст\n");
return;
}

Node* current = head;
printf("Список: ");
while (current != NULL) {
printf("%d ", current->value);
current = current->next;
}
printf("\n");
}

// Функция освобождения памяти
void free_list() {
Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}

void clear_input_buffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
}

int main() {
int choice, value;
int input_count;

do {
printf("\nМеню:\n");
printf("0 - Выйти\n");
printf("1 - Добавить значение\n");
printf("2 - Удалить значение\n");
printf("3 - Распечатать список\n");
printf("Выбор: ");

input_count = scanf("%d", &choice);

if (input_count != 1) {
printf("Ошибка: введите число от 0 до 3\n");
clear_input_buffer();
continue;
}

switch (choice) {
case 0:
free_list();
printf("Выход\n");
break;
case 1:
printf("Введите значение для добавления: ");
if (scanf("%d", &value) != 1) {
printf("Ошибка: введите целое число\n");
clear_input_buffer();
} else {
add_value(value);
}
break;
case 2:
printf("Введите значение для удаления: ");
if (scanf("%d", &value) != 1) {
printf("Ошибка: введите целое число\n");
clear_input_buffer();
} else {
remove_value(value);
}
break;
case 3:
print_list();
break;
default:
printf("Введите число от 0 до 3\n");
}
} while (choice != 0);

return 0;
}
65 changes: 65 additions & 0 deletions 6.Lists/Task_2/Counting-rhyme.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int value;
struct Node* next;
} Node;

int main() {
int n, m;

printf("Введите количество воинов (n): ");
scanf("%d", &n);
printf("Введите шаг (m): ");
scanf("%d", &m);

if (n == 1) {
printf("Последний выживший: 1\n");
return 0;
}

Node* head = (Node*)malloc(sizeof(Node));
head->value = 1;

Node* prev = head;
for (int i = 2; i <= n; i++) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->value = i;
prev->next = new_node;
prev = new_node;
}
prev->next = head;

Node* current = head;
prev = NULL;

if (m == 1) {
prev = current;
while (prev->next != head) {
prev = prev->next;
}
}

int count = n;

while (count > 1) {
for (int i = 0; i < m - 1; i++) {
prev = current;
current = current->next;
}

Node* to_delete = current;
prev->next = current->next;
current = current->next;
free(to_delete);

count--;
}

printf("Последний выживший: %d\n", current->value);

free(current);

return 0;
}