aboutsummaryrefslogtreecommitdiff
path: root/src/common/stack_core.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/stack_core.c')
-rw-r--r--src/common/stack_core.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/common/stack_core.c b/src/common/stack_core.c
new file mode 100644
index 0000000..6875862
--- /dev/null
+++ b/src/common/stack_core.c
@@ -0,0 +1,56 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* stack_core.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/19 06:37:45 by cacharle #+# #+# */
+/* Updated: 2020/01/19 06:50:50 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+t_stack *stack_new(int size)
+{
+ t_stack *stack;
+
+ if ((stack = (t_stack*)malloc(sizeof(t_stack))) == NULL)
+ return (NULL);
+ if ((stack->elements = (int*)malloc(sizeof(int) * size)) == NULL)
+ {
+ free(stack);
+ return (NULL);
+ }
+ stack->top = -1;
+ return (stack);
+}
+
+void stack_destroy(t_stack *stack)
+{
+ if (stack == NULL)
+ return ;
+ free(stack->elements);
+ free(stack);
+}
+
+void stack_push(t_stack *stack, int n)
+{
+ stack->top++;
+ stack->elements[stack->top] = n;
+}
+
+void stack_pop(t_stack *stack)
+{
+ if (stack_empty(stack))
+ return ;
+ stack->top--;
+}
+
+int stack_peek(t_stack *stack)
+{
+ if (stack_empty(stack))
+ return (0);
+ return (stack->elements[stack->top]);
+}