66 lines
1.3 KiB
C
66 lines
1.3 KiB
C
#include "a_train.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
struct car* add_car(struct car* first,const char* target) {
|
|
if(first == NULL){
|
|
first = calloc(1, sizeof(struct car));
|
|
strcpy(first->value, target);
|
|
return first;
|
|
}
|
|
|
|
struct car* temp = first;
|
|
while(temp->next){
|
|
temp = temp->next;
|
|
}
|
|
struct car* newCar = (struct car*)calloc(1, sizeof(struct car));
|
|
strcpy(newCar->value, target);
|
|
temp->next = newCar;
|
|
return first;
|
|
|
|
}
|
|
|
|
void print_train(struct car* first) {
|
|
struct car* temp = first;
|
|
while(temp){
|
|
printf("%s\n", temp->value);
|
|
temp = temp->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* curr = first;
|
|
struct car* prev = NULL;
|
|
|
|
while (curr) {
|
|
if (strcmp(curr->value, target) == 0) {
|
|
struct car* to_delete = curr;
|
|
if (prev)
|
|
prev->next = curr->next;
|
|
else
|
|
first = curr->next;
|
|
curr = curr->next;
|
|
free(to_delete);
|
|
} else {
|
|
prev = curr;
|
|
curr = curr->next;
|
|
}
|
|
}
|
|
|
|
return first;
|
|
}
|
|
|