Update cv4/a_train.c
This commit is contained in:
parent
1c0b2f5e47
commit
463045201d
@ -1,18 +1,64 @@
|
||||
#include "a_train.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Додаємо вагон в кінець списку
|
||||
struct car* add_car(struct car* first, const char* target) {
|
||||
return NULL;
|
||||
struct car* newcar = (struct car*)calloc(1, sizeof(struct car));
|
||||
if (newcar == NULL) {
|
||||
return first;
|
||||
}
|
||||
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("Station: %s\n", this->value);
|
||||
this = this->next;
|
||||
}
|
||||
}
|
||||
|
||||
// Видаляємо весь список (звільняємо пам'ять)
|
||||
void cancel_train(struct car* first) {
|
||||
struct car* this = first;
|
||||
while (this != NULL) {
|
||||
struct car* next = this->next;
|
||||
free(this);
|
||||
this = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Видаляємо всі вагони з певною станцією
|
||||
struct car* clear_train(struct car* first, const char* target) {
|
||||
return NULL;
|
||||
struct car* prev = NULL;
|
||||
struct car* this = first;
|
||||
while (this != NULL) {
|
||||
if (strcmp(this->value, target) == 0) {
|
||||
if (prev == NULL) {
|
||||
first = this->next;
|
||||
free(this);
|
||||
this = first;
|
||||
} else {
|
||||
prev->next = this->next;
|
||||
free(this);
|
||||
this = prev->next;
|
||||
}
|
||||
} else {
|
||||
prev = this;
|
||||
this = this->next;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user