usaa24/cv4/a_train.c

98 lines
1.5 KiB
C
Raw Permalink Normal View History

2024-10-16 08:23:13 +00:00
#include <stdio.h>
2024-10-23 19:37:45 +00:00
#include"a_train.h"
2024-10-23 19:55:03 +00:00
2024-10-23 19:35:27 +00:00
2024-10-24 21:22:04 +00:00
2024-10-23 19:35:27 +00:00
struct car* add_car(struct car* first,const char* target)
{
2024-10-24 21:02:16 +00:00
struct car* ret=first;
struct car* nc=(struct car*) malloc(sizeof(struct car));
2024-10-23 19:35:27 +00:00
strcpy(nc->value, target);
nc->next=NULL;
if(first)
{
while(first->next!=NULL)
{
first=first->next;
}
first->next=nc;
2024-10-23 19:55:03 +00:00
return ret;
2024-10-23 19:35:27 +00:00
}
first=nc;
return first;
2024-10-16 08:23:13 +00:00
}
2024-10-23 19:35:27 +00:00
void print_train(struct car* first)
{
if(first==NULL)
{
return;
}
do
{
2024-10-23 19:47:05 +00:00
printf("%s", first->value);
2024-10-23 19:35:27 +00:00
first=first->next;
2024-10-24 21:02:16 +00:00
if(first) printf("->");
2024-10-23 19:47:05 +00:00
}while(first!=NULL);
2024-10-16 08:23:13 +00:00
}
2024-10-23 19:35:27 +00:00
void cancel_train(struct car* first)
{
struct car* t;
while(first!=NULL)
{
t=first;
first=first->next;
free(t);
}
2024-10-16 08:23:13 +00:00
}
2024-10-23 19:35:27 +00:00
struct car* clear_train(struct car* first, const char* target)
{
2024-10-24 21:02:16 +00:00
struct car* t;
struct car* ptr;
2024-10-16 08:23:13 +00:00
2024-10-23 19:35:27 +00:00
if(first==NULL || target==NULL)
{
return first;
}
2024-10-24 21:22:04 +00:00
2024-10-24 21:02:16 +00:00
while(strcmp(first->value, target)==0)
2024-10-23 19:35:27 +00:00
{
t=first;
first=first->next;
free(t);
2024-10-24 21:22:04 +00:00
2024-10-23 19:35:27 +00:00
if(first==NULL)
{
2024-10-24 21:22:04 +00:00
2024-10-23 19:35:27 +00:00
return NULL;
2024-10-24 21:02:16 +00:00
}
2024-10-23 19:35:27 +00:00
}
2024-10-24 21:02:16 +00:00
2024-10-24 21:22:04 +00:00
ptr=first;
while (ptr->next != NULL)
2024-10-23 19:35:27 +00:00
{
2024-10-24 21:22:04 +00:00
if (strcmp(ptr->next->value, target) == 0)
2024-10-23 19:35:27 +00:00
{
2024-10-24 21:22:04 +00:00
t = ptr->next;
ptr->next = ptr->next->next;
2024-10-23 19:35:27 +00:00
free(t);
}
else
{
2024-10-24 21:22:04 +00:00
ptr = ptr->next;
2024-10-23 19:35:27 +00:00
}
2024-10-24 21:22:04 +00:00
}
2024-10-23 19:35:27 +00:00
2024-10-24 21:02:16 +00:00
return first;
}