libstd v0.1.0
Loading...
Searching...
No Matches
stack.h
Go to the documentation of this file.
1#ifndef STACK_H_
2#define STACK_H_
3
4#include <stdbool.h>
5#include <stddef.h>
6
10typedef struct StackNode {
11 void* data;
12 struct StackNode* next;
14
18typedef struct {
20 size_t element_size;
21 size_t size;
22} Stack;
23
30void stack_init(Stack* stack, size_t element_size);
31
38void stack_destroy(Stack* stack);
39
48bool stack_push(Stack* stack, const void* data);
49
57bool stack_pop(Stack* stack);
58
66void* stack_top(Stack* stack);
67
78int stack_search(Stack* stack, int (*cmp_fn)(void*, void*), void* key);
79
86int stack_size(const Stack* stack);
87
88#endif // STACK_H_
void stack_destroy(Stack *stack)
Destroys the Stack and frees all allocated nodes and their associated data.
Definition stack.c:39
int stack_size(const Stack *stack)
Retrieves the total number of elements currently stored on the stack.
Definition stack.c:119
void stack_init(Stack *stack, size_t element_size)
Initializes an empty Stack.
Definition stack.c:29
int stack_search(Stack *stack, int(*cmp_fn)(void *, void *), void *key)
Searches for an element in the stack using a comparison function.
Definition stack.c:99
bool stack_pop(Stack *stack)
Pops (removes) the top element from the stack.
Definition stack.c:77
void * stack_top(Stack *stack)
Accesses the top element of the stack without removing it.
Definition stack.c:91
bool stack_push(Stack *stack, const void *data)
Pushes a new element onto the top of the stack.
Definition stack.c:61
Node structure representing an element in the stack.
Definition stack.h:10
void * data
Definition stack.h:11
struct StackNode * next
Definition stack.h:12
Structure representing a generic linked-list-based Stack.
Definition stack.h:18
size_t size
Definition stack.h:21
StackNode * top
Definition stack.h:19
size_t element_size
Definition stack.h:20