From d2818fb779b4ccf55f16c55500a0083e619c9c3c Mon Sep 17 00:00:00 2001
From: "orestis.malaspin" <orestis.malaspinas@hesge.ch>
Date: Tue, 7 Dec 2021 08:43:59 +0100
Subject: [PATCH] Resolve "Add clear function that reallocs to
 DEFAULT_CAPACITY"

---
 stack.c | 20 +++++++++++++++-----
 stack.h |  5 +++++
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/stack.c b/stack.c
index 209089f..fb5d80d 100644
--- a/stack.c
+++ b/stack.c
@@ -1,9 +1,6 @@
-#include <stdlib.h>
-#include <stdbool.h>
-#include <stdio.h>
-
 #include "stack.h"
-
+#include <stdio.h>
+#include <stdlib.h>
 
 #define DEFAULT_CAPACITY 4
 
@@ -87,4 +84,17 @@ int get_length(stack s) {
 bool stack_is_empty(stack s)
 {
     return s.top == -1;
+}
+
+/**
+ * @brief Reset the stack's top to -1
+ *        New values will overwrite the old ones 
+ * 
+ * @param s
+ */
+void stack_clear(stack *s) 
+{
+    s->top = -1;
+    s->capacity = DEFAULT_CAPACITY;
+    s->data = realloc(s->data, sizeof(int) * s->capacity);
 }
\ No newline at end of file
diff --git a/stack.h b/stack.h
index 5aa60b4..48803dd 100644
--- a/stack.h
+++ b/stack.h
@@ -1,6 +1,10 @@
+
 #ifndef _STACK_H_
 #define _STACK_H_
 
+#include <stdbool.h>
+
+
 typedef struct _stack {
     int *data;
     int capacity;
@@ -17,6 +21,7 @@ void stack_peek(stack s, int *value);
 void stack_clone(stack s, stack *clone);
 int get_length(stack s);
 bool stack_is_empty(stack s);
+void stack_clear(stack *s) ;
 
 void stack_print(const stack s);
 
-- 
GitLab