usaa24/cv4/a_train.c
Bohdan Kapliuk 6f7842f86d cv4
2024-10-21 17:15:46 +03:00

59 lines
1.4 KiB
C

#include "a_train.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.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;
while (this != NULL) {
printf("%s\n", this->value);
this = this->next;
}
}
void cancel_train(struct car* first) {
struct car* this = first;
while (this != NULL) {
struct car* next_car = this->next;
free(this);
this = next_car;
}
}
struct car* clear_train(struct car* first, const char* name) {
struct car* this = first;
struct car* prev = NULL;
while (this != NULL) {
if (strcmp(this->value, name) == 0) {
if (prev == NULL) {
struct car* temp = this->next;
free(this);
return clear_train(temp, name);
} else {
prev->next = this->next;
free(this);
return first;
}
}
prev = this;
this = this->next;
}
return first;
}