From e7553071a63535722071e9d3000f906ac8643756 Mon Sep 17 00:00:00 2001
From: "ines.maya" <ines.maya@etu.hesge.ch>
Date: Sun, 28 Nov 2021 11:44:47 +0100
Subject: [PATCH] =?UTF-8?q?add=20gitignore=20et=20makefile,mon=20pop=20est?=
 =?UTF-8?q?=20ok=20encore=20=C3=A0=20verifier?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore |  2 ++
 Makefile   | 14 ++++++++++++++
 main.c     | 14 +++++++++++++-
 stack.c    | 17 +++++++++++------
 4 files changed, 40 insertions(+), 7 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 Makefile

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..96249ce
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.o
+main
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..8af5065
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
+CC=gcc
+CFLAGS=-Wall -Wextra -pedantic -g -std=c11 -O3
+CFLAGS_ASAN=-fsanitize=address -fno-omit-frame-pointer
+LDFLAGS=-lm
+LDFLAGS_ASAN=-fsanitize=address -fno-omit-frame-pointer
+
+main: stack.o main.o
+	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LDFLAGS_ASAN)
+
+%.o: %.c 
+	$(CC) $(CFLAGS) $(CFLAGS_ASAN) -c $^ -o $@
+
+clean:
+	rm -f *.o main
diff --git a/main.c b/main.c
index d42583f..28bc37d 100644
--- a/main.c
+++ b/main.c
@@ -4,7 +4,19 @@ int main(){
 	stack s;
 	int value = 0;
 	stack_init(&s);
+
+	for (int i = 0; i < s.capacity; i++){
+		s.top += 1;
+		s.data[s.top] = i;
+		printf("push : %d\n", s.data[s.top]);
+	}
+	
 	// stack_is_empty(stack s)
-	stack_peek(s, &value);
 	stack_pop(&s, &value);
+	stack_peek(s, &value);
+
+	free(s.data);
+	s.data = NULL;
+	s.capacity = -1;
+	s.top = -1;
 }
\ No newline at end of file
diff --git a/stack.c b/stack.c
index 9da70ef..feee064 100644
--- a/stack.c
+++ b/stack.c
@@ -21,13 +21,18 @@ void stack_pop(stack *s, int *value){
 	if (stack_is_empty(*s)){
 		return;
 	}	
+
+	if (s->top == s->capacity/4){
+		s->capacity /= 2;
+	}
+	
 	*value = s->data[s->top];
-	s->top -= 1;
 	printf("pop : %d, top : %d\n", s->data[s->top],s->top);
+	s->top -= 1;
 
-	// si capacite 2x trop grande, on la rend plus petite
-	if (s->capacity > 2*s->top){
-		s->capacity = s->top;
-		s->data = realloc(s, sizeof(int)*s->capacity);
-	}
+	// // si capacite 2x trop grande, on la rend plus petite
+	// if (s->capacity > 2*s->top){
+	// 	s->capacity = s->top;
+	// 	s->data = realloc(s, sizeof(int)*s->capacity);
+	// }
 }
-- 
GitLab