75 lines
1.4 KiB
C
75 lines
1.4 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));
|
|
strcpy(newcar->value, target);
|
|
newcar->next=NULL;
|
|
if (first==NULL)
|
|
{
|
|
return newcar;
|
|
}
|
|
struct car* this=first;
|
|
while (this->next!=NULL)
|
|
{
|
|
this=this->next;
|
|
}
|
|
this->next=newcar;
|
|
return first;
|
|
return NULL;
|
|
}
|
|
|
|
void print_train(struct car* first)
|
|
{
|
|
struct car* current=first;
|
|
while (current!=NULL)
|
|
{
|
|
printf("%s\n", current->value);
|
|
current=current->next;
|
|
}
|
|
}
|
|
|
|
void cancel_train(struct car* first)
|
|
{
|
|
struct car* current=first;
|
|
while (current!=NULL)
|
|
{
|
|
struct car* next=current->next;
|
|
free(current);
|
|
current=next;
|
|
}
|
|
}
|
|
|
|
|
|
struct car* clear_train(struct car* first, const char* target)
|
|
{
|
|
while (first!=NULL && strcmp(first->value, target)==0)
|
|
{
|
|
struct car* temp=first;
|
|
first=first->next;
|
|
free(temp);
|
|
}
|
|
if (first==NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
struct car* current=first;
|
|
while (current->next!=NULL)
|
|
{
|
|
if (strcmp(current->next->value, target)==0)
|
|
{
|
|
struct car* temp=current->next;
|
|
current->next=current->next->next;
|
|
free(temp);
|
|
}
|
|
else
|
|
{
|
|
current=current->next;
|
|
}
|
|
}
|
|
return first;
|
|
}
|