diff --git a/du7/snake.c b/du7/snake.c index da890dc..0f307bf 100644 --- a/du7/snake.c +++ b/du7/snake.c @@ -10,15 +10,20 @@ struct snake* add_snake(struct snake* snake, int x, int y) { } struct snake* remove_snake(struct snake* snake) { -struct snake* this = head; -this = this->next; -while (this->next != NULL){ - this = this->next; -} -this = head; -while (this != NULL){ - // Niečo môžme s prvkom this spraviť - this= this->next; + if (snake == NULL || snake->next == NULL) { + free(snake); + return NULL; + } + struct snake* current = snake; + struct snake* prev = NULL; + while (current->next != NULL) { + prev = current; + current = current->next; + } + free(current); + current = NULL; + prev->next = NULL; + } }