diff --git a/cv9/snake.c b/cv9/snake.c index f1f94e7..3c522f6 100644 --- a/cv9/snake.c +++ b/cv9/snake.c @@ -11,10 +11,25 @@ struct snake* add_snake(struct snake* snake, int x, int y) { } struct snake* remove_snake(struct snake* snake) { - if (snake == NULL) return NULL; - struct snake* hlava = snake->next; - free(snake); - return hlava; + if (!snake) return NULL; + + struct snake* current = snake; + struct snake* previous = NULL; + + while (current->next) { + previous = current; + current = current->next; + } + + if (previous) { + previous->next = NULL; + } else { + free(current); + return NULL; + } + + free(current); + return snake; } void free_snake(struct snake* sn){