libstd v0.1.0
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
stack.h File Reference
#include <stdbool.h>
#include <stddef.h>
Include dependency graph for stack.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  StackNode
 Node structure representing an element in the stack. More...
 
struct  Stack
 Structure representing a generic linked-list-based Stack. More...
 

Typedefs

typedef struct StackNode StackNode
 Node structure representing an element in the stack.
 

Functions

void stack_init (Stack *stack, size_t element_size)
 Initializes an empty Stack.
 
void stack_destroy (Stack *stack)
 Destroys the Stack and frees all allocated nodes and their associated data.
 
bool stack_push (Stack *stack, const void *data)
 Pushes a new element onto the top of the stack.
 
bool stack_pop (Stack *stack)
 Pops (removes) the top element from the stack.
 
void * stack_top (Stack *stack)
 Accesses the top element of the stack without removing it.
 
int stack_search (Stack *stack, int(*cmp_fn)(void *, void *), void *key)
 Searches for an element in the stack using a comparison function.
 
int stack_size (const Stack *stack)
 Retrieves the total number of elements currently stored on the stack.
 

Typedef Documentation

◆ StackNode

typedef struct StackNode StackNode

Node structure representing an element in the stack.

Function Documentation

◆ stack_destroy()

void stack_destroy ( Stack stack)

Destroys the Stack and frees all allocated nodes and their associated data.

Parameters
stackPointer to the Stack to destroy.

◆ stack_init()

void stack_init ( Stack stack,
size_t  element_size 
)

Initializes an empty Stack.

Parameters
stackPointer to the Stack structure to initialize.
element_sizeSize of each element in bytes.

◆ stack_pop()

bool stack_pop ( Stack stack)

Pops (removes) the top element from the stack.

Parameters
stackPointer to the Stack.
Returns
true if an element was successfully removed, or false if the stack was empty.

◆ stack_push()

bool stack_push ( Stack stack,
const void *  data 
)

Pushes a new element onto the top of the stack.

Parameters
stackPointer to the Stack.
dataPointer to the element data to insert.
Returns
true if insertion was successful, or false on memory allocation failure.

◆ stack_search()

int stack_search ( Stack stack,
int(*)(void *, void *)  cmp_fn,
void *  key 
)

Searches for an element in the stack using a comparison function.

Parameters
stackPointer to the Stack.
cmp_fnPointer to the comparison function (should return 0 when elements match).
keyPointer to the value/key being searched for.
Returns
The 1-based distance from the top of the stack if found (where 1 is top), or -1 if not found.

◆ stack_size()

int stack_size ( const Stack stack)

Retrieves the total number of elements currently stored on the stack.

Parameters
stackPointer to the Stack.
Returns
Total number of elements.

◆ stack_top()

void * stack_top ( Stack stack)

Accesses the top element of the stack without removing it.

Parameters
stackPointer to the Stack.
Returns
Pointer to the data at the top of the stack, or NULL if the stack is empty.