usaa20/cv5/a_train.c

57 lines
1.1 KiB
C
Raw Normal View History

2020-10-26 14:07:14 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2020-10-26 13:38:06 +00:00
#include "a_train.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){
2020-10-26 14:49:01 +00:00
return newcar;
2020-10-26 13:38:06 +00:00
}
2020-10-26 14:49:01 +00:00
struct car *this = first;
while(this->next != NULL){
this = this->next;
2020-10-26 13:38:06 +00:00
}
2020-10-26 14:49:01 +00:00
this->next = newcar;
2020-10-26 13:38:06 +00:00
return first;
}
void print_train(struct car* first) {
struct car* this = first;
if(first == NULL){
2020-10-26 16:25:34 +00:00
exit(0);
2020-10-26 13:38:06 +00:00
}
else{
while(this != NULL){
printf("%s\n", this->value);
this = this->next;
}
}
}
void cancel_train(struct car* first) {
2020-10-26 14:49:01 +00:00
if(first == NULL){
2020-10-26 16:25:34 +00:00
exit(0);
2020-10-26 13:38:06 +00:00
}
2020-10-26 14:49:01 +00:00
cancel_train(first->next);
free(first);
2020-10-26 13:38:06 +00:00
}
struct car* clear_train(struct car* first, const char* target) {
struct car* this = first;
while(this != NULL){
if(this->value == target){
return NULL;
}
this = this->next;
}
return first;
}