summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAriadna Vigo <arivigodr@gmail.com>2020-09-29 18:13:42 +0200
committerAriadna Vigo <arivigodr@gmail.com>2020-09-29 18:13:42 +0200
commit1330bcf4023df3b3c814a1d868456ddbb211a378 (patch)
tree29f6dff232d885bd2a2188b09465470c81286c4f
parentbc909e76a6d360fea029a218e9eda5f0e591f043 (diff)
Implemented deletion of entries (and dbl link lists)
-rw-r--r--tasklst.c33
-rw-r--r--tasklst.h2
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);