diff --git a/cv9/snake.c b/cv9/snake.c index 404df01..a897483 100644 --- a/cv9/snake.c +++ b/cv9/snake.c @@ -9,18 +9,24 @@ struct snake* add_snake(struct snake* snake,int x,int y){ return head; } -struct snake* remove_snake(struct snake* snake){ - struct snake* head = calloc(1,sizeof(struct snake)); - struct snake* this = head; - this = this->next; - this = head; - while (this != NULL){ - this= this->next; +struct snake* remove_snake(struct snake* snake) { + struct snake* temp = snake; + if (snake!= NULL) { + snake = snake->next; + free(temp); } - return head; + return snake; } -void free_snake(struct snake* sn){ +void free_snake(struct snake* sn) { + struct snake* current = sn; + struct snake* next; + + while (current!= NULL) { + next = current->next; + free(current); + current = next; + } } int is_snake(struct snake* snake,int x,int y){