From fed1c1a091df4501663c8624638730a4a3f9c792 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Wed, 24 Jul 2019 22:00:17 -0400 Subject: [PATCH] Added functionality to get the size of the list --- include/klist.h | 1 + src/klist.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/klist.h b/include/klist.h index 125935d..67228cb 100644 --- a/include/klist.h +++ b/include/klist.h @@ -3,6 +3,7 @@ struct node* init_list(void*, size_t); int is_list_init(struct node**); +int list_size(struct node**); void insert_at_begin(struct node**, void*, size_t); void insert_at_end(struct node**, void*, size_t); diff --git a/src/klist.c b/src/klist.c index 59fbb68..c377f8e 100644 --- a/src/klist.c +++ b/src/klist.c @@ -22,6 +22,25 @@ int is_list_init(struct node **list) else return 0; } +int list_size(struct node **list) +{ + int count = 0; + struct node *tmp = *list; + + struct node *in_tmp = NULL; + + while (tmp != NULL || tmp->next != NULL) { + in_tmp = tmp; + tmp = tmp->next; + + ++count; + + if (tmp == NULL) + break; + } + + return count; +} void insert_at_begin(struct node **list, void *val, size_t val_size) {