usaa25/du4/a_train.c
2025-11-04 11:24:57 +01:00

65 lines
1.1 KiB
C

#include "a_train.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct car* add_car(struct car* first,const char* target) {
struct car* newcar = calloc(1,sizeof(struct car));
if(!newcar) return first;
strcpy(newcar->value, target);
newcar->next = NULL;
if(first == NULL){
return newcar;
}
struct car* temp = first;
while(temp->next !=NULL){
temp = temp->next;
}
temp->next = newcar;
return first;
}
void print_train(struct car* first) {
struct car* temp = first;
while(temp != NULL) {
printf("%s\n",temp->value);
temp = temp->next;
}
}
void cancel_train(struct car* first) {
struct car* temp;
while(first != NULL){
temp = first -> next;
free(first);
first = temp;
}
}
struct car* clear_train(struct car* first, const char* target) {
struct car* temp = first;
struct car* prev = NULL;
while(temp != NULL){
if(strcmp(temp->value, target)== 0){
if(prev == NULL){
first= temp->next;
free(temp);
temp= first;
}else{
prev->next= temp->next;
free(temp);
temp = prev->next;
}
}else{
prev= temp;
temp =temp->next;
}
}
return first;
}