From 729f87fee04e1e03c5546a2bf19191f591afd634 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Mon, 22 Jul 2019 21:58:03 -0400 Subject: [PATCH] added function to free list, added function pointer for the use of traversing the list data, and code cleanup --- include/klist.h | 7 ++++++- src/klist.c | 46 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/include/klist.h b/include/klist.h index 3ca873c..00758e6 100644 --- a/include/klist.h +++ b/include/klist.h @@ -1,5 +1,10 @@ #include "types.h" +struct node* init_list(void*); + +int is_list_init(struct node**); + void insert_at_begin(struct node**, void*); void insert_at_end(struct node**, void*); -void traverse(struct node**); +void free_list(struct node**); +void traverse(struct node*, void (*)(struct node*)); diff --git a/src/klist.c b/src/klist.c index 3a1ad6f..d53b4c8 100644 --- a/src/klist.c +++ b/src/klist.c @@ -4,11 +4,29 @@ #include "klist.h" +struct node* init_list(void *val) +{ + struct node *list = (struct node*)malloc(sizeof(struct node)); + void *tmp_val = (void*)malloc(sizeof(val)); + memcpy(tmp_val, val, sizeof(val)); + list->data = tmp_val; + list->next = NULL; + + return list; +} + +int is_list_init(struct node **list) +{ + if (*list == NULL) + return 1; + else + return 0; +} void insert_at_begin(struct node **list, void *val) { struct node *tmp = (struct node*)malloc(sizeof(struct node)); - void *tmp_val = malloc(sizeof(val)); + void *tmp_val = (void*)malloc(sizeof(val)); memcpy(tmp_val, val, sizeof(val)); if ((*list) == NULL) { @@ -25,7 +43,7 @@ void insert_at_begin(struct node **list, void *val) void insert_at_end(struct node **list, void *val) { struct node *tmp = (struct node*)malloc(sizeof(struct node)); - void *tmp_val = malloc(sizeof(val)); + void *tmp_val = (void*)malloc(sizeof(val)); memcpy(tmp_val, val, sizeof(val)); if ((*list) == NULL) { @@ -43,18 +61,32 @@ void insert_at_end(struct node **list, void *val) tmp->next->data = tmp_val; tmp->next->next = NULL; } -void traverse(struct node **list) +void free_list(struct node **list) { - if (*list == NULL) + struct node *t = NULL; + struct node *l = *list; + + do { + t = l; + l = l->next; + free(t); + t = NULL; + } + while (l->next != NULL); + +} +void traverse(struct node *list, void (*print_val)(struct node*)) +{ + if (list == NULL) return; struct node *tmp; do { - printf("node value %d\n", *(int*)(*list)->data); - tmp = *list; - *list = (*list)->next; + (*print_val)(list); + tmp = list; + list = list->next; } while(tmp->next != NULL); }