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