This commit is contained in:
Bohdan Kapliuk 2024-10-21 17:15:46 +03:00
parent 67305af891
commit 6f7842f86d

View File

@ -6,6 +6,9 @@
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;
@ -15,21 +18,41 @@ struct car* add_car(struct car* first,const char* target) {
}
void print_train(struct car* first) {
while(first != NULL){
printf("%s", first->value);
first = first->next;
struct car* this = first;
while (this != NULL) {
printf("%s\n", this->value);
this = this->next;
}
}
void cancel_train(struct car* first) {
while(first != NULL){
free(first->next);
first = first->next;
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;
}
struct car* clear_train(struct car* first, const char* target) {
return NULL;
}