usaa20/cv5/a_train.c
Maryna Kravtsova 7bd7f9c076 train
2020-11-02 16:59:59 +01:00

92 lines
1.8 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){
return 0;
}
if(first == NULL){
strcpy(newcar->value, target);
newcar->next = NULL;
return newcar;
}
strcpy(newcar->value,target);
newcar->next = NULL;
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){
return 0;
}
if(first->next != NULL){
cancel_train(first->next);
first->next = NULL;
}
else if(first->next == NULL){
free(first);
// cancel_train(first->next);
}
//free(first);
}
struct car* clear_train(struct car* first, const char* target) {
if(first == NULL){
return 0;
}
int x;
if(first->next == NULL){
x = strcmp(first->value; target);
if(x == 0){
free(first);
return 0;
}
}
else{
struct car* prev = first;
while(prev->next->next != NULL){
x = strcmp(prev->next->value, target);
if(x == 0){
struct car* third = prev->next->next;
free(prev->next);
break;
}
prev = prev->next;
}
}
return first;
}