Skip to content
Snippets Groups Projects
Commit e7553071 authored by ines.maya's avatar ines.maya
Browse files

add gitignore et makefile,mon pop est ok encore à verifier

parent 981decc1
No related branches found
No related tags found
1 merge request!16Resolve "Add pop function"
This commit is part of merge request !16. Comments created here will be created in the context of that merge request.
.gitignore 0 → 100644
*.o
main
\ No newline at end of file
Makefile 0 → 100644
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
...@@ -4,7 +4,19 @@ int main(){ ...@@ -4,7 +4,19 @@ int main(){
stack s; stack s;
int value = 0; int value = 0;
stack_init(&s); 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_is_empty(stack s)
stack_peek(s, &value);
stack_pop(&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
...@@ -21,13 +21,18 @@ void stack_pop(stack *s, int *value){ ...@@ -21,13 +21,18 @@ void stack_pop(stack *s, int *value){
if (stack_is_empty(*s)){ if (stack_is_empty(*s)){
return; return;
} }
if (s->top == s->capacity/4){
s->capacity /= 2;
}
*value = s->data[s->top]; *value = s->data[s->top];
s->top -= 1;
printf("pop : %d, top : %d\n", s->data[s->top],s->top); 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 // // si capacite 2x trop grande, on la rend plus petite
if (s->capacity > 2*s->top){ // if (s->capacity > 2*s->top){
s->capacity = s->top; // s->capacity = s->top;
s->data = realloc(s, sizeof(int)*s->capacity); // s->data = realloc(s, sizeof(int)*s->capacity);
} // }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment