This is a generic stack implementation. A stack is a LIFO (last in, first out) data structure that supports two main operations: adding data on top of the stack (push
) and retrieving the topmost item on the stack (pop
). Additionally, it should be possible to know a size
of a stack and whether it is_empty
.
The Stack
API is detailed below.
Fields:
Item*
top
-- the first (top) item in the stackunsigned int size
-- number of items in the stack
Methods (assume stack
is a Stack*
):
Stack* stack_create()
-- Creates a newStack
and returns a pointer to it, orNULL
in case of failure.stack->free(Stack* stack)
-- Frees the givenstack
.stack->is_empty(Stack* stack)
-- Returnstrue
if thestack
is empty,false
otherwise.stack->push(Stack* stack, void* data)
-- Adds thedata
on top of thestack
. Returns 0 on success and -1 on failure.stack->pop(Stack* stack)
-- Removes and returns the item on top of thestack
. ReturnsNULL
if the stack is null or empty.stack->contains(Stack* stack, void* item)
-- Returnstrue
if the stack contains the item, andfalse
otherwise.
For more details: