diff --git a/hw.sortList/CMakeLists.txt b/hw.sortList/CMakeLists.txt new file mode 100644 index 0000000..a7bc2df --- /dev/null +++ b/hw.sortList/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.25) +project(rewrite C) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +# Создаём библиотеку из файла в поддиректории +add_library(sortList sortList.c) + +# Создаём исполняемый файл из файла в другой поддиректории +add_executable(main main.c) + +# Линкуем библиотеку +target_link_libraries(main PRIVATE sortList) diff --git a/hw.sortList/main.c b/hw.sortList/main.c new file mode 100644 index 0000000..eba9b1f --- /dev/null +++ b/hw.sortList/main.c @@ -0,0 +1,86 @@ +#include "sortList.h" +#include +#include +#include +#include + +bool testDeleteEmptyNode() +{ + List* list = newList(); + return !deleteNode(list, 123313); +} + +bool testDeleteNode() +{ + List* list = newList(); + insert(list, 5); + return deleteNode(list, 5); +} + +bool testSorted1(){ + List* list = newList(); + insert(list, 5); + insert(list, 6); + insert(list, -1111); + int array[] = {-1111, 5, 6}; + return equalToArray(list, array, 3); +} + +bool testSorted2(){ + List* list = newList(); + insert(list, 5); + insert(list, 6); + insert(list, -1111); + int array[] = {-1111, 5}; + return !equalToArray(list, array, 2); +} + +bool test() +{ + return (testSorted1() && testDeleteEmptyNode() && testSorted2() && testDeleteNode()); +} + +int main(int argc, char** argv) +{ + + if (argc == 2 && strcmp(argv[1], "--test") == 0) { + if (!test()) { + printf("Test failed\n"); + return 1; + } else { + printf("Test passed\n"); + return 0; + } + } + + List* list = newList(); + int value = 0; + + const char* helpMassage = "Ведите номер операции: 0 - выйти, 1 - добавить значение, 2 - удалить значение, 3 - распечатать список: "; + int operation = 0; + do { + printf("%s", helpMassage); + int operation = 0; + scanf("%d", &operation); + 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"); + } + } while (operation != 0); + printf("Вы вышли\n"); + deleteList(list); + return 0; +} \ No newline at end of file diff --git a/hw.sortList/sortList.c b/hw.sortList/sortList.c new file mode 100644 index 0000000..1c8f785 --- /dev/null +++ b/hw.sortList/sortList.c @@ -0,0 +1,131 @@ +#include "sortList.h" +#include +#include + +// элемент списка +typedef struct ListNode { + // значение + int value; + // ссылка на следующую "ячейку" + struct ListNode* next; +} ListNode; + +typedef struct List { + ListNode* head; + int size; +} List; + +List* newList() +{ + List* list = (List*)malloc(sizeof(List)); + list->head = NULL; + list->size = 0; + 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; + list->size++; + return; + } + + // поиск того, куда вставить новый элемент + ListNode* current = list->head; + while (current->next != NULL && current->next->value < value) { + current = current->next; + } + + // вставка + newNode->next = current->next; + current->next = newNode; + list->size++; +} + +// проверка на возможность удаления элемента +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); + list->size--; + 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); + list->size--; + return true; +} + +// вывод списка +void printList(List* list) +{ + ListNode* current = list->head; + while (current != NULL) { + printf("%d ", current->value); + current = current->next; + } + printf("\n"); +} + +bool isEmpty(List* list){ + return list->head == NULL; +} + +// удаление списка +void deleteList(List* list) +{ + ListNode* current = list->head; + while (current != NULL) { + ListNode* temprery = current; + current = current->next; + free(temprery); + } + free(list); +} + +bool equalToArray(List* list, int* array, int arrayLength){ + ListNode* current = list->head; + int i = 0; + if (list->size != arrayLength){ + return false; + } + + while (i < arrayLength) { + if (current-> value != array[i]){ + return false; + } + current = current->next; + i++; + } + return true; +} \ No newline at end of file diff --git a/hw.sortList/sortList.h b/hw.sortList/sortList.h new file mode 100644 index 0000000..8086f76 --- /dev/null +++ b/hw.sortList/sortList.h @@ -0,0 +1,12 @@ +#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); +bool isEmpty(List* list); +bool equalToArray(List* list, int* array, int arrayLength); \ No newline at end of file