diff options
author | Ariadna Vigo <arivigodr@gmail.com> | 2020-09-29 18:13:42 +0200 |
---|---|---|
committer | Ariadna Vigo <arivigodr@gmail.com> | 2020-09-29 18:13:42 +0200 |
commit | 1330bcf4023df3b3c814a1d868456ddbb211a378 (patch) | |
tree | 29f6dff232d885bd2a2188b09465470c81286c4f | |
parent | bc909e76a6d360fea029a218e9eda5f0e591f043 (diff) |
Implemented deletion of entries (and dbl link lists)
-rw-r--r-- | tasklst.c | 33 | ||||
-rw-r--r-- | tasklst.h | 2 |
2 files changed, 33 insertions, 2 deletions
@@ -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) { @@ -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); |