diff --git a/hw.sortList/CMakeLists.txt b/hw.sortList/CMakeLists.txt new file mode 100644 index 0000000..267037d --- /dev/null +++ b/hw.sortList/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.25) +project(rewrite C) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +# Создаём библиотеку из файла в поддиректории +add_library(sortList sortList/sortList.c) + +# Создаём исполняемый файл из файла в другой поддиректории +add_executable(main hw.sortList/main.c) + +# Линкуем библиотеку +target_link_libraries(main PRIVATE sortList) + +# Добавляем путь к заголовочным файлам библиотеки +target_include_directories(main PRIVATE + ${CMAKE_SOURCE_DIR}/sortList +) \ No newline at end of file diff --git a/hw.sortList/main.c b/hw.sortList/main.c new file mode 100644 index 0000000..aa670ba --- /dev/null +++ b/hw.sortList/main.c @@ -0,0 +1,38 @@ +#include "../sortList/sortList.h" +#include +#include +#include + +int main(int argc, char** argv) +{ + List* list = newList(); + int value = 0; + + printf("Ведите номер операции: 0 - выйти, 1 - добавить значение, 2 - удалить значение, 3 - распечатать список: "); + int operation = 0; + scanf("%d", &operation); + while (operation != 0) { + if (operation == 1) { + printf("Ведите, какое значение вставить: "); + scanf("%d", &value); + insert(list, value); + } else if (operation == 2) { + printf("Ведите, какое значение удалить: "); + scanf("%d", &value); + if (deleteNode(list, value)) { + printf("Элемент удален\n"); + } else { + printf("Удалить элемент невозможно\n"); + } + } else if (operation == 3) { + printList(list); + } else { + printf("Такой операции нет\n"); + } + printf("Ведите номер операции: 0 - выйти, 1 - добавить значение, 2 - удалить значение, 3 - распечатать список: "); + scanf("%d", &operation); + } + printf("Вы вышли\n"); + deleteList(list); + return 0; +} \ No newline at end of file diff --git a/sortList/sortList.c b/sortList/sortList.c new file mode 100644 index 0000000..82f73bc --- /dev/null +++ b/sortList/sortList.c @@ -0,0 +1,104 @@ +#include "sortList.h" +#include +#include + +// элемент списка +typedef struct ListNode { + // значение + int value; + // ссылка на следующую "ячейку" + struct ListNode* next; +} ListNode; + +typedef struct List { + ListNode* head; +} List; + +List* newList() +{ + List* list = (List*)malloc(sizeof(List)); + list->head = NULL; + return list; +} + +// положить элемент в соответствии с сортировкой +void insert(List* list, int value) +{ + ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); + newNode->value = value; + newNode->next = NULL; + + // Если список пуст или новый элемент меньше головы + if (list->head == NULL || value < list->head->value) { + newNode->next = list->head; + list->head = newNode; + return; + } + + // поиск того, куда вставить новый элемент + ListNode* current = list->head; + while (current->next != NULL && current->next->value < value) { + current = current->next; + } + + // вставка + newNode->next = current->next; + current->next = newNode; +} + +// проверка на возможность удаления элемента +bool deleteNode(List* list, int value) +{ + // если список пустой + if (list->head == NULL) { + return false; + } + + // если элемент это голова + if (list->head->value == value) { + ListNode* temprery = list->head; + list->head = list->head->next; + free(temprery); + return true; + } + + // поиск элемента для удаления + ListNode* current = list->head; + while (current->next != NULL && current->next->value != value) { + current = current->next; + } + + // Если элемент не найден + if (current->next == NULL) { + return false; + } + + // удаление элемента + ListNode* temprery = current->next; + current->next = current->next->next; + free(temprery); + return true; +} + +// вывод списка +void printList(List* list) +{ + ListNode* current = list->head; + while (current != NULL) { + printf("%d ", current->value); + current = current->next; + } + printf("\n"); +} + +// удаление списка +void deleteList(List* list) +{ + ListNode* current = list->head; + while (current != NULL) { + ListNode* temprery = current; + current = current->next; + free(temprery); + } + list->head = NULL; +} \ No newline at end of file diff --git a/sortList/sortList.h b/sortList/sortList.h new file mode 100644 index 0000000..b94db67 --- /dev/null +++ b/sortList/sortList.h @@ -0,0 +1,10 @@ +#pragma once +#include + +typedef struct List List; + +List* newList(); +void insert(List* list, int value); +bool deleteNode(List* list, int value); +void printList(List* list); +void deleteList(List* list); \ No newline at end of file