From a395055ead7be838d86ce8c3698ec8c9d669e4ce Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Sun, 1 Nov 2020 19:41:36 +0100 Subject: [PATCH] train --- cv5/a_train.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/cv5/a_train.c b/cv5/a_train.c index 12d27bd..e9c0062 100644 --- a/cv5/a_train.c +++ b/cv5/a_train.c @@ -32,16 +32,27 @@ void print_train(struct car* first) { } void cancel_train(struct car* first) { + struct car* this; if(first == NULL){ exit(0); } - if(first->next == NULL){ - struct car* this = first; + else if(first->next == NULL){ + this = first; first = first->next; free(this); + first = NULL; } - cancel_train(first->next); - free(first); + else{ + struct car* prev = first; + while(prev->next->next != NULL){ + prev = prev->next; + } + this = prev->next; + prev->next = NULL; + free(this); + } + //cancel_train(first->next); + //free(first); }