Update 'cv4/a_train.c'
This commit is contained in:
parent
ba657296b1
commit
e14db80e39
@ -0,0 +1,67 @@
|
|||||||
|
#include "train.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
struct car* add_car(struct car* first, const char* target) {
|
||||||
|
struct car* new_car = (struct car*)calloc(1, sizeof(struct car));
|
||||||
|
if (new_car == NULL) {
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(new_car->value, target);
|
||||||
|
new_car->next = NULL;
|
||||||
|
|
||||||
|
if (first == NULL) {
|
||||||
|
return new_car;
|
||||||
|
} else {
|
||||||
|
struct car* current = first;
|
||||||
|
while (current->next != NULL) {
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
current->next = new_car;
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_train(struct car* first) {
|
||||||
|
struct car* current = first;
|
||||||
|
while (current != NULL) {
|
||||||
|
printf("%s\n", current->value);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cancel_train(struct car* first) {
|
||||||
|
while (first != NULL) {
|
||||||
|
struct car* current = first;
|
||||||
|
first = first->next;
|
||||||
|
free(current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct car* clear_train(struct car* first, const char* target) {
|
||||||
|
struct car* new_first = first;
|
||||||
|
struct car* prev = NULL;
|
||||||
|
struct car* current = first;
|
||||||
|
|
||||||
|
while (current != NULL) {
|
||||||
|
if (strcmp(current->value, target) == 0) {
|
||||||
|
if (prev == NULL) {
|
||||||
|
new_first = current->next;
|
||||||
|
free(current);
|
||||||
|
current = new_first;
|
||||||
|
} else {
|
||||||
|
prev->next = current->next;
|
||||||
|
free(current);
|
||||||
|
current = prev->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
prev = current;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new_first;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user