commit 1867267b0619c2b3de9e71a71eaa6553a740d2dd Author: amazing-username Date: Fri Jul 19 00:33:27 2019 -0400 Initial commit. Able to add int to the beginning and end of the list, as well as traverse the list. diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c5f6903 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.10) + +project(klist) + +set (INCLUDE + include/klist.h + include/types.h +) + +set (SOURCES + src/klist.c +) + +include_directories(${CMAKE_CURRENT_LIST_DIR}/include/) + +add_library(klist STATIC ${SOURCES} ${INCLUDE}) diff --git a/include/klist.h b/include/klist.h new file mode 100644 index 0000000..3ca873c --- /dev/null +++ b/include/klist.h @@ -0,0 +1,5 @@ +#include "types.h" + +void insert_at_begin(struct node**, void*); +void insert_at_end(struct node**, void*); +void traverse(struct node**); diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..e41aeec --- /dev/null +++ b/include/types.h @@ -0,0 +1,6 @@ + +struct node +{ + void *data; + struct node *next; +}; diff --git a/src/klist.c b/src/klist.c new file mode 100644 index 0000000..3a1ad6f --- /dev/null +++ b/src/klist.c @@ -0,0 +1,60 @@ +#include +#include +#include + +#include "klist.h" + + +void insert_at_begin(struct node **list, void *val) +{ + struct node *tmp = (struct node*)malloc(sizeof(struct node)); + void *tmp_val = malloc(sizeof(val)); + memcpy(tmp_val, val, sizeof(val)); + + if ((*list) == NULL) { + (*list) = tmp; + (*list)->data = tmp_val; + (*list)->next = NULL; + return; + } + + tmp->data = tmp_val; + tmp->next = *list; + *list = tmp; +} +void insert_at_end(struct node **list, void *val) +{ + struct node *tmp = (struct node*)malloc(sizeof(struct node)); + void *tmp_val = malloc(sizeof(val)); + memcpy(tmp_val, val, sizeof(val)); + + if ((*list) == NULL) { + (*list) = tmp; + (*list)->data = tmp_val; + (*list)->next = NULL; + return; + } + + tmp = *list; + while (tmp->next != NULL) + tmp = tmp->next; + + tmp->next = (struct node*)malloc(sizeof(struct node)); + tmp->next->data = tmp_val; + tmp->next->next = NULL; +} +void traverse(struct node **list) +{ + if (*list == NULL) + return; + + struct node *tmp; + + do + { + printf("node value %d\n", *(int*)(*list)->data); + tmp = *list; + *list = (*list)->next; + } + while(tmp->next != NULL); +}