From bd05b81d3ee664e649feec074dc3e160c3793a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Vasi=C4=BE?= Date: Tue, 26 Apr 2022 21:14:38 +0200 Subject: [PATCH] domaca_uloha_7 --- du7/snake.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) 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){