81 lines
1.7 KiB
C
81 lines
1.7 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 = (struct car*)calloc(1, sizeof(struct car));
|
|
if (newcar == NULL) {
|
|
printf("Ошибка: не удалось выделить память.\n");
|
|
exit(1);
|
|
}
|
|
|
|
strcpy(newcar->value, target);
|
|
newcar->next = NULL;
|
|
|
|
if (first == NULL) {
|
|
return newcar;
|
|
}
|
|
|
|
struct car* current = first;
|
|
while (current->next != NULL) {
|
|
current = current->next;
|
|
}
|
|
|
|
current->next = newcar;
|
|
return first;
|
|
}
|
|
|
|
|
|
void print_train(struct car* first) {
|
|
struct car* current = first;
|
|
|
|
if (current == NULL) {
|
|
printf("Поезд пуст.\n");
|
|
return;
|
|
}
|
|
|
|
while (current != NULL) {
|
|
printf("%s\n", current->value);
|
|
current = current->next;
|
|
}
|
|
}
|
|
|
|
|
|
void cancel_train(struct car* first) {
|
|
if (first == NULL) {
|
|
return;
|
|
}
|
|
|
|
cancel_train(first->next);
|
|
free(first);
|
|
}
|
|
|
|
|
|
struct car* clear_train(struct car* first, const char* target) {
|
|
struct car* prev = NULL;
|
|
struct car* current = first;
|
|
|
|
while (current != NULL) {
|
|
if (strcmp(current->value, target) == 0) {
|
|
if (prev == NULL) {
|
|
struct car* temp = current->next;
|
|
free(current);
|
|
current = temp;
|
|
first = current;
|
|
} else {
|
|
prev->next = current->next;
|
|
free(current);
|
|
current = prev->next;
|
|
}
|
|
} else {
|
|
prev = current;
|
|
current = current->next;
|
|
}
|
|
}
|
|
|
|
return first;
|
|
}
|
|
|