From 3c3f421d335c28671c9e887878a6fbac2a408858 Mon Sep 17 00:00:00 2001 From: Vladyslav Korzun Date: Thu, 20 Apr 2023 06:17:06 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'du7/snake.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du7/snake.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) 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; + } }