diff --git a/du7/snake.c b/du7/snake.c index 3d38897..d8abbd9 100644 --- a/du7/snake.c +++ b/du7/snake.c @@ -18,11 +18,23 @@ 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* nasledujuci = calloc(1, sizeof(struct snake)); - nasledujuci->next = snake->next; - free(snake); + else if (snake->next == NULL) { + free(snake); + return NULL; + } - return nasledujuci; + else { + struct snake* medzipamat = calloc(1, sizeof(struct snake)); + medzipamat = snake + + while (1) { + snake = snake->next; + if (snake->next->next == NULL) break; + } + free(snake->next->next); + snake->next = NULL; + return medzipamat; + } } void free_snake(struct snake* sn){