From f6a441cc6495e4a9443f711c55561c8ebd6d1466 Mon Sep 17 00:00:00 2001 From: Denys Sanchuk Date: Tue, 8 Apr 2025 12:25:05 +0000 Subject: [PATCH] Aktualizovat du5/program.c --- du5/program.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/du5/program.c b/du5/program.c index e69de29..f7efbe0 100644 --- a/du5/program.c +++ b/du5/program.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include "snake.h" + + +struct snake *add_snake(struct snake *snake, int x, int y) { + struct snake *new_head = (struct snake *)malloc(sizeof(struct snake)); + if (!new_head) { + return NULL; + } + new_head->x = x; + new_head->y = y; + new_head->next = snake; + return new_head; +} + +struct snake *remove_snake(struct snake *snake) { + if (!snake) { + return NULL; + } + + if (!snake->next) { + free(snake); + return NULL; + } + + struct snake *current = snake; + while (current->next && current->next->next) { + current = current->next; + } + + free(current->next); + current->next = NULL; + return snake; +} + +int is_snake(struct snake *snake, int x, int y) { + struct snake *current = snake; + while (current) { + if (current->x == x && current->y == y) { + return 1; + } + current = current->next; + } + return 0; +} +