diff options
author | Rob Landley <rob@landley.net> | 2019-07-10 01:53:23 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-07-10 01:53:23 -0500 |
commit | c3ccbbc4ff3352877ed39b6d4fa430affea0e9a4 (patch) | |
tree | 13db6e3a501a83f151b60102ef51ab02242e4666 | |
parent | 4c6f35ea529ebca9b2dfb6d7c79cade30118fd75 (diff) | |
download | toybox-c3ccbbc4ff3352877ed39b6d4fa430affea0e9a4.tar.gz |
Add dlist_lpop() to remove last entry (use dlist as stack).
-rw-r--r-- | lib/lib.h | 1 | ||||
-rw-r--r-- | lib/llist.c | 16 |
2 files changed, 17 insertions, 0 deletions
@@ -45,6 +45,7 @@ void llist_free_double(void *node); void llist_traverse(void *list, void (*using)(void *node)); void *llist_pop(void *list); // actually void **list void *dlist_pop(void *list); // actually struct double_list **list +void *dlist_lpop(void *list); // also struct double_list **list void dlist_add_nomalloc(struct double_list **list, struct double_list *new); struct double_list *dlist_add(struct double_list **list, char *data); void *dlist_terminate(void *list); diff --git a/lib/llist.c b/lib/llist.c index 2969102d..e1e6a564 100644 --- a/lib/llist.c +++ b/lib/llist.c @@ -51,6 +51,7 @@ void *llist_pop(void *list) return (void *)next; } +// Remove first item from &list and return it void *dlist_pop(void *list) { struct double_list **pdlist = (struct double_list **)list, *dlist = *pdlist; @@ -66,6 +67,21 @@ void *dlist_pop(void *list) return dlist; } +// remove last item from &list and return it (stack pop) +void *dlist_lpop(void *list) +{ + struct double_list *dl = *(struct double_list **)list; + void *v = 0; + + if (dl) { + dl = dl->prev; + v = dlist_pop(&dl); + if (!dl) *(void **)list = 0; + } + + return v; +} + void dlist_add_nomalloc(struct double_list **list, struct double_list *new) { if (*list) { |