From 88b8efbceb71cf942ebd5bfcb89dab69d5a95814 Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Mon, 22 Dec 2025 05:25:34 +0300 Subject: [PATCH 1/6] Create list --- src/list/list.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ src/list/list.h | 27 ++++++++++ 2 files changed, 159 insertions(+) create mode 100644 src/list/list.c create mode 100644 src/list/list.h diff --git a/src/list/list.c b/src/list/list.c new file mode 100644 index 0000000..52b967a --- /dev/null +++ b/src/list/list.c @@ -0,0 +1,132 @@ +#include "list.h" +#include +#include + +struct ListNode { + int value; + ListNode* next; +}; + +static ListNode* createNode(int value) +{ + ListNode* node = (ListNode*)malloc(sizeof(ListNode)); + if (!node) + return NULL; + node->value = value; + node->next = NULL; + return node; +} + +List* listNew(void) +{ + List* list = (List*)malloc(sizeof(List)); + list->head = NULL; + return list; +} + +void listInsert(List* list, int index, int value) +{ + if (index == 0) { + ListNode* newNode = createNode(value); + if (!newNode) + return; + newNode->next = list->head; + list->head = newNode; + return; + } + + ListNode* current = list->head; + for (int i = 0; i < index - 1; ++i) { + if (!current) + return; + current = current->next; + } + + if (!current) + return; + + ListNode* newNode = createNode(value); + if (!newNode) + return; + newNode->next = current->next; + current->next = newNode; +} + +int listGet(List* list, int index) +{ + ListNode* current = list->head; + for (int i = 0; i < index; ++i) { + if (!current) + return 0; + current = current->next; + } + + return current->value; +} + +void listRemove(List* list, int index) +{ + if (!list || index < 0 || !list->head) + return; + + ListNode* toDelete = NULL; + + if (index == 0) { + toDelete = list->head; + list->head = list->head->next; + } else { + ListNode* current = list->head; + for (int i = 0; i < index - 1; ++i) { + if (!current || !current->next) + return; + current = current->next; + } + + if (!current || !current->next) + return; + + toDelete = current->next; + current->next = toDelete->next; + } + + free(toDelete); +} + +void listAppend(List* dest, List* src) +{ + if (!dest->head) { + dest->head = src->head; + } else { + ListNode* current = dest->head; + while (current->next) + current = current->next; + current->next = src->head; + } + + src->head = NULL; +} + +void listPrint(List* list) +{ + ListNode* current = list->head; + printf("["); + while (current) { + printf("%d", current->value); + if (current->next) + printf(", "); + current = current->next; + } + printf("]\n"); +} + +void listDelete(List* list) +{ + ListNode* current = list->head; + while (current) { + ListNode* next = current->next; + free(current); + current = next; + } + + free(list); +} diff --git a/src/list/list.h b/src/list/list.h new file mode 100644 index 0000000..fe92a67 --- /dev/null +++ b/src/list/list.h @@ -0,0 +1,27 @@ +#pragma once + +typedef struct ListNode ListNode; + +typedef struct { + ListNode* head; +} List; + +List* listNew(void); + +// Вставляет value на элемент списка под номером index +void listInsert(List* list, int index, int value); + +// Возвращает элемент по индексу +int listGet(List* list, int index); + +// Убирает элемент по индексу +void listRemove(List* list, int index); + +// Сцепляет 2 списка +void listAppend(List* list1, List* list2); + +// Распечатывает содержимое списка +void listPrint(List* list); + +// Удаляет список и освобождает память +void listDelete(List* list); From 6d600569914b99c3d61f0b0137b1f6d79f26845e Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Mon, 22 Dec 2025 05:57:03 +0300 Subject: [PATCH 2/6] Create sorted list --- src/28oct25/sorted_list.c | 89 +++++++++++++++++++++++++++++++++++++++ src/28oct25/sorted_list.h | 24 +++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/28oct25/sorted_list.c create mode 100644 src/28oct25/sorted_list.h diff --git a/src/28oct25/sorted_list.c b/src/28oct25/sorted_list.c new file mode 100644 index 0000000..04a1852 --- /dev/null +++ b/src/28oct25/sorted_list.c @@ -0,0 +1,89 @@ +#include "sorted_list.h" +#include +#include + +struct SortedListNode { + int value; + SortedListNode* next; +}; + +static SortedListNode* createNode(int value) +{ + SortedListNode* node = (SortedListNode*)malloc(sizeof(SortedListNode)); + node->value = value; + node->next = NULL; + return node; +} + +SortedList* sortedListNew(void) +{ + SortedList* list = (SortedList*)malloc(sizeof(SortedList)); + list->head = NULL; + return list; +} + +void sortedListInsert(SortedList* list, int value) +{ + SortedListNode* newNode = createNode(value); + + if (!list->head || list->head->value >= value) { + newNode->next = list->head; + list->head = newNode; + return; + } + + SortedListNode* current = list->head; + while (current->next && current->next->value < value) { + current = current->next; + } + + newNode->next = current->next; + current->next = newNode; +} + +void sortedListRemove(SortedList* list, int value) +{ + SortedListNode* current = list->head; + SortedListNode* prev = NULL; + + while (current && current->value != value) { + prev = current; + current = current->next; + } + + if (!current) + return; + + if (!prev) { + list->head = current->next; + } else { + prev->next = current->next; + } + + free(current); +} + +void sortedListPrint(SortedList* list) +{ + SortedListNode* current = list->head; + printf("["); + while (current) { + printf("%d", current->value); + if (current->next) + printf(", "); + current = current->next; + } + printf("]\n"); +} + +void sortedListDelete(SortedList* list) +{ + SortedListNode* current = list->head; + while (current) { + SortedListNode* next = current->next; + free(current); + current = next; + } + + free(list); +} diff --git a/src/28oct25/sorted_list.h b/src/28oct25/sorted_list.h new file mode 100644 index 0000000..f5a78fe --- /dev/null +++ b/src/28oct25/sorted_list.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +typedef struct SortedListNode SortedListNode; + +typedef struct { + SortedListNode* head; +} SortedList; + +// Создает новый отсортированный список +SortedList* sortedListNew(void); + +// Вставляет элемент в список, сохраняя порядок сортировки +void sortedListInsert(SortedList* list, int value); + +// Удаляет первое вхождение элемента с заданным значением +void sortedListRemove(SortedList* list, int value); + +// Распечатывает содержимое списка +void sortedListPrint(SortedList* list); + +// Удаляет список и освобождает память +void sortedListDelete(SortedList* list); From 195e7bb1ea22908246ef4edf9a8bda14ee6ba019 Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Mon, 22 Dec 2025 05:57:33 +0300 Subject: [PATCH 3/6] Sorted list CLI --- src/28oct25/main.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/28oct25/main.c diff --git a/src/28oct25/main.c b/src/28oct25/main.c new file mode 100644 index 0000000..d134a83 --- /dev/null +++ b/src/28oct25/main.c @@ -0,0 +1,46 @@ +#include "sorted_list.h" +#include + +int main(void) +{ + SortedList* list = sortedListNew(); + int choice = -1; + int value; + + printf("0 – выйти\n"); + printf("1 – добавить значение в сортированный список\n"); + printf("2 – удалить значение из списка\n"); + printf("3 – распечатать список\n"); + + while (choice != 0) { + printf("> "); + if (scanf("%d", &choice) != 1) { + while (getchar() != '\n'); + continue; + } + + switch (choice) { + case 1: + printf("Введите значение: "); + scanf("%d", &value); + sortedListInsert(list, value); + break; + case 2: + printf("Введите значение для удаления: "); + scanf("%d", &value); + sortedListRemove(list, value); + break; + case 3: + sortedListPrint(list); + break; + case 0: + break; + default: + printf("Неверный выбор!\n"); + break; + } + } + + sortedListDelete(list); + return 0; +} From 5c04d9bb36d35c6e7cecb4f223556516f33090fc Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Mon, 22 Dec 2025 06:22:08 +0300 Subject: [PATCH 4/6] Create loop list --- src/28oct25/loop_list.c | 106 ++++++++++++++++++++++++++++++++++++++++ src/28oct25/loop_list.h | 29 +++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/28oct25/loop_list.c create mode 100644 src/28oct25/loop_list.h diff --git a/src/28oct25/loop_list.c b/src/28oct25/loop_list.c new file mode 100644 index 0000000..00ae47f --- /dev/null +++ b/src/28oct25/loop_list.c @@ -0,0 +1,106 @@ +#include "loop_list.h" +#include +#include + + +static LoopListNode* createNode(int value) +{ + LoopListNode* node = (LoopListNode*)malloc(sizeof(LoopListNode)); + node->value = value; + node->next = NULL; + return node; +} + +LoopList* loopListNew(void) +{ + LoopList* list = (LoopList*)malloc(sizeof(LoopList)); + list->head = NULL; + return list; +} + +void loopListInsert(LoopList* list, int value) +{ + LoopListNode* newNode = createNode(value); + + if (!list->head) { + list->head = newNode; + newNode->next = newNode; + return; + } + + LoopListNode* current = list->head; + while (current->next != list->head) { + current = current->next; + } + + current->next = newNode; + newNode->next = list->head; +} + +void loopListRemove(LoopList* list, int value) +{ + LoopListNode* current = list->head; + LoopListNode* prev = NULL; + + do { + if (current->value == value) + break; + prev = current; + current = current->next; + } while (current != list->head); + + if (current->value != value) + return; + + if (current == list->head) { + if (current->next == list->head) { + list->head = NULL; + } else { + LoopListNode* last = list->head; + while (last->next != list->head) { + last = last->next; + } + list->head = current->next; + last->next = list->head; + } + } else { + prev->next = current->next; + } + + free(current); +} + +void loopListPrint(LoopList* list) +{ + printf("["); + if (list->head) { + LoopListNode* current = list->head; + do { + printf("%d", current->value); + if (current->next != list->head) + printf(", "); + current = current->next; + } while (current != list->head); + } + printf("]\n"); +} + +void loopListDelete(LoopList* list) +{ + if (list->head) { + LoopListNode* current = list->head; + LoopListNode* last = list->head; + while (last->next != list->head) { + last = last->next; + } + last->next = NULL; + + while (current) { + LoopListNode* next = current->next; + free(current); + current = next; + } + } + + free(list); +} diff --git a/src/28oct25/loop_list.h b/src/28oct25/loop_list.h new file mode 100644 index 0000000..8e7564b --- /dev/null +++ b/src/28oct25/loop_list.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +typedef struct LoopListNode LoopListNode; + +struct LoopListNode { + int value; + LoopListNode* next; +}; + +typedef struct { + LoopListNode* head; +} LoopList; + +// Создает новый циклический список +LoopList* loopListNew(void); + +// Вставляет элемент в конец циклического списка +void loopListInsert(LoopList* list, int value); + +// Удаляет первое вхождение элемента с заданным значением +void loopListRemove(LoopList* list, int value); + +// Распечатывает содержимое циклического списка +void loopListPrint(LoopList* list); + +// Удаляет список и освобождает память +void loopListDelete(LoopList* list); From 5272bb35a9c2c070ee1c71999a4ddf941d92c500 Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Mon, 22 Dec 2025 06:23:41 +0300 Subject: [PATCH 5/6] Solve counting rhyme problem --- src/28oct25/schitalochka.c | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/28oct25/schitalochka.c diff --git a/src/28oct25/schitalochka.c b/src/28oct25/schitalochka.c new file mode 100644 index 0000000..1f128ab --- /dev/null +++ b/src/28oct25/schitalochka.c @@ -0,0 +1,48 @@ +#include "loop_list.h" +#include +#include + +int main(void) +{ + int n, m; + scanf("%d %d", &n, &m); + LoopList* list = loopListNew(); + + for (int i = 1; i <= n; ++i) + loopListInsert(list, i); + + if (!list->head) { + loopListDelete(list); + return 0; + } + + LoopListNode* current = list->head; + + LoopListNode* prev = list->head; + while (prev->next != list->head) + prev = prev->next; + + printf("Порядок удаления: "); + while (list->head->next != list->head) { + for (int i = 0; i < m - 1; ++i) { + prev = current; + current = current->next; + } + + printf("%d ", current->value); + LoopListNode* toDelete = current; + + if (toDelete == list->head) + list->head = toDelete->next; + + prev->next = toDelete->next; + current = toDelete->next; + free(toDelete); + } + + printf("\nПоследний оставшийся: %d\n", list->head->value); + + loopListDelete(list); + + return 0; +} From 0c9c911a51447374e2ef65b8039a30174ddb4b90 Mon Sep 17 00:00:00 2001 From: Max Ipatov Date: Sat, 14 Feb 2026 03:46:13 +0300 Subject: [PATCH 6/6] Move list printer to main --- src/28oct25/main.c | 23 ++++++++++++++++++++++- src/28oct25/sorted_list.c | 27 +++++++++++++++++++++------ src/28oct25/sorted_list.h | 4 ++-- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/28oct25/main.c b/src/28oct25/main.c index d134a83..bd75a29 100644 --- a/src/28oct25/main.c +++ b/src/28oct25/main.c @@ -1,5 +1,26 @@ #include "sorted_list.h" #include +#include + +static void printList(SortedList* list) +{ + int size; + int* array = sortedListToArray(list, &size); + if (!array) { + printf("[]\n"); + return; + } + + printf("["); + for (int i = 0; i < size; i++) { + printf("%d", array[i]); + if (i < size - 1) + printf(", "); + } + printf("]\n"); + + free(array); +} int main(void) { @@ -31,7 +52,7 @@ int main(void) sortedListRemove(list, value); break; case 3: - sortedListPrint(list); + printList(list); break; case 0: break; diff --git a/src/28oct25/sorted_list.c b/src/28oct25/sorted_list.c index 04a1852..1203eda 100644 --- a/src/28oct25/sorted_list.c +++ b/src/28oct25/sorted_list.c @@ -63,17 +63,32 @@ void sortedListRemove(SortedList* list, int value) free(current); } -void sortedListPrint(SortedList* list) +int* sortedListToArray(SortedList* list, int* outSize) { + // Считаем количество элементов + int size = 0; SortedListNode* current = list->head; - printf("["); while (current) { - printf("%d", current->value); - if (current->next) - printf(", "); + size++; current = current->next; } - printf("]\n"); + + // Выделяем память под массив + int* array = (int*)malloc(size * sizeof(int)); + if (!array) { + *outSize = 0; + return NULL; + } + + // Копируем значения + current = list->head; + for (int i = 0; i < size; i++) { + array[i] = current->value; + current = current->next; + } + + *outSize = size; + return array; } void sortedListDelete(SortedList* list) diff --git a/src/28oct25/sorted_list.h b/src/28oct25/sorted_list.h index f5a78fe..a4e950d 100644 --- a/src/28oct25/sorted_list.h +++ b/src/28oct25/sorted_list.h @@ -17,8 +17,8 @@ void sortedListInsert(SortedList* list, int value); // Удаляет первое вхождение элемента с заданным значением void sortedListRemove(SortedList* list, int value); -// Распечатывает содержимое списка -void sortedListPrint(SortedList* list); +// Возвращает содержимое списка в виде массива (caller должен освободить память) +int* sortedListToArray(SortedList* list, int* outSize); // Удаляет список и освобождает память void sortedListDelete(SortedList* list);