usaa24/cv4/a_train.c

68 lines
1.5 KiB
C
Raw Normal View History

2024-10-15 12:34:20 +00:00
#include "a_train.h"
#include <stdio.h>
2024-10-15 13:43:00 +00:00
#include <stdlib.h>
#include <string.h>
2024-10-15 12:34:20 +00:00
struct car* add_car(struct car* first,const char* target) {
2024-10-15 13:43:00 +00:00
struct car* last = first;
struct car* newcar* = calloc(1, sizeof(struct car));
strcpy(newcar->value, target);
if (last != NULL){
struct car* current = last;
while(current->next != NULL){
current = current->next
}
current->next = newcar;
} else{
last = newcar;
return last;
}
return first;
2024-10-15 12:34:20 +00:00
}
void print_train(struct car* first) {
2024-10-15 13:43:00 +00:00
if(first != NULL){
struct car* current = first;
while(current->next != NULL){
printf("%s\n", current->value);
current = current->next;
}
}
2024-10-15 12:34:20 +00:00
}
void cancel_train(struct car* first) {
2024-10-15 13:43:00 +00:00
struct car* current = first;
while(first != NULL){
current = current->next;
free(first);
first = current;
}
2024-10-15 12:34:20 +00:00
}
struct car* clear_train(struct car* first, const char* target) {
2024-10-15 13:43:00 +00:00
if (first = NULL){
return NULL;
}
struct car* current = first, new_first = first;
if (first != NULL){
if(strcmp(first->destination, target) == 0){
new_first = new_first->next;
free(first);
first = new_first;
}
}
while (first->next != NULL) {
current = first->next;
if (strcmp(current->destination, target) == 0) {
first->next = current->next;
free(current);
} else {
first = current;
}
}
return new_first;
2024-10-15 12:34:20 +00:00
}