Stack
A stack stores elements in last-in-first-out order. Each slot is exactly elesz bytes, the same as vec_init. Push adds on top, pop removes from top, peek reads the top without removing. Initialization, destructor behavior, and error conventions match the underlying vector.
Header
Section titled “Header”#include <stack.h>Struct
Section titled “Struct”struct stack { struct vector vec;};vec holds the stack state, including element size and the optional destructor given to stack_init.
Macros
Section titled “Macros”stack_empty
Section titled “stack_empty”stack_empty(stack)Returns non-zero if the stack contains no elements.
Parameters
stack— pointer to the stack
stack_size
Section titled “stack_size”stack_size(stack)Returns the current element count.
Parameters
stack— pointer to the stack
Functions
Section titled “Functions”stack_init
Section titled “stack_init”int stack_init(struct stack *stack, size_t elesz, void (*destroy)(void *));Initializes an empty stack with element size elesz and an optional destructor. Must be called before any other function. Returns 0 on success, -1 on error.
Parameters
stack— pointer to an uninitialized stack structelesz— byte size of each element, must be non-zerodestroy— called on each element when it is discarded, or NULL for no-op
stack_fini
Section titled “stack_fini”void stack_fini(struct stack *stack);Destroys all elements and frees the buffer. No-op if stack is NULL. Does not free the struct stack itself.
Parameters
stack— pointer to the stack
stack_push
Section titled “stack_push”int stack_push(struct stack *stack, void *ele);Copies elesz bytes from ele onto the top, growing the buffer if needed. Returns 0 on success, -1 on error.
Parameters
stack— pointer to the stackele— pointer to the value to copy, must not be NULL
stack_pop
Section titled “stack_pop”int stack_pop(struct stack *stack, void *dest);Removes the top element. If dest is non-NULL, copies the element there and skips destroy. If dest is NULL and destroy is set, calls destroy on the element. Returns 0 on success, -1 if the stack is empty or stack is NULL.
Parameters
stack— pointer to the stackdest— destination buffer of at leasteleszbytes to receive the element, or NULL to invokedestroy
stack_peek
Section titled “stack_peek”void *stack_peek(struct stack *stack);Returns a pointer to the top element, or NULL if the stack is empty or stack is NULL.
Parameters
stack— pointer to the stack
stack_clear
Section titled “stack_clear”void stack_clear(struct stack *stack);Removes all elements and resets size to zero, calling destroy on each when set. Does not free the buffer or the struct stack itself. No-op if stack is NULL.
Parameters
stack— pointer to the stack
Example
Section titled “Example”#include <stack.h>
int main(void){ struct stack s; int x, y;
if (stack_init(&s, sizeof(int), NULL) != 0) return 1;
x = 10; stack_push(&s, &x); x = 20; stack_push(&s, &x);
if (!stack_empty(&s)) y = *(int *)stack_peek(&s); /* y == 20 */
stack_pop(&s, &y); /* y == 20 */ stack_pop(&s, &y); /* y == 10 */
stack_fini(&s); return 0;}