Skip to content

ahy1/stack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stack

Description

stack is a simple library implementing a stack of pointers.

Usage

Just add stack.c to your project file / Makefile and include stack.h where needed.

Functions

STACK *stackalloc(size_t capacity)

Create a new stack.

  • capacity - Initial stack capacity

Return: A new stack handle (to be freed with stackfree)

int stacksetdtor(STACK *stack, int (*dtor)(void *))

Set an optional destructor function for elements.

  • stack - Stack handle
  • dtor - Destructor function

Return: 0 => Ok, -1 => Error

STACK *stackrealloc(STACK *stack, size_t capacity)

Resize a stack.

  • stack - Stack handle
  • capacity - New size of stack

Return: The handle of the reallocated stack, or NULL for error

int stackfree(STACK *stack)

Release all allocations related to a stack.

  • stack - Stack handle or NULL

Return: 0 => OK, -1 => Error

int stackdestruct(STACK *stack)

Run destructor function on all elements and release all allocations related to a stack

  • stack - Stack handle

Return: 0 => OK, -1 => Error

STACK *stackexpand(STACK *stack, size_t needed_capacity)

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

int stackpush(STACK *stack, void *data)

Add a new pointer to the stack.

  • stack - Stack handle
  • data - Pointer to be added

Return: 0 => OK, -1 => Error

void *stacktop(STACK *stack)

Return the pointer on the top of the stack.

  • stack - Stack handle

Return: Pointer on top of stack, or NULL if error

void *stackback(STACK *stack, size_t back_index)

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

void *stackpop(STACK *stack)

Remove the pointer on the top of the stack.

  • stack - Stack handle

Return: Pointer on top of stack, or NULL if error

int stackpopdestruct(STACK *stack)

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)

size_t stacksize(STACK *size)

Return number of elements in stack

  • stack - Stack handle

Return: Number of element in stack

About

Stack library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published