diff --git a/slides/exemples/dyn_stack.c b/slides/exemples/dyn_stack.c new file mode 100644 index 0000000000000000000000000000000000000000..81aeb0edefbb5c3e2e3fb0d764875f1476cf8da7 --- /dev/null +++ b/slides/exemples/dyn_stack.c @@ -0,0 +1,139 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <assert.h> + +#define MIN_CAPACITY 5 + +struct stack { + int top; + int capacity; + int *data; +}; + +void stack_init(struct stack *s, int capacity) { + s->top = -1; + s->data = malloc(capacity * sizeof(int)); + s->capacity = capacity; +} + +bool stack_is_empty(struct stack s) { + return s.top == -1; +} + +void stack_push(struct stack *s, int val) { + if (s->top != s->capacity - 1) { + s->top += 1; + s->data[s->top] = val; + } else { + s->capacity *= 2; + s->data = realloc(s->data, s->capacity * sizeof(int)); + s->top += 1; + s->data[s->top] = val; + } +} + +/*int stack_pop(struct stack *s) {*/ +/* if (!stack_is_empty(*s)) {*/ +/* int tmp = s->data[s->top];*/ +/* s->top -= 1;*/ +/* return tmp;*/ +/* }*/ +/*}*/ + +/*void stack_pop(struct stack *s, int *val) {*/ +/* if (!stack_is_empty(*s)) {*/ +/* *val = s->data[s->top];*/ +/* s->top -= 1;*/ +/* }*/ +/*}*/ + +int *stack_pop(struct stack *s) { + if (!stack_is_empty(*s)) { + int *val = &(s->data[s->top]); + s->top -= 1; + if (s->top < s->capacity / 4 && (s->capacity / 2) > MIN_CAPACITY) { + s->capacity /= 2; + s->data = realloc(s->data, s->capacity * sizeof(int)); + } + return val; + } else { + return NULL; + } +} + +int *stack_peek(struct stack s) { + if (!stack_is_empty(s)) { + int *val = &(s.data[s.top]); + return val; + } else { + return NULL; + } +} + +void stack_print(struct stack s) { + printf("capacity = %d\n", s.capacity); + for (int i = s.top; i >= 0; --i) { + printf("%d\n", s.data[i]); + } +} + +int main() { + struct stack s; + stack_init(&s, 5); + stack_print(s); + + printf("s.top = %d\n", s.top); + printf("is_empty(): %s\n", stack_is_empty(s) ? "True" : "False"); + stack_push(&s, 10); + stack_push(&s, 20); + stack_push(&s, 30); + stack_push(&s, 40); + stack_push(&s, 50); + stack_push(&s, 60); + stack_push(&s, 70); + stack_push(&s, 80); + stack_push(&s, 90); + stack_push(&s, 100); + stack_push(&s, 110); + printf("is_empty(): %s\n", stack_is_empty(s) ? "True" : "False"); + stack_print(s); + /*int val = -1;*/ + /*stack_pop(&s, &val);*/ + /*printf("popped value = %d\n", val);*/ + /*stack_pop(&s, &val);*/ + /*printf("popped value = %d\n", val);*/ + /*stack_pop(&s, &val);*/ + /*printf("popped value = %d\n", val);*/ + /*stack_pop(&s, &val);*/ + /*printf("popped value = %d\n", val);*/ + /*stack_pop(&s, &val);*/ + /*printf("popped value = %d\n", val);*/ + /*stack_pop(&s, &val);*/ + /*printf("popped value = %d\n", val);*/ + + /*printf("popped value = %d\n", stack_pop(&s));*/ + /*printf("popped value = %d\n", stack_pop(&s));*/ + /*printf("popped value = %d\n", stack_pop(&s));*/ + /*printf("popped value = %d\n", stack_pop(&s));*/ + /*printf("popped value = %d\n", stack_pop(&s));*/ + /*printf("popped value = %d\n", stack_pop(&s));*/ + + printf("peeked value = %d\n", *stack_peek(s)); + printf("peeked value = %d\n", *stack_peek(s)); + stack_print(s); + + printf("popped value = %d\n", *stack_pop(&s)); + printf("popped value = %d\n", *stack_pop(&s)); + printf("popped value = %d\n", *stack_pop(&s)); + printf("popped value = %d\n", *stack_pop(&s)); + printf("popped value = %d\n", *stack_pop(&s)); + printf("popped value = %d\n", *stack_pop(&s)); + printf("popped value = %d\n", *stack_pop(&s)); + printf("popped value = %d\n", *stack_pop(&s)); + /*printf("popped value = %d\n", *stack_pop(&s));*/ + stack_print(s); + printf("is_empty(): %s\n", stack_is_empty(s) ? "True" : "False"); + + return EXIT_SUCCESS; +} diff --git a/slides/exemples/stack.c b/slides/exemples/stack.c index eb5cedfea75f191cefae564e87040f7f5fd0acbd..9e07a0c2391dac44be82ea09b7c62344af0f8887 100644 --- a/slides/exemples/stack.c +++ b/slides/exemples/stack.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> +#include <assert.h> #define MAX_CAPACITY 5 @@ -18,6 +19,7 @@ bool stack_is_empty(struct stack s) { } void stack_push(struct stack *s, int val) { + assert(s->top < MAX_CAPACITY - 1); if (s->top != MAX_CAPACITY - 1) { s->top += 1; s->data[s->top] = val; @@ -40,6 +42,8 @@ void stack_push(struct stack *s, int val) { /*}*/ int *stack_pop(struct stack *s) { + assert(!stack_is_empty(*s)); + if (!stack_is_empty(*s)) { int *val = &(s->data[s->top]); s->top -= 1;