From 3ca5fe60e74c8074bdbab5d3cb647e7c2c113c92 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Fri, 14 Nov 2025 11:52:41 +0300 Subject: [PATCH 1/4] sorted list task --- 6/main_sorted_list.c | 42 +++++++++++++++++ 6/sorted_list.c | 108 +++++++++++++++++++++++++++++++++++++++++++ 6/sorted_list.h | 22 +++++++++ 3 files changed, 172 insertions(+) create mode 100644 6/main_sorted_list.c create mode 100644 6/sorted_list.c create mode 100644 6/sorted_list.h diff --git a/6/main_sorted_list.c b/6/main_sorted_list.c new file mode 100644 index 0000000..0ae850e --- /dev/null +++ b/6/main_sorted_list.c @@ -0,0 +1,42 @@ +#include "sorted_list.h" + +void main() { + List list; + initList(&list); + + printf("0 – выйти\n1 – добавить значение в сортированный список\n2 – удалить значение из списка\n3 – распечатать список\n\n"); + + char inp; + printf("Введите команду: "); + scanf("%c", &inp); + + while (inp != '0') { + int value; + switch (inp) { + case '1': + printf("Добавить: "); + scanf("%d", &value); + + addValue(&list, value); + + break; + + case '2': + printf("Удалить: "); + scanf("%d", &value); + + removeValue(&list, value); + + break; + + case '3': + printf("Список: "); + printList(&list); + break; + default: + printf("Введите команду: "); + break; + } + scanf("%c", &inp); + } +} diff --git a/6/sorted_list.c b/6/sorted_list.c new file mode 100644 index 0000000..39a9210 --- /dev/null +++ b/6/sorted_list.c @@ -0,0 +1,108 @@ +#include "sorted_list.h" + + +void initList(List* list) { + (*list).head = NULL; + (*list).size = 0; +} + +void addValue(List* list, int value) { + Node* head = (*list).head; + + if (head == NULL) { + Node* node = malloc(sizeof(Node)); + (*node).next = NULL; + (*node).value = value; + + (*list).head = node; + (*list).size++; + return; + } + + + + int current = (*head).value; + Node* buff = NULL; + + + while (current < value) { + if ((*head).next == NULL) { + Node* node = malloc(sizeof(Node)); + (*node).next = NULL; + (*node).value = value; + + (*head).next = node; + + (*list).size++; + return; + } + + + buff = head; + head = (*head).next; + + current = (*head).value; + } + + + Node* node = malloc(sizeof(Node)); + (*node).next = head; + (*node).value = value; + + (*list).size++; + + if (buff != NULL) { + (*buff).next = node; + } + + else { + (*list).head = node; + } +} + + +void removeValue(List* list, int value) { + Node* head = (*list).head; + + if (head == NULL) { + return; + } + + if ((*head).next == NULL) { + + } + + Node* buff = NULL; + + while (head != NULL) { + int listValue = (*head).value; + if (listValue == value) { + if (buff != NULL) { + (*buff).next = (*head).next; + } + + else { + (*list).head = (*head).next; + } + + free(head); + (*list).size--; + + return; + } + + buff = head; + head = (*head).next; + } +} + + +void printList(List* list) { + Node* head = (*list).head; + while (head != NULL) { + int value = (*head).value; + printf("%d ", value); + head = (*head).next; + } + printf("\n"); +} diff --git a/6/sorted_list.h b/6/sorted_list.h new file mode 100644 index 0000000..5194370 --- /dev/null +++ b/6/sorted_list.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include + + + +struct Node { + int value; + struct Node* next; + +} typedef Node; + + +struct List { + Node* head; + int size; +} typedef List; + +void initList(List* list); +void addValue(List* list, int value); +void removeValue(List* list, int value); +void printList(List* list); From 58e63ebac1cdc1b23e96bfcbd4288088c88a7465 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Sat, 22 Nov 2025 14:35:19 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B=20+=20=D0=B8?= =?UTF-8?q?=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 6/main_sorted_list.c | 70 +++++++++++++++++++++++++++++++++++++++++++- 6/sorted_list.c | 3 +- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/6/main_sorted_list.c b/6/main_sorted_list.c index 0ae850e..432b1a8 100644 --- a/6/main_sorted_list.c +++ b/6/main_sorted_list.c @@ -1,6 +1,74 @@ #include "sorted_list.h" -void main() { +#include + +void runAllTests(); +void testAddValue(List* list, int value, int except); +void testRemoveValue(List* list, int value, int except); + +void runAllTests() { + printf("--- Запуск тестов ---\n"); + + List list; + initList(&list); + + printf("AddValue\n"); + testAddValue(&list, 5, list.size + 1); + testAddValue(&list, 5, list.size + 1); + testAddValue(&list, 5, list.size + 1); + testAddValue(&list, 5, list.size + 1); + testAddValue(&list, -14, list.size + 1); + testAddValue(&list, 5, list.size + 1); + testAddValue(&list, 100, list.size + 1); + testAddValue(&list, 54, list.size + 1); + printf("\n"); + printf("RemoveValue\n"); + testRemoveValue(&list, 199, list.size); + testRemoveValue(&list, 5, list.size - 5); + testRemoveValue(&list, 199, list.size); + testRemoveValue(&list, 199, list.size); + testRemoveValue(&list, 5, list.size); + testRemoveValue(&list, 5, list.size); + testRemoveValue(&list, 5, list.size); + testRemoveValue(&list, 5, list.size); + testRemoveValue(&list, -14, list.size - 1); + testRemoveValue(&list, 100, list.size - 1); + testRemoveValue(&list, 54, list.size - 1); + testRemoveValue(&list, 5, list.size); + testRemoveValue(&list, 100, 0); + testRemoveValue(&list, 54, 0); + printf("--- Тесты завершены ---\n"); +} + + +void testAddValue(List* list, int value, int except) { + addValue(list, value); + + if (except != (*list).size) { + printf("Тест не пройден\n"); + exit(-1); + } + printList(list); +} + +void testRemoveValue(List* list, int value, int except) { + removeValue(list, value); + if (except != (*list).size) { + printf("Тест не пройден\n"); + exit(-1); + } + printList(list); +} + + +void main(int argc, char *argv[]) { + for (int i = 0; i < argc; i++) { + if (strcmp(argv[i], "--test") == 0) { + runAllTests(); + return ; + } + } + List list; initList(&list); diff --git a/6/sorted_list.c b/6/sorted_list.c index 39a9210..c5bf4f6 100644 --- a/6/sorted_list.c +++ b/6/sorted_list.c @@ -88,7 +88,8 @@ void removeValue(List* list, int value) { free(head); (*list).size--; - return; + head = (*list).head; + continue; } buff = head; From 90ee2d75bc21fdbd7f04e6e37265d81096d0cc10 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Thu, 25 Dec 2025 12:43:03 +0300 Subject: [PATCH 3/4] sorted lisst update --- 6/main_sorted_list.c | 127 +++++++---------------------- 6/sorted_list.c | 185 +++++++++++++++++++++---------------------- 6/sorted_list.h | 16 ++-- 3 files changed, 129 insertions(+), 199 deletions(-) diff --git a/6/main_sorted_list.c b/6/main_sorted_list.c index 432b1a8..ecd2d22 100644 --- a/6/main_sorted_list.c +++ b/6/main_sorted_list.c @@ -1,110 +1,43 @@ #include "sorted_list.h" -#include +void main(int argc, char* argv[]) +{ + List list; + initList(&list); -void runAllTests(); -void testAddValue(List* list, int value, int except); -void testRemoveValue(List* list, int value, int except); + printf("0 – выйти\n1 – добавить значение в сортированный список\n2 – удалить значение из списка\n3 – распечатать список\n\n"); -void runAllTests() { - printf("--- Запуск тестов ---\n"); + char inp; + printf("Введите команду: "); + scanf("%c", &inp); - List list; - initList(&list); - - printf("AddValue\n"); - testAddValue(&list, 5, list.size + 1); - testAddValue(&list, 5, list.size + 1); - testAddValue(&list, 5, list.size + 1); - testAddValue(&list, 5, list.size + 1); - testAddValue(&list, -14, list.size + 1); - testAddValue(&list, 5, list.size + 1); - testAddValue(&list, 100, list.size + 1); - testAddValue(&list, 54, list.size + 1); - printf("\n"); - printf("RemoveValue\n"); - testRemoveValue(&list, 199, list.size); - testRemoveValue(&list, 5, list.size - 5); - testRemoveValue(&list, 199, list.size); - testRemoveValue(&list, 199, list.size); - testRemoveValue(&list, 5, list.size); - testRemoveValue(&list, 5, list.size); - testRemoveValue(&list, 5, list.size); - testRemoveValue(&list, 5, list.size); - testRemoveValue(&list, -14, list.size - 1); - testRemoveValue(&list, 100, list.size - 1); - testRemoveValue(&list, 54, list.size - 1); - testRemoveValue(&list, 5, list.size); - testRemoveValue(&list, 100, 0); - testRemoveValue(&list, 54, 0); - printf("--- Тесты завершены ---\n"); -} + while (inp != '0') { + int value; + switch (inp) { + case '1': + printf("Добавить: "); + scanf("%d", &value); + addValue(&list, value); -void testAddValue(List* list, int value, int except) { - addValue(list, value); - - if (except != (*list).size) { - printf("Тест не пройден\n"); - exit(-1); - } - printList(list); -} + break; -void testRemoveValue(List* list, int value, int except) { - removeValue(list, value); - if (except != (*list).size) { - printf("Тест не пройден\n"); - exit(-1); - } - printList(list); -} + case '2': + printf("Удалить: "); + scanf("%d", &value); + removeValue(&list, value); -void main(int argc, char *argv[]) { - for (int i = 0; i < argc; i++) { - if (strcmp(argv[i], "--test") == 0) { - runAllTests(); - return ; - } - } + break; - List list; - initList(&list); - - printf("0 – выйти\n1 – добавить значение в сортированный список\n2 – удалить значение из списка\n3 – распечатать список\n\n"); - - char inp; - printf("Введите команду: "); - scanf("%c", &inp); - - while (inp != '0') { - int value; - switch (inp) { - case '1': - printf("Добавить: "); - scanf("%d", &value); - - addValue(&list, value); - - break; - - case '2': - printf("Удалить: "); - scanf("%d", &value); - - removeValue(&list, value); - - break; - - case '3': - printf("Список: "); - printList(&list); - break; - default: - printf("Введите команду: "); - break; - } - scanf("%c", &inp); + case '3': + printf("Список: "); + printList(&list); + break; + default: + printf("Введите команду: "); + break; } + scanf("%c", &inp); + } } diff --git a/6/sorted_list.c b/6/sorted_list.c index c5bf4f6..3ca7d5b 100644 --- a/6/sorted_list.c +++ b/6/sorted_list.c @@ -1,109 +1,106 @@ #include "sorted_list.h" - -void initList(List* list) { - (*list).head = NULL; - (*list).size = 0; +void initList(List* list) +{ + (*list).head = NULL; + (*list).size = 0; } -void addValue(List* list, int value) { - Node* head = (*list).head; - - if (head == NULL) { - Node* node = malloc(sizeof(Node)); - (*node).next = NULL; - (*node).value = value; - - (*list).head = node; - (*list).size++; - return; - } - - - - int current = (*head).value; - Node* buff = NULL; - - - while (current < value) { - if ((*head).next == NULL) { - Node* node = malloc(sizeof(Node)); - (*node).next = NULL; - (*node).value = value; - - (*head).next = node; - - (*list).size++; - return; - } - - - buff = head; - head = (*head).next; - - current = (*head).value; - } - - - Node* node = malloc(sizeof(Node)); - (*node).next = head; - (*node).value = value; - - (*list).size++; - - if (buff != NULL) { - (*buff).next = node; - } - - else { - (*list).head = node; - } +Node* createNode(int value, Node* next) +{ + Node* node = malloc(sizeof(Node)); + if (node == NULL) { + printf("Ошибка выделения памяти\n"); + exit(-1); + } + + (*node).next = next; + (*node).value = value; + + return node; } +void addValue(List* list, int value) +{ + Node* head = (*list).head; -void removeValue(List* list, int value) { - Node* head = (*list).head; + if (head == NULL) { + Node* node = createNode(value, NULL); - if (head == NULL) { - return; - } - + (*list).head = node; + (*list).size++; + return; + } + + Node* buff = NULL; + + while (head->value < value) { if ((*head).next == NULL) { - - } - - Node* buff = NULL; - - while (head != NULL) { - int listValue = (*head).value; - if (listValue == value) { - if (buff != NULL) { - (*buff).next = (*head).next; - } - - else { - (*list).head = (*head).next; - } - - free(head); - (*list).size--; - - head = (*list).head; - continue; - } - - buff = head; - head = (*head).next; + Node* node = createNode(value, NULL); + + (*head).next = node; + + (*list).size++; + return; } + + buff = head; + head = (*head).next; + } + + Node* node = createNode(value, head); + + (*list).size++; + + if (buff != NULL) { + (*buff).next = node; + } + + else { + (*list).head = node; + } } +void removeValue(List* list, int value) +{ + Node* iter = (*list).head; + + if (iter == NULL) { + return; + } -void printList(List* list) { - Node* head = (*list).head; - while (head != NULL) { - int value = (*head).value; - printf("%d ", value); - head = (*head).next; + Node* buff = NULL; + + while (iter != NULL) { + int listValue = (*iter).value; + if (listValue == value) { + if (buff != NULL) { + (*buff).next = (*iter).next; + } + + else { + (*list).head = (*iter).next; + } + + free(iter); + (*list).size--; + + iter = (*list).head; + continue; } - printf("\n"); + + buff = iter; + iter = (*iter).next; + } +} + +void printList(List* list) +{ + Node* iter = (*list).head; + while (iter != NULL) { + int value = (*iter).value; + printf("%d ", value); + iter = (*iter).next; + } + printf("\n"); } diff --git a/6/sorted_list.h b/6/sorted_list.h index 5194370..e8d60ba 100644 --- a/6/sorted_list.h +++ b/6/sorted_list.h @@ -2,21 +2,21 @@ #include #include - - struct Node { - int value; - struct Node* next; - -} typedef Node; + int value; + struct Node* next; +} typedef Node; struct List { - Node* head; - int size; + Node* head; + int size; } typedef List; void initList(List* list); +Node* createNode(int value, Node* next); + void addValue(List* list, int value); void removeValue(List* list, int value); + void printList(List* list); From e4312d0d1c796fac6a14f67dc568afc611c6cae3 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Thu, 25 Dec 2025 16:48:39 +0300 Subject: [PATCH 4/4] Tests --- 6/main_sorted_list.c | 14 +++- 6/test/test_sorted_list.c | 162 ++++++++++++++++++++++++++++++++++++++ 6/test/test_sorted_list.h | 16 ++++ 3 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 6/test/test_sorted_list.c create mode 100644 6/test/test_sorted_list.h diff --git a/6/main_sorted_list.c b/6/main_sorted_list.c index ecd2d22..ca668e5 100644 --- a/6/main_sorted_list.c +++ b/6/main_sorted_list.c @@ -1,7 +1,15 @@ -#include "sorted_list.h" +#include "test/test_sorted_list.h" -void main(int argc, char* argv[]) +#include + +int main(int argc, char* argv[]) { + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--test") == 0) { + int res = testRunAll(); + return res; + } + } List list; initList(&list); @@ -40,4 +48,6 @@ void main(int argc, char* argv[]) } scanf("%c", &inp); } + + return 0; } diff --git a/6/test/test_sorted_list.c b/6/test/test_sorted_list.c new file mode 100644 index 0000000..f18cb15 --- /dev/null +++ b/6/test/test_sorted_list.c @@ -0,0 +1,162 @@ +#include "test_sorted_list.h" + +int compareIntAscending(const void* a, const void* b) +{ + int x = *((const int*)a); + int y = *((const int*)b); + + return x - y; +} + +void testInitList() +{ + List list; + initList(&list); + + int res = (list.head == NULL) && (list.size == 0); + + assert(res); +} + +void testCreateNode() +{ + int value = 5; + Node* node = createNode(value, NULL); + + int res = (node->next == NULL) && (node->value == value); + + assert(res); +} + +void testSize() +{ + int arr[] = { 0, 25, 25, -25, 2, 5, 6, 8 }; + int sizeArr = sizeof(arr) / sizeof(int); + + List list; + initList(&list); + + for (int i = 0; i < sizeArr; i++) { + addValue(&list, arr[i]); + } + + int sizeList = list.size; + + assert(sizeList == sizeArr); +} + +void testAddValueInSortedOrder() +{ + int arr[] = { 0, 25, 25, -25, 2, 5, 6, 8 }; + int sizeArr = sizeof(arr) / sizeof(int); + + List list; + initList(&list); + + for (int i = 0; i < sizeArr; i++) { + addValue(&list, arr[i]); + } + + qsort(arr, sizeArr, sizeof(int), compareIntAscending); + + Node* iter = list.head; + for (int i = 0; i < sizeArr; i++) { + assert(iter != NULL); + assert(iter->value == arr[i]); + + iter = iter->next; + } +} + +void testRemoveNoValueInList() +{ + int arr[] = { 0, 25, 25, -25, 2, 5, 6, 8 }; + int sizeArr = sizeof(arr) / sizeof(int); + + List list; + initList(&list); + + for (int i = 0; i < sizeArr; i++) { + addValue(&list, arr[i]); + } + + int oldSize = list.size; + + removeValue(&list, -2342); + + int newSize = list.size; + + assert(oldSize == newSize); +} + +void testRemoveValueOneTimeInList() +{ + int arr[] = { 0, 25, 25, -25, 2, 5, 6, 8 }; + int sizeArr = sizeof(arr) / sizeof(int); + + List list; + initList(&list); + + for (int i = 0; i < sizeArr; i++) { + addValue(&list, arr[i]); + } + + int oldSize = list.size; + + removeValue(&list, 0); + + int newSize = list.size; + + assert(oldSize - 1 == newSize); +} + +void testRemoveMoreOneTimeInList() +{ + int arr[] = { 0, 25, 25, 25, -25, 2, 5, 6, 8 }; + int sizeArr = sizeof(arr) / sizeof(int); + + List list; + initList(&list); + + for (int i = 0; i < sizeArr; i++) { + addValue(&list, arr[i]); + } + + int oldSize = list.size; + + removeValue(&list, 25); + + int newSize = list.size; + + assert(oldSize - 3 == newSize); +} + +int testRunAll() +{ + printf("---------- TESTS ----------\n"); + + testInitList(); + printf("Test 1 PASSED\n"); + + testCreateNode(); + printf("Test 2 PASSED\n"); + + testSize(); + printf("Test 3 PASSED\n"); + + testAddValueInSortedOrder(); + printf("Test 4 PASSED\n"); + + testRemoveNoValueInList(); + printf("Test 5 PASSED\n"); + + testRemoveValueOneTimeInList(); + printf("Test 6 PASSED\n"); + + testRemoveMoreOneTimeInList(); + printf("Test 7 PASSED\n"); + + printf("\n\nALL TESTS PASSED\n"); + + return EXIT_SUCCESS; +} diff --git a/6/test/test_sorted_list.h b/6/test/test_sorted_list.h new file mode 100644 index 0000000..da16c18 --- /dev/null +++ b/6/test/test_sorted_list.h @@ -0,0 +1,16 @@ +#pragma once +#include "../sorted_list.h" + +#include + +int compareIntAscending(const void* a, const void* b); + +void testInitList(); +void testCreateNode(); +void testSize(); +void testAddValueInSortedOrder(); +void testRemoveNoValueInList(); +void testRemoveValueOneTimeInList(); +void testRemoveMoreOneTimeInList(); + +int testRunAll();