stack is a simple library implementing a stack of pointers.
Just add stack.c to your project file / Makefile and include stack.h where needed.
Create a new stack.
- capacity - Initial stack capacity
Return: A new stack handle (to be freed with stackfree)
Set an optional destructor function for elements.
- stack - Stack handle
- dtor - Destructor function
Return: 0 => Ok, -1 => Error
Resize a stack.
- stack - Stack handle
- capacity - New size of stack
Return: The handle of the reallocated stack, or NULL for error
Release all allocations related to a stack.
- stack - Stack handle or NULL
Return: 0 => OK, -1 => Error
Run destructor function on all elements and release all allocations related to a stack
- stack - Stack handle
Return: 0 => OK, -1 => Error
Make sure the capacity of the stack is at least needed_capacity.
- stack - Stack handle
- needed_capacity - Minimum capacity
Return: The handle of the possibly reallocated stack
Add a new pointer to the stack.
- stack - Stack handle
- data - Pointer to be added
Return: 0 => OK, -1 => Error
Return the pointer on the top of the stack.
- stack - Stack handle
Return: Pointer on top of stack, or NULL if error
Return a pointer from the stack.
- stack - Stack handle
- back_index - Distance from top of stack
Return: Pointer back_index elements from top of stack, or NULL if error
Remove the pointer on the top of the stack.
- stack - Stack handle
Return: Pointer on top of stack, or NULL if error
Remove element on top of the stack and, if destruct function exists, run it on the element
- stack - Stack handle
Return: 0 => Ok, -1 => Error (Stack is already empty)
Return number of elements in stack
- stack - Stack handle
Return: Number of element in stack