usaa25/du4/a_train.c
2025-11-06 22:47:42 +01:00

59 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* new = calloc(1, sizeof(struct car));
if(!new) return first;
strcpy(new->value,target);
if (first==NULL){
return new;
}
struct car* sm = first;
while(sm->next!=NULL){
sm = sm->next;
}
sm->next = new;
return first;
}
void print_train(struct car* first) {
struct car* hlp = first;
while(hlp!=NULL){
printf("Cielova stanica je %s\n",hlp->value);
hlp = hlp->next;
}
}
void cancel_train(struct car* first) {
if(!first) return;
cancel_train(first->next);
free(first);
}
struct car* clear_train(struct car* first, const char* target) {
if(!first) return NULL;
if (strcmp(first->value, target)==0){
struct car* tmp = first->next;
free(first);
return tmp;
}
struct car* hlp = first;
while (hlp->next!=NULL&&strcmp(hlp->next->value,target)!=0){
hlp = hlp->next;
}
if (hlp->next!=NULL&&strcmp(hlp->next->value,target)==0){
struct car* del = hlp->next;
hlp->next=hlp->next->next;
free(del);
}
return first;
}