Does GLib have a data type that can be used as a LIFO (stack) collection? It does have lists, queues, hash tables and such but I can't seem to find a stack data type.
There is a trash stack type but it was designed with a specific purpose in mind and it has also been deprecated since version 2.48.
What can be used as a stack in GLib?
A bit late to the party, but a more light-weight approach to a stack would be using the singly-linked list type GSList, which doesn't require an explicit container object.
GSList *stack = NULL;
// push:
stack = g_slist_prepend(stack, element);
// stack non-empty?
if (stack) { ... }
// peek head without popping:
element = stack->data;
// pop:
stack = g_slist_delete_link(stack, stack);
A wrapper function for a proper "pop" that returns the element could then look something like this:
void *stack_pop(GSList **stackp) {
if (!*stackp)
return;
void *ret = (*stackp)->data;
*stackp = g_slist_delete_link(*stackp, *stackp);
return ret;
}
// ...
element = stack_pop(&stack); // returns NULL if stack is empty