#include <stdbool.h>
#include <stddef.h>
Go to the source code of this file.
|
| 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.
|
| |
◆ StackNode
Node structure representing an element in the stack.
◆ stack_destroy()
| void stack_destroy |
( |
Stack * |
stack | ) |
|
Destroys the Stack and frees all allocated nodes and their associated data.
- Parameters
-
| stack | Pointer to the Stack to destroy. |
◆ stack_init()
| void stack_init |
( |
Stack * |
stack, |
|
|
size_t |
element_size |
|
) |
| |
Initializes an empty Stack.
- Parameters
-
| stack | Pointer to the Stack structure to initialize. |
| element_size | Size of each element in bytes. |
◆ stack_pop()
| bool stack_pop |
( |
Stack * |
stack | ) |
|
Pops (removes) the top element from the stack.
- Parameters
-
| stack | Pointer 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
-
| stack | Pointer to the Stack. |
| data | Pointer 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
-
| stack | Pointer to the Stack. |
| cmp_fn | Pointer to the comparison function (should return 0 when elements match). |
| key | Pointer 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
-
| stack | Pointer 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
-
| stack | Pointer to the Stack. |
- Returns
- Pointer to the data at the top of the stack, or NULL if the stack is empty.