diff --git a/src/hw6_sortList/CMakeLists.txt b/src/hw6_sortList/CMakeLists.txt new file mode 100644 index 0000000..a0a4a4e --- /dev/null +++ b/src/hw6_sortList/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 4.0) +project(hw6_sortList C) + +set(CMAKE_C_STANDARD 17) + +add_executable(sortList + interactiveMain.c + sortList.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"); + } + } +} diff --git a/src/hw6_sortList/sortList.c b/src/hw6_sortList/sortList.c new file mode 100644 index 0000000..4d519b9 --- /dev/null +++ b/src/hw6_sortList/sortList.c @@ -0,0 +1,110 @@ +#include +#include +#include "sortList.h" + +// Создание пустого списка +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; +} + +// Удаление всего списка +void deleteList(struct List* list) +{ + struct Node* curr = list->head; + while (curr) { + struct Node* tempNode = curr; + curr = curr->next; + free(tempNode); + } + + free(list); +} + +// Вставка элемента +void insertElement(struct List* list, int element) +{ + struct Node* newNode = malloc(sizeof(struct Node)); + if (!newNode) { + printf("Ошибка выделения памяти\n"); + return; + } + + newNode->data = element; + newNode->next = NULL; + + if (!list->head || list->head->data >= element) { + newNode->next = list->head; + list->head = 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++; +} + +// Удаление элемента +int deleteElement(struct List* list, int element) +{ + if (!list->head) { + return 0; + } + + if (list->head->data == element) { + struct Node* tempNode = list->head; + list->head = list->head->next; + free(tempNode); + list->size--; + return 1; + } + + // Поиск элемента + struct Node* curr = list->head; + while (curr->next && curr->next->data != element) { + curr = curr->next; + } + + if (!curr->next) { + return 0; // не найденн + } + + struct Node* tempNode = curr->next; + curr->next = tempNode->next; + free(tempNode); + list->size--; + return 1; +} + +// Печать списка +void printList(struct List* list) +{ + if (!list->head) { + printf("Список пуст\n"); + return; + } + + struct Node* curr = list->head; + printf("["); + while (curr) { + printf("%d", curr->data); + if (curr->next) printf(", "); + curr = curr->next; + } + printf("]\n"); +} + diff --git a/src/hw6_sortList/sortList.h b/src/hw6_sortList/sortList.h new file mode 100644 index 0000000..8c018ae --- /dev/null +++ b/src/hw6_sortList/sortList.h @@ -0,0 +1,28 @@ +#pragma once + +struct Node { + int data; + struct Node* next; +}; + +struct List { + struct Node* head; + int size; +}; + +// Создание пустого списка +struct List* createEmptyList(); + +// Реализация команды 0 - выйти: +// Удаление всего списка для освобождения и выхода +void deleteList(struct List* list); + +// Реализация команды 1 - добавить значениe в сортированный список: +void insertElement(struct List* list, int element); + +// Реализация команды 2 – удалить значение из списка: +int deleteElement(struct List* list, int element); + +// Реализация команды 3 – распечатать список: +void printList(struct List* list); +