From 1b8550bb50abb30a723904f870d008989113da98 Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Wed, 19 Nov 2025 21:21:05 +0300 Subject: [PATCH 01/12] =?UTF-8?q?hw6:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=202=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=A1=D0=BE=D1=80=D1=82.=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20-=20=D0=BE=D1=81=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=BD=D0=BE=D0=B9=20=D0=B8=20=D0=B7=D0=B0=D0=B3=D0=BE?= =?UTF-8?q?=D0=BB=D0=BE=D0=B2=D0=BE=D1=87=D0=BD=D1=8B=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sortList.c | 247 +++++++++++++++++++++++++++++++++++++++++++++++++ src/sortList.h | 53 +++++++++++ 2 files changed, 300 insertions(+) create mode 100644 src/sortList.c create mode 100644 src/sortList.h diff --git a/src/sortList.c b/src/sortList.c new file mode 100644 index 0000000..e2dd3ce --- /dev/null +++ b/src/sortList.c @@ -0,0 +1,247 @@ +// +// Created by sasha on 19.11.2025. +// +#include +#include "sortList.h" +#include + +// Создание пустого списка +struct List* createEmptyList() +{ + struct List* list = malloc(sizeof(struct List)); + if (!list) { + fprintf(stderr, "Ошибка выделения памяти\n"); + exit(1); + } + list->head = NULL; + list->size = 0; + return list; +} + +// Получение элемента по индексу +int getElement(struct List* list, int i) +{ + // Проверка корректности индекса + if (i < 0 || i >= list->size) { + printf("Некорректный индекс: он не может быть < 0 или больше размера списка\n"); + return -1; + } + + struct Node* currNode = list->head; + while (i--) { + currNode = currNode->next; + } + return currNode->data; +} + +// Реализация команды 0 - выйти: +// Удаление всего списка для освобождения и выхода +void deleteList(struct List* list) +{ + struct Node* currNode = list->head; + // Удаление узлов + while (currNode) { + struct Node* tempNode = currNode; + currNode = currNode->next; + free(tempNode); + } + // Освобождение памяти + free(list); +} + +// Реализация команды 1 - добавить значение в сортированный список: +// Поиск позиции для вставки элемента в список +int findPosition(struct List* list, int element) +{ + if (list->size == 0) + return 0; + + struct Node* currNode = list->head; + int position = 0; + + // Ищем первую позицию, где след элемент больше нового значения + // Так как список отсортирован, то прошлый будет или больше или равен вставляемому + while (currNode && currNode->data < element) { + currNode = currNode->next; + position++; + } + return position; +} + +// Вставка элемента в список по заданному индексу +void insertElement(struct List* list, int i, int element) +{ + // Проверка корректности индекса + if (i < 0 || i > list->size) { + printf("Некорректный индекс: он не может быть < 0 или больше размера списка\n"); + return; + } + + // Создание нового узла + struct Node* newNode = malloc(sizeof(struct Node)); + if (!newNode) { + printf("Ошибка выделения памяти для саздания нового узла\n"); + return; + } + newNode->data = element; + // Вставка + if (i == 0) { + newNode->next = list->head; + list->head = newNode; + } else { + // Поиск позиции для вставки + struct Node* currNode = list->head; + int pos = 1; + while (pos < i) { + currNode = currNode->next; + pos++; + } + newNode->next = currNode->next; + currNode->next = newNode; + } + list->size++; +} + +// Реализация команды 2 – удалить значение из списка: +// Проверка наличия значения в списке +int checkElement(struct List* list, int element) +{ + struct Node* currNode = list->head; + int position = 0; + + while (currNode) { + if (currNode->data == element) { + return position; + } + currNode = currNode->next; + position++; + } + return -1; +} + +// Удаление элемента по индексу +void deleteElement(struct List* list, int i) +{ + // проверка корректности индекса + if (i < 0 || i >= list->size) { + printf("Некорректный индекс: он не может быть < 0 или больше размера списка\n"); + return; + } + struct Node* nodeForDeletion; + // Удаление из начала списка + if (i == 0) { + nodeForDeletion = list->head; + list->head = list->head->next; + } else { + // Поиск узла перед удаляемым + struct Node* currNode = list->head; + int pos = 1; + while (pos < i) { + currNode = currNode->next; + pos++; + } + nodeForDeletion = currNode->next; + currNode->next = nodeForDeletion->next; + } + // Освобождение памяти + free(nodeForDeletion); + list->size--; +} + +// Реализация команды 3 – распечатать список: +// Печать содержимого списка +void printList(struct List* list) +{ + if (list->size == 0) { + printf("Список пустой\n"); + return; + } + + struct Node* currNode = list->head; + printf("Содержание списка:\n"); + printf("["); + while (currNode) { + printf("%d", currNode->data); + if (currNode->next) printf(", "); + currNode = currNode->next; + } + printf("]\n"); +} + + +// Функция main для реализации выбора команд 0-3 пользователем +int main() +{ + struct List* list = createEmptyList(); + int command, inputElement; + + while (1) { + printf("Команды:\n"); + printf("0 - Выйти\n"); + printf("1 - Добавить значение в сортированный список\n"); + printf("2 - Удалить значение из списка\n"); + printf("3 - Распечатать список\n"); + printf("Выберите команду (0,1,2,3): "); + + + if (scanf("%d", &command) != 1) { + printf("Ошибка: вводите только цифры\n"); + int ch; + while ((ch = getchar()) != '\n'); + + continue; + } + + switch (command) { + case 0: + printf("Совершён выход\n"); + deleteList(list); + return 0; + + case 1: + printf("Введите значение для добавления: "); + + if (scanf("%d", &inputElement) != 1) { + printf("Ошибка: вводите только цифры\n"); + + int ch; + while ((ch = getchar()) != '\n'); + + continue; + } + + int position1 = findPosition(list, inputElement); + insertElement(list, position1, inputElement); + printf("Значение было добавлено на позицию %d\n", position1); + break; + + case 2: + printf("Введите значение для удаления:"); + + if (scanf("%d", &inputElement) != 1) { + printf("Ошибка: вводите только цифры\n"); + + int ch; + while ((ch = getchar()) != '\n'); + + continue; + } + + int position2 = checkElement(list, inputElement); + if (position2 != -1) { + deleteElement(list, position2); + printf("Значение было удалено с позиции %d\n", position2); + } else { + printf("Значение не найдено\n"); + } + break; + + case 3: + printList(list); + break; + + default: + printf("Введена неподдерживаемая команда: можно только 0,1,2,3\n"); + } + } +} \ No newline at end of file diff --git a/src/sortList.h b/src/sortList.h new file mode 100644 index 0000000..2eb518d --- /dev/null +++ b/src/sortList.h @@ -0,0 +1,53 @@ +// +// Created by sasha on 19.11.2025. +// + +#ifndef SORTLIST_MAIN_H +#define SORTLIST_MAIN_H + +// Cтруктура для узла списка +struct Node { + int data; // данные узла + struct Node* next; // указатель на следующий узел +}; + + +// Структура для самого списка +struct List { + struct Node* head; // указатель на начало списка + int size; // размер списка +}; + +// Создание пустого списка +struct List* createEmptyList(); + +// Получение элемента по индексу +int getElement(struct List* list, int i); + +// Реализация команды 0 - выйти: +// Удаление всего списка для освобождения и выхода +void deleteList(struct List* list); + +// Реализация команды 1 - добавить значениe в сортированный список: +// Поиск позиции для вставки элемента в список +int findPosition(struct List* list, int element); + +// Вставка элемента в список по заданному индексу +void insertElement(struct List* list, int i, int element); + +// Реализация команды 2 – удалить значение из списка: +// Проверка наличия значения в списке +int checkElement(struct List* list, int element); + +// Удаление элемента по индексу +void deleteElement(struct List* list, int i); + +// Реализация команды 3 – распечатать список: +// распечатывание содержимое списка +void printList(struct List* list); + +// Функция main для реализации выбора команд 0-3 пользователем +int main(); + + +#endif //SORTLIST_MAIN_H \ No newline at end of file From 75a623b5f1f666bdf89773a3c15cd30561986381 Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Wed, 19 Nov 2025 21:47:38 +0300 Subject: [PATCH 02/12] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BC=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B8=D0=BB=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= =?UTF-8?q?=20hw6=20=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=83=D1=8E=20=D0=BF=D0=B0=D0=BF=D0=BA=D1=83=20hw6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/{ => hw6_sortList}/sortList.c | 0 src/{ => hw6_sortList}/sortList.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/{ => hw6_sortList}/sortList.c (100%) rename src/{ => hw6_sortList}/sortList.h (100%) diff --git a/src/sortList.c b/src/hw6_sortList/sortList.c similarity index 100% rename from src/sortList.c rename to src/hw6_sortList/sortList.c diff --git a/src/sortList.h b/src/hw6_sortList/sortList.h similarity index 100% rename from src/sortList.h rename to src/hw6_sortList/sortList.h From 438cb635103a40e2d5209a2bcab828773a0d0218 Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Sat, 20 Dec 2025 22:33:37 +0300 Subject: [PATCH 03/12] Create CMakeLists.txt --- src/hw6_sortList/CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/hw6_sortList/CMakeLists.txt diff --git a/src/hw6_sortList/CMakeLists.txt b/src/hw6_sortList/CMakeLists.txt new file mode 100644 index 0000000..661d636 --- /dev/null +++ b/src/hw6_sortList/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 4.0) +project(hw6_sortList C) + +set(CMAKE_C_STANDARD 17) + +include_directories(src/hw6_sortList) +add_executable(sortList src/hw6_sortList/sortList.c) From 2c679ad7900a0218a2ad77687e7160f3d23789ba Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Sun, 21 Dec 2025 12:54:37 +0300 Subject: [PATCH 04/12] Fixed CMakeLists.txt --- src/hw6_sortList/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/hw6_sortList/CMakeLists.txt b/src/hw6_sortList/CMakeLists.txt index 661d636..e8f253d 100644 --- a/src/hw6_sortList/CMakeLists.txt +++ b/src/hw6_sortList/CMakeLists.txt @@ -3,5 +3,4 @@ project(hw6_sortList C) set(CMAKE_C_STANDARD 17) -include_directories(src/hw6_sortList) -add_executable(sortList src/hw6_sortList/sortList.c) +add_executable(sortList sortList.c) From 7079662bb2ea41f2f178d868b9220ff93cd17a2a Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Fri, 26 Dec 2025 04:35:56 +0300 Subject: [PATCH 05/12] Update CMakeLists.txt --- src/hw6_sortList/CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/hw6_sortList/CMakeLists.txt b/src/hw6_sortList/CMakeLists.txt index e8f253d..cd988be 100644 --- a/src/hw6_sortList/CMakeLists.txt +++ b/src/hw6_sortList/CMakeLists.txt @@ -3,4 +3,7 @@ project(hw6_sortList C) set(CMAKE_C_STANDARD 17) -add_executable(sortList sortList.c) +add_executable(sortList + interactiveMain.c + sortList.c ) + From 4ffd9cf7d2912cb3e92ee0f6c18ed75bf97067aa Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Fri, 26 Dec 2025 04:36:38 +0300 Subject: [PATCH 06/12] Style fix in CMakeLists.txt --- src/hw6_sortList/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hw6_sortList/CMakeLists.txt b/src/hw6_sortList/CMakeLists.txt index cd988be..a0a4a4e 100644 --- a/src/hw6_sortList/CMakeLists.txt +++ b/src/hw6_sortList/CMakeLists.txt @@ -5,5 +5,6 @@ set(CMAKE_C_STANDARD 17) add_executable(sortList interactiveMain.c - sortList.c ) + sortList.c + ) From bfc54aea420ab006d534b9b168c3e0a3c255480e Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Fri, 26 Dec 2025 04:37:25 +0300 Subject: [PATCH 07/12] Update sortList.c without main --- src/hw6_sortList/sortList.c | 245 +++++++++--------------------------- 1 file changed, 56 insertions(+), 189 deletions(-) diff --git a/src/hw6_sortList/sortList.c b/src/hw6_sortList/sortList.c index e2dd3ce..f7bfb38 100644 --- a/src/hw6_sortList/sortList.c +++ b/src/hw6_sortList/sortList.c @@ -1,9 +1,6 @@ -// -// Created by sasha on 19.11.2025. -// #include -#include "sortList.h" #include +#include "sortList.h" // Создание пустого списка struct List* createEmptyList() @@ -18,230 +15,100 @@ struct List* createEmptyList() return list; } -// Получение элемента по индексу -int getElement(struct List* list, int i) -{ - // Проверка корректности индекса - if (i < 0 || i >= list->size) { - printf("Некорректный индекс: он не может быть < 0 или больше размера списка\n"); - return -1; - } - - struct Node* currNode = list->head; - while (i--) { - currNode = currNode->next; - } - return currNode->data; -} - -// Реализация команды 0 - выйти: -// Удаление всего списка для освобождения и выхода +// Удаление всего списка void deleteList(struct List* list) { - struct Node* currNode = list->head; - // Удаление узлов - while (currNode) { - struct Node* tempNode = currNode; - currNode = currNode->next; - free(tempNode); + struct Node* curr = list->head; + while (curr) { + struct Node* tmp = curr; + curr = curr->next; + free(tmp); } - // Освобождение памяти free(list); } -// Реализация команды 1 - добавить значение в сортированный список: -// Поиск позиции для вставки элемента в список -int findPosition(struct List* list, int element) -{ - if (list->size == 0) - return 0; - - struct Node* currNode = list->head; - int position = 0; - - // Ищем первую позицию, где след элемент больше нового значения - // Так как список отсортирован, то прошлый будет или больше или равен вставляемому - while (currNode && currNode->data < element) { - currNode = currNode->next; - position++; - } - return position; -} - -// Вставка элемента в список по заданному индексу -void insertElement(struct List* list, int i, int element) +// Вставка элемента +void insertElement(struct List* list, int element) { - // Проверка корректности индекса - if (i < 0 || i > list->size) { - printf("Некорректный индекс: он не может быть < 0 или больше размера списка\n"); - return; - } - - // Создание нового узла struct Node* newNode = malloc(sizeof(struct Node)); if (!newNode) { - printf("Ошибка выделения памяти для саздания нового узла\n"); + printf("Ошибка выделения памяти\n"); return; } newNode->data = element; - // Вставка - if (i == 0) { + newNode->next = NULL; + + if (!list->head || list->head->data >= element) { newNode->next = list->head; list->head = newNode; - } else { - // Поиск позиции для вставки - struct Node* currNode = list->head; - int pos = 1; - while (pos < i) { - currNode = currNode->next; - pos++; - } - newNode->next = currNode->next; - currNode->next = newNode; + list->size++; + return; } + + // Поиск места + struct Node* curr = list->head; + while (curr->next && curr->next->data < element) { + curr = curr->next; + } + + newNode->next = curr->next; + curr->next = newNode; list->size++; } -// Реализация команды 2 – удалить значение из списка: -// Проверка наличия значения в списке -int checkElement(struct List* list, int element) +// Удаление элемента +int deleteElement(struct List* list, int element) { - struct Node* currNode = list->head; - int position = 0; + if (!list->head) { + return 0; + } - while (currNode) { - if (currNode->data == element) { - return position; - } - currNode = currNode->next; - position++; + if (list->head->data == element) { + struct Node* tmp = list->head; + list->head = list->head->next; + free(tmp); + list->size--; + return 1; } - return -1; -} -// Удаление элемента по индексу -void deleteElement(struct List* list, int i) -{ - // проверка корректности индекса - if (i < 0 || i >= list->size) { - printf("Некорректный индекс: он не может быть < 0 или больше размера списка\n"); - return; + // Поиск элемента + struct Node* curr = list->head; + while (curr->next && curr->next->data != element) { + curr = curr->next; } - struct Node* nodeForDeletion; - // Удаление из начала списка - if (i == 0) { - nodeForDeletion = list->head; - list->head = list->head->next; - } else { - // Поиск узла перед удаляемым - struct Node* currNode = list->head; - int pos = 1; - while (pos < i) { - currNode = currNode->next; - pos++; - } - nodeForDeletion = currNode->next; - currNode->next = nodeForDeletion->next; + + if (!curr->next) { + return 0; // не найденн } - // Освобождение памяти - free(nodeForDeletion); + + struct Node* tmp = curr->next; + curr->next = tmp->next; + free(tmp); list->size--; + return 1; } -// Реализация команды 3 – распечатать список: -// Печать содержимого списка +// Печать списка void printList(struct List* list) { - if (list->size == 0) { - printf("Список пустой\n"); + if (!list->head) { + printf("Список пуст\n"); return; } - struct Node* currNode = list->head; - printf("Содержание списка:\n"); + struct Node* curr = list->head; printf("["); - while (currNode) { - printf("%d", currNode->data); - if (currNode->next) printf(", "); - currNode = currNode->next; + while (curr) { + printf("%d", curr->data); + if (curr->next) printf(", "); + curr = curr->next; } printf("]\n"); } -// Функция main для реализации выбора команд 0-3 пользователем -int main() -{ - struct List* list = createEmptyList(); - int command, inputElement; - - while (1) { - printf("Команды:\n"); - printf("0 - Выйти\n"); - printf("1 - Добавить значение в сортированный список\n"); - printf("2 - Удалить значение из списка\n"); - printf("3 - Распечатать список\n"); - printf("Выберите команду (0,1,2,3): "); - - - if (scanf("%d", &command) != 1) { - printf("Ошибка: вводите только цифры\n"); - int ch; - while ((ch = getchar()) != '\n'); - - continue; - } - - switch (command) { - case 0: - printf("Совершён выход\n"); - deleteList(list); - return 0; - - case 1: - printf("Введите значение для добавления: "); - - if (scanf("%d", &inputElement) != 1) { - printf("Ошибка: вводите только цифры\n"); - - int ch; - while ((ch = getchar()) != '\n'); - - continue; - } - - int position1 = findPosition(list, inputElement); - insertElement(list, position1, inputElement); - printf("Значение было добавлено на позицию %d\n", position1); - break; - - case 2: - printf("Введите значение для удаления:"); - - if (scanf("%d", &inputElement) != 1) { - printf("Ошибка: вводите только цифры\n"); - - int ch; - while ((ch = getchar()) != '\n'); - - continue; - } - - int position2 = checkElement(list, inputElement); - if (position2 != -1) { - deleteElement(list, position2); - printf("Значение было удалено с позиции %d\n", position2); - } else { - printf("Значение не найдено\n"); - } - break; - - case 3: - printList(list); - break; - default: printf("Введена неподдерживаемая команда: можно только 0,1,2,3\n"); } } -} \ No newline at end of file +} From b427b1d37e9d4de3d10ad7fc7f034ad27a9079a7 Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Fri, 26 Dec 2025 04:38:00 +0300 Subject: [PATCH 08/12] Fixed sortList.c --- src/hw6_sortList/sortList.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/hw6_sortList/sortList.c b/src/hw6_sortList/sortList.c index f7bfb38..f196c56 100644 --- a/src/hw6_sortList/sortList.c +++ b/src/hw6_sortList/sortList.c @@ -106,9 +106,3 @@ void printList(struct List* list) printf("]\n"); } - - default: - printf("Введена неподдерживаемая команда: можно только 0,1,2,3\n"); - } - } -} From 172329e5824d7b577c233477373fd1cba4e63bc5 Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Fri, 26 Dec 2025 04:41:54 +0300 Subject: [PATCH 09/12] Update sortList.c --- src/hw6_sortList/sortList.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/hw6_sortList/sortList.c b/src/hw6_sortList/sortList.c index f196c56..4d519b9 100644 --- a/src/hw6_sortList/sortList.c +++ b/src/hw6_sortList/sortList.c @@ -20,10 +20,11 @@ void deleteList(struct List* list) { struct Node* curr = list->head; while (curr) { - struct Node* tmp = curr; + struct Node* tempNode = curr; curr = curr->next; - free(tmp); + free(tempNode); } + free(list); } @@ -35,6 +36,7 @@ void insertElement(struct List* list, int element) printf("Ошибка выделения памяти\n"); return; } + newNode->data = element; newNode->next = NULL; @@ -64,9 +66,9 @@ int deleteElement(struct List* list, int element) } if (list->head->data == element) { - struct Node* tmp = list->head; + struct Node* tempNode = list->head; list->head = list->head->next; - free(tmp); + free(tempNode); list->size--; return 1; } @@ -81,9 +83,9 @@ int deleteElement(struct List* list, int element) return 0; // не найденн } - struct Node* tmp = curr->next; - curr->next = tmp->next; - free(tmp); + struct Node* tempNode = curr->next; + curr->next = tempNode->next; + free(tempNode); list->size--; return 1; } From 92037dc97103f7f2f86b6dda0f44b669a0eb18c4 Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Fri, 26 Dec 2025 04:42:26 +0300 Subject: [PATCH 10/12] Style fix of sortList.h --- src/hw6_sortList/sortList.h | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/src/hw6_sortList/sortList.h b/src/hw6_sortList/sortList.h index 2eb518d..133c03c 100644 --- a/src/hw6_sortList/sortList.h +++ b/src/hw6_sortList/sortList.h @@ -1,53 +1,34 @@ -// -// Created by sasha on 19.11.2025. -// +#pragma once -#ifndef SORTLIST_MAIN_H -#define SORTLIST_MAIN_H - -// Cтруктура для узла списка struct Node { - int data; // данные узла - struct Node* next; // указатель на следующий узел + int data; + struct Node* next; }; - -// Структура для самого списка struct List { - struct Node* head; // указатель на начало списка - int size; // размер списка + struct Node* head; + int size; }; // Создание пустого списка struct List* createEmptyList(); -// Получение элемента по индексу -int getElement(struct List* list, int i); - // Реализация команды 0 - выйти: // Удаление всего списка для освобождения и выхода void deleteList(struct List* list); // Реализация команды 1 - добавить значениe в сортированный список: -// Поиск позиции для вставки элемента в список -int findPosition(struct List* list, int element); - -// Вставка элемента в список по заданному индексу -void insertElement(struct List* list, int i, int element); +void insertElement(struct List* list, int element); // Реализация команды 2 – удалить значение из списка: -// Проверка наличия значения в списке -int checkElement(struct List* list, int element); - -// Удаление элемента по индексу -void deleteElement(struct List* list, int i); +int deleteElement(struct List* list, int element); // Реализация команды 3 – распечатать список: -// распечатывание содержимое списка void printList(struct List* list); + // Функция main для реализации выбора команд 0-3 пользователем int main(); -#endif //SORTLIST_MAIN_H \ No newline at end of file +#endif //SORTLIST_MAIN_H From a9aa9cf6f74d4f3ae81fe25a06bf26afff31b503 Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Fri, 26 Dec 2025 04:42:56 +0300 Subject: [PATCH 11/12] Style fix of sortList.h --- src/hw6_sortList/sortList.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/hw6_sortList/sortList.h b/src/hw6_sortList/sortList.h index 133c03c..8c018ae 100644 --- a/src/hw6_sortList/sortList.h +++ b/src/hw6_sortList/sortList.h @@ -26,9 +26,3 @@ int deleteElement(struct List* list, int element); // Реализация команды 3 – распечатать список: void printList(struct List* list); - -// Функция main для реализации выбора команд 0-3 пользователем -int main(); - - -#endif //SORTLIST_MAIN_H From 669d2332a4e0cc96674ffc07029eedd9cad0edd4 Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Fri, 26 Dec 2025 04:43:35 +0300 Subject: [PATCH 12/12] Add interactiveMain.c --- src/hw6_sortList/interactiveMain.c | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/hw6_sortList/interactiveMain.c diff --git a/src/hw6_sortList/interactiveMain.c b/src/hw6_sortList/interactiveMain.c new file mode 100644 index 0000000..9553a8f --- /dev/null +++ b/src/hw6_sortList/interactiveMain.c @@ -0,0 +1,62 @@ +#include +#include "sortList.h" + +int main(void) +{ + struct List* list = createEmptyList(); + int command, inputElement; + + while (1) { + printf("Команды:\n"); + printf("0 - Выйти\n"); + printf("1 - Добавить значение в сортированный список\n"); + printf("2 - Удалить значение из списка\n"); + printf("3 - Распечатать список\n"); + printf("Выберите команду (0,1,2,3): "); + + if (scanf("%d", &command) != 1) { + while (getchar() != '\n'); // очистка ввода + continue; + } + + switch (command) { + case 0: + printf("Совершён выход\n"); + deleteList(list); + return 0; + + case 1: + printf("Введите значение для добавления: "); + + if (scanf("%d", &inputElement) != 1) { + printf("Ошибка: вводите только цифры\n"); + while (getchar() != '\n'); + continue; + } + insertElement(list, inputElement); + break; + + case 2: + printf("Введите значение для удаления:"); + + if (scanf("%d", &inputElement) != 1) { + printf("Ошибка: вводите только цифры\n"); + while (getchar() != '\n'); + continue; + } + if (deleteElement(list, inputElement)) { + printf("Значение было удалено\n"); + } else { + printf("Элемент не найден\n"); + } + break; + + case 3: + printList(list); + break; + + default: + printf("Введена неподдерживаемая команда: можно только 0,1,2,3\n"); + } + } +}