From 3ca5fe60e74c8074bdbab5d3cb647e7c2c113c92 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Fri, 14 Nov 2025 11:52:41 +0300 Subject: [PATCH 1/2] 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 b894a4deb158ce8e3230b7bdb16d272502049361 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Thu, 25 Dec 2025 12:43:03 +0300 Subject: [PATCH 2/2] sorted lisst update --- 6/main_sorted_list.c | 77 +++++++++--------- 6/sorted_list.c | 184 +++++++++++++++++++++---------------------- 6/sorted_list.h | 16 ++-- 3 files changed, 138 insertions(+), 139 deletions(-) diff --git a/6/main_sorted_list.c b/6/main_sorted_list.c index 0ae850e..ecd2d22 100644 --- a/6/main_sorted_list.c +++ b/6/main_sorted_list.c @@ -1,42 +1,43 @@ #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); +void main(int argc, char* argv[]) +{ + 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 index 39a9210..3ca7d5b 100644 --- a/6/sorted_list.c +++ b/6/sorted_list.c @@ -1,108 +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--; - - return; - } - - 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);