pvjc25/du5/snake.c
2025-04-08 19:19:05 +02:00

44 lines
826 B
C

#include <stdio.h>
#include "snake.h"
#include <stdlib.h>
struct snake* add_snake (struct snake* snake, int x, int y) {
struct snake* head = NULL;
head = malloc(sizeof(struct snake));
if (!head) {
return snake;
}
head->x = x;
head->y = y;
head->next = NULL;
if (snake !=NULL) {
head->next = snake;
}
return head;
}
struct snake* remove_snake(struct snake* snake) {
if(snake == NULL || snake-> next == NULL) {
free(snake);
return NULL;
}
struct snake* previous = NULL;
struct snake* current = NULL;
while (current->next !=NULL) {
previous = current;
current = current->next;
}
if (previous !=NULL) {
free(previous->next);
previous->next = NULL;
}
}