list: add LIST_POP() helper that pops the first item off a linked list
This commit is contained in:
@@ -181,3 +181,12 @@
|
||||
} \
|
||||
(b) = NULL; \
|
||||
} while (false)
|
||||
|
||||
#define LIST_POP(name, a) \
|
||||
({ \
|
||||
typeof(a)* _a = &(a); \
|
||||
typeof(a) _p = *_a; \
|
||||
if (_p) \
|
||||
LIST_REMOVE(name, *_a, _p); \
|
||||
_p; \
|
||||
})
|
||||
|
||||
@@ -247,5 +247,14 @@ int main(int argc, const char *argv[]) {
|
||||
|
||||
assert_se(head == NULL);
|
||||
|
||||
LIST_PREPEND(item, head, items + 0);
|
||||
LIST_PREPEND(item, head, items + 1);
|
||||
LIST_PREPEND(item, head, items + 2);
|
||||
|
||||
assert_se(LIST_POP(item, head) == items + 2);
|
||||
assert_se(LIST_POP(item, head) == items + 1);
|
||||
assert_se(LIST_POP(item, head) == items + 0);
|
||||
assert_se(LIST_POP(item, head) == NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user