71 lines
1.3 KiB
C
71 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "a_train.h"
|
|
|
|
struct car* add_car(struct car* first,const char* target) {
|
|
struct car* newcar = calloc(1, sizeof(struct car));
|
|
if(target == NULL){
|
|
exit(0);
|
|
}
|
|
|
|
strcpy(newcar->value, target);
|
|
|
|
if(first == NULL){
|
|
return newcar;
|
|
}
|
|
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);
|
|
}
|
|
/*struct car* this = first;
|
|
if(first == NULL){
|
|
exit(0);
|
|
}
|
|
else{
|
|
while(this != NULL){
|
|
printf("%s\n", this->value);
|
|
this = this->next;
|
|
}
|
|
}*/
|
|
}
|
|
|
|
void cancel_train(struct car* first) {
|
|
if(first == NULL){
|
|
exit(0);
|
|
}
|
|
else if(first->next == NULL){
|
|
free(first);
|
|
//first = NULL;
|
|
}
|
|
else{
|
|
cancel_train(first->next);
|
|
first->next = NULL;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
}
|
|
|