usaa24/cv6/a_station.c

73 lines
1.6 KiB
C
Raw Normal View History

2024-11-18 16:43:08 +00:00
#include "a_train.h"
2024-11-08 13:24:45 +00:00
#include <stdio.h>
2024-11-01 10:00:40 +00:00
#include <stdlib.h>
#include <string.h>
2024-11-18 16:43:08 +00:00
struct car* add_car(struct car* first, const char* target) {
struct car* last = first;
struct car* newcar = calloc(1, sizeof(struct car));
strcpy(newcar->value, target);
2024-11-09 21:41:39 +00:00
2024-11-18 16:43:08 +00:00
if (last != NULL) {
struct car* current = last;
while (current->next != NULL) {
2024-11-09 21:41:39 +00:00
current = current->next;
}
2024-11-18 16:43:08 +00:00
current->next = newcar;
} else {
last = newcar;
return last;
2024-11-09 21:41:39 +00:00
}
2024-11-01 10:00:40 +00:00
2024-11-18 16:43:08 +00:00
return first;
2024-11-01 10:00:40 +00:00
}
2024-11-18 16:43:08 +00:00
void print_train(struct car* first) {
if(first != NULL){
struct car* current = first;
while(current->next != NULL){
printf("%s\n", current->value);
current = current->next;
2024-11-09 21:41:39 +00:00
}
}
2024-11-01 10:00:40 +00:00
}
2024-11-18 16:43:08 +00:00
void cancel_train(struct car* first) {
struct car* current = first;
while(first != NULL){
2024-11-09 21:41:39 +00:00
current = current->next;
2024-11-18 16:43:08 +00:00
free(first);
first = current;
2024-11-09 21:41:39 +00:00
}
2024-11-01 10:00:40 +00:00
}
2024-11-08 13:58:56 +00:00
2024-11-18 16:43:08 +00:00
struct car* clear_train(struct car* first, const char* target) {
if (first == NULL) {
return NULL;
}
2024-11-01 10:00:40 +00:00
2024-11-18 16:43:08 +00:00
struct car* current = first;
struct car* previous = NULL;
while (current != NULL && strcmp(current->value, target) == 0) {
struct car* temp = current;
first = current->next;
current = first;
free(temp);
2024-11-09 21:41:39 +00:00
}
2024-11-01 10:00:40 +00:00
2024-11-18 16:43:08 +00:00
while (current != NULL) {
if (strcmp(current->value, target) == 0) {
previous->next = current->next;
free(current);
current = previous->next;
} else {
previous = current;
2024-11-09 21:41:39 +00:00
current = current->next;
}
}
2024-11-01 10:00:40 +00:00
2024-11-18 16:43:08 +00:00
return first;
}