Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/SortList/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
61 changes: 61 additions & 0 deletions src/SortList/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#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");
}
12 changes: 12 additions & 0 deletions src/SortList/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdbool.h>

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);
119 changes: 119 additions & 0 deletions src/SortList/sort_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#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;
}