usaa20/cv5/a_train.c
Maryna Kravtsova a395055ead train
2020-11-01 19:41:36 +01:00

73 lines
1.4 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));
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) {
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) {
struct car* this;
if(first == NULL){
exit(0);
}
else if(first->next == NULL){
this = first;
first = first->next;
free(this);
first = NULL;
}
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);
}
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;
}