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); diff --git a/src/28oct25/main.c b/src/28oct25/main.c new file mode 100644 index 0000000..bd75a29 --- /dev/null +++ b/src/28oct25/main.c @@ -0,0 +1,67 @@ +#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) +{ + 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: + printList(list); + break; + case 0: + break; + default: + printf("Неверный выбор!\n"); + break; + } + } + + sortedListDelete(list); + return 0; +} 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; +} diff --git a/src/28oct25/sorted_list.c b/src/28oct25/sorted_list.c new file mode 100644 index 0000000..1203eda --- /dev/null +++ b/src/28oct25/sorted_list.c @@ -0,0 +1,104 @@ +#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); +} + +int* sortedListToArray(SortedList* list, int* outSize) +{ + // Считаем количество элементов + int size = 0; + SortedListNode* current = list->head; + while (current) { + size++; + current = current->next; + } + + // Выделяем память под массив + 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) +{ + 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..a4e950d --- /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); + +// Возвращает содержимое списка в виде массива (caller должен освободить память) +int* sortedListToArray(SortedList* list, int* outSize); + +// Удаляет список и освобождает память +void sortedListDelete(SortedList* list); 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);