From 1330bcf4023df3b3c814a1d868456ddbb211a378 Mon Sep 17 00:00:00 2001 From: Ariadna Vigo Date: Tue, 29 Sep 2020 18:13:42 +0200 Subject: Implemented deletion of entries (and dbl link lists) --- tasklst.c | 33 +++++++++++++++++++++++++++++++-- tasklst.h | 2 ++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/tasklst.c b/tasklst.c index ab30f31..b64fa2d 100644 --- a/tasklst.c +++ b/tasklst.c @@ -108,7 +108,6 @@ task_lst_add_task(TaskLst *list, int status, const char *str) return -1; newtask->status = status; - newtask->next = NULL; strncpy(newtask->tdesc, str, TASK_LST_DESC_MAX_SIZE); last = task_lst_get_last_task(*list); @@ -117,12 +116,42 @@ task_lst_add_task(TaskLst *list, int status, const char *str) else last->next = newtask; + newtask->prev = last; /* Also if last == NULL */ + newtask->next = NULL; + return 0; } +int +task_lst_del_task(TaskLst *list, int i) +{ + Task *del, *prev, *next; + + if ((i > task_lst_get_size(*list)) || (i < 0)) + return -1; + + if ((del = task_lst_get_task(*list, i)) == NULL) + return -1; + + prev = del->prev; + next = del->next; + + if (prev != NULL) + prev->next = next; + + if (next != NULL) + next->prev = prev; + + free(del); + + return 0; +} + Task * -task_lst_get_task(TaskLst list, int i) +task_lst_get_task(TaskLst list, int i) /* TODO: mv above task_lst_add_task() */ { + /* TODO: Maybe binary search? */ + Task *ptr; for (ptr = list.first; i > 0; ptr = ptr->next) { diff --git a/tasklst.h b/tasklst.h index ee3b56b..bb1d94d 100644 --- a/tasklst.h +++ b/tasklst.h @@ -10,6 +10,7 @@ enum { typedef struct TASK_ Task; struct TASK_ { int status; + Task *prev; Task *next; char tdesc[TASK_LST_DESC_MAX_SIZE]; }; @@ -28,6 +29,7 @@ int task_lst_count_todo(TaskLst list); int task_lst_count_done(TaskLst list); Task *task_lst_get_task(TaskLst list, int i); int task_lst_add_task(TaskLst *list, int status, const char *str); +int task_lst_del_task(TaskLst *list, int i); int task_lst_read_from_file(TaskLst *list, FILE *fp); void task_lst_write_to_file(FILE *fp, TaskLst list); -- cgit v1.2.3