usaa24/cv4/a_train.c

100 lines
1.5 KiB
C

#include <stdio.h>
#include"a_train.h"
struct car* add_car(struct car* first,const char* target)
{
struct car* ret=first;
struct car* nc=(struct car*) malloc(sizeof(struct car));
strcpy(nc->value, target);
nc->next=NULL;
if(first)
{
while(first->next!=NULL)
{
first=first->next;
}
first->next=nc;
return ret;
}
first=nc;
return first;
}
void print_train(struct car* first)
{
if(first==NULL)
{
return;
}
do
{
printf("%s", first->value);
first=first->next;
if(first) printf("->");
}while(first!=NULL);
}
void cancel_train(struct car* first)
{
struct car* t;
while(first!=NULL)
{
t=first;
first=first->next;
free(t);
}
}
struct car* clear_train(struct car* first, const char* target)
{
struct car* t;
struct car* ptr;
if(first==NULL || target==NULL)
{
return first;
}
while(strcmp(first->value, target)==0)
{
t=first;
first=first->next;
free(t);
if(first==NULL)
{
return NULL;
}
}
ptr=first;
do
{
if(strcmp(ptr->next->value, target)==0)
{
t=ptr->next;;
ptr->next = ptr -> next->next;
free(t);
}
else
{
ptr = ptr -> next;
}
}while(ptr->next);
return first;
}