diff --git a/src/SortList/CMakeLists.txt b/src/SortList/CMakeLists.txt new file mode 100644 index 0000000..490359d --- /dev/null +++ b/src/SortList/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.10) +project(SortList) + +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD_REQUIRED ON) + +add_executable(Sort_list sort_list.c list.c) + +target_compile_options(Sort_list PRIVATE -Wall -Wextra) + +enable_testing() +add_test(NAME SortListTest COMMAND Sort_list --test) diff --git a/src/SortList/list.c b/src/SortList/list.c new file mode 100644 index 0000000..ff8fef4 --- /dev/null +++ b/src/SortList/list.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include "list.h" + +void append(SortList *begin, int value){ + SortList *NewElem = malloc(sizeof(SortList)); + SortList *p = begin; + bool flag = 0; + NewElem->value = value; + while (p->next != NULL){ + if (p->next->value > value){ + NewElem->next = p->next; + p->next = NewElem; + flag = 1; + break; + } + p = p->next; + } + if (!flag){ + NewElem->next = p->next; + p->next = NewElem; + } +} +int get(SortList *begin){ + if (begin->next != NULL) + return begin->next->value; + return -1; +} +void DelElem(SortList *begin, int value){ + SortList *p = begin; + bool flag = 0; + while (p->next != NULL){ + if (p->next->value == value){ + SortList *elem = p->next; + p->next = elem->next; + flag = 1; + free(elem); + break; + } + p = p->next; + } + if (!flag){ + printf("\n %s", "the item is not in the list"); + } +} +void DelList(SortList *begin){ + while (begin->next != NULL){ + SortList *p = begin->next; + begin->next = p->next; + free(p); + } +} +void PrintfList(SortList *begin){ + SortList *p = begin->next; + while (p != NULL){ + printf("%d %c", p->value, ' '); + p = p->next; + } + printf("\n"); +} diff --git a/src/SortList/list.h b/src/SortList/list.h new file mode 100644 index 0000000..51ec797 --- /dev/null +++ b/src/SortList/list.h @@ -0,0 +1,12 @@ +#include + +typedef struct SortList{ + int value; + struct SortList *next; +} SortList; + +void append(SortList *begin, int value); +int get(SortList *begin); +void DelElem(SortList *begin, int value); +void DelList(SortList *begin); +void PrintfList(SortList *begin); diff --git a/src/SortList/sort_list.c b/src/SortList/sort_list.c new file mode 100644 index 0000000..ffa7e0d --- /dev/null +++ b/src/SortList/sort_list.c @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include "list.h" + +void Test1(){ + SortList List; + List.next = NULL; + + append(&List, 5); + append(&List, 8); + append(&List, -4); + append(&List, 0); + append(&List, 11); + append(&List, 9); + append(&List, 5); + + assert(get(&List) == -4); + DelElem(&List, -4); + assert(get(&List) == 0); + DelElem(&List, 0); + assert(get(&List) == 5); + DelElem(&List, 5); + assert(get(&List) == 5); + DelElem(&List, 5); + assert(get(&List) == 8); + DelElem(&List, 8); + assert(get(&List) == 9); + DelElem(&List, 9); + assert(get(&List) == 11); + DelElem(&List, 11); +} + +void Test2(){ + SortList List; + List.next = NULL; + + append(&List, 1); + append(&List, 2); + append(&List, 3); + + assert(get(&List) == 1); + DelElem(&List, 1); + assert(get(&List) == 2); + DelElem(&List, 2); + assert(get(&List) == 3); + DelElem(&List, 3); +} + +void Test3(){ + SortList List; + List.next = NULL; + + append(&List, 3); + append(&List, 2); + append(&List, 1); + + assert(get(&List) == 1); + DelElem(&List, 1); + assert(get(&List) == 2); + DelElem(&List, 2); + assert(get(&List) == 3); + DelElem(&List, 3); +} +void Test4(){ + SortList List; + List.next = NULL; + + append(&List, 3); + append(&List, 2); + append(&List, 1); + + DelList(&List); + assert(List.next == NULL); +} +void run(){ + Test1(); + Test2(); + Test3(); + Test4(); + printf("%s \n", "all tests have been completed successfully"); +} +int main(int argc, char *argv[]){ + if (argc == 2 && strcmp(argv[1], "--test") == 0){ + run(); + return 0; + } + SortList begin; + begin.next = NULL; + bool flag = 1; + while (flag){ + int c, value; + printf("%s \n", "0: close the program"); + printf("%s \n", "1: add a value to the list"); + printf("%s \n", "2: delete a value"); + printf("%s \n", "3: output a list"); + scanf ("%d", &c); + switch(c){ + case 0: + flag = 0; + break; + case 1: + scanf("%d", &value); + append(&begin, value); + break; + case 2: + scanf("%d", &value); + DelElem(&begin, value); + break; + case 3: + PrintfList(&begin); + break; + } + } + DelList(&begin); + return 0; +}