feat(stack): introduce a stack ds, internally this uses a sll

This commit is contained in:
Squibid 2025-11-15 21:47:45 -05:00
parent 69dc1dcb45
commit 945343daa0
Signed by: squibid
GPG key ID: BECE5684D3C4005D
4 changed files with 127 additions and 0 deletions

38
ds.h
View file

@ -22,6 +22,11 @@ typedef struct {
void *val;
} _ds_hmp_kv_t;
typedef struct {
unsigned n;
ds_sll_t *items;
} ds_stack_t;
#define ds_ll_foreach(t, ll) for (t *cur = ll; cur; cur = cur->next)
/**
@ -168,4 +173,37 @@ void *ds_hmp_get(ds_hmp_t *hmp, char *key);
*/
void *ds_hmp_remove(ds_hmp_t *hmp, char *key);
/**
* @brief initialize a new stack
*
* @return the stack
*/
ds_stack_t *ds_stack_init(void);
/**
* @brief push new data onto the stack
*
* @param stack the stack
* @param data the data
* @return 0 on success
*/
int ds_stack_push(ds_stack_t *stack, void *data);
/**
* @brief pop the data off the top of the stack
*
* @param stack the stack
* @return the data
*/
void *ds_stack_pop(ds_stack_t *stack);
/**
* @brief peek forward x items
*
* @param stack the stack
* @param x how many items to look ahead
* @return the data or NULL if out of range
*/
void *ds_stack_peek(ds_stack_t *stack, unsigned x);
#endif