summaryrefslogtreecommitdiff
path: root/stack.h
diff options
context:
space:
mode:
authornoodle <shawtynoodle@gmail.com>2023-07-10 15:40:08 +0300
committernoodle <shawtynoodle@gmail.com>2023-07-10 15:40:08 +0300
commitb7ac144cd2d242791938b51569effb7a1378a332 (patch)
tree0db39dc6d72a96697707c662c32f4dcdb99372b7 /stack.h
parent35eacac40f265aad47bf25d10f3ecd3670b79b2f (diff)
Add files
Diffstat (limited to 'stack.h')
-rw-r--r--stack.h68
1 files changed, 68 insertions, 0 deletions
diff --git a/stack.h b/stack.h
new file mode 100644
index 0000000..611a54c
--- /dev/null
+++ b/stack.h
@@ -0,0 +1,68 @@
+#ifndef STACK_H
+#define STACK_H
+
+// stack
+typedef struct Stack Stack;
+
+/*
+ * stack_new: create a new stack
+ * returns:
+ * Stack * -> the created stack
+ * NULL -> failed to allocate memory for stack
+ */
+Stack *stack_new(void);
+
+/* stack_delete: free the stack, but don't free any buffer in any of it's */
+void stack_delete(Stack *stack);
+
+/*
+ * stack_free: free the stack, each element is also freed accordingly using
+ * 'free_data'
+ * parameters:
+ * stack (Stack *) -> stack to delete
+ * free_data -> function that would free resources of each element in stack
+ */
+void stack_free(Stack *stack, void (*free_data)(void *data));
+
+/*
+ * stack_push: push element to top of stack
+ * parameters:
+ * stack (Stack *) -> stack to push data into
+ * data (void *) -> data to push
+ * returns:
+ * void * -> the pushed data
+ * NULL -> failure of allocating memory for new stack element
+ */
+void *stack_push(Stack *stack, void *data);
+
+/* stack_pop: pop element from top of stack */
+void *stack_pop(Stack *stack);
+
+/* stack_get_size: get size of stack */
+size_t stack_size(Stack *stack);
+
+/*
+ * stack_get_size: iterate over a stack top-to-bottom, running 'apply' on each
+ * stack element
+ * parameters:
+ * stack (Stack *) -> stack to iterate over
+ * apply -> function that would be passed each element in stack
+ * returns:
+ * void * -> sucess (pointer to stack)
+ * NULL -> one of the executions of 'apply' returned NULL
+ */
+void *stack_iter(Stack *stack, void *(*apply)(void *data));
+
+/*
+ * stack_iter_reverse: iterate over a stack bottom-to-top, running 'apply' on
+ * each stack element
+ * parameters:
+ * stack (Stack *) -> stack to iterate over
+ * apply -> function that would be passed each element in stack
+ * returns:
+ * void * -> sucess (pointer to stack)
+ * NULL -> one of the executions of 'apply' returned NULL
+ */
+void *stack_iter_reverse(Stack *stack, void *(*apply)(void *data));
+
+#endif /* STACK_H */