usaa20/cv5/a_train.c
2020-11-06 07:24:19 +01:00

55 lines
1.0 KiB
C

#include "a_train.h"
#include <stdio.h>
struct car* add_car(struct car* first,const char* target) {
struct car *newCar = calloc(1,sizeof(struct car));
strcpy(newCar->value,target);
if(first==NULL){
first = newCar;
}else{
struct car* this = first;
while(this->next!=NULL){
this = this->next;
}
this->next = newCar;
}
return first;
}
void print_train(struct car* first) {
for(struct car* this = first;this!=NULL;this = this->next){
printf("%s\n",this->value);
}
}
void cancel_train(struct car* first){
if(first==NULL){
return;
}
cancel_train(first->next);
free(first);
}
struct car* clear_train(struct car* first, const char* target) {
struct car *previous = NULL;
for(struct car *this = first;this!=NULL;this=this->next){
if(strcmp(this->value,target)==0){
if(previous==NULL){
first = first->next;
free(this);
break;
}else{
previous->next = this->next;
free(this);
break;
}
}
previous = this;
}
return first;
}