usaa24/cv4/a_train.c

100 lines
1.6 KiB
C
Raw 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
struct car* add_car(struct car* first,const char* target)
{
2024-10-23 19:55:03 +00:00
struct car* ret = first;
2024-10-23 19:35:27 +00:00
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;
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-23 19:47:05 +00:00
if(first){
printf("->");
}
}while(first!=NULL);
2024-10-23 19:35:27 +00:00
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)
{
struct car* t; //змінна для збереження кого видаляти
2024-10-16 08:23:13 +00:00
2024-10-23 19:35:27 +00:00
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;
}
}
//голова не таргет
do
{
if(strcmp(first->next->value, target)==0)
{
t=first;
first=first->next;
free(t);
}
else
{
first=first->next;
}
}
while(first);
return first;
2024-10-16 08:23:13 +00:00
}
2024-10-23 19:37:45 +00:00
2024-10-23 19:35:27 +00:00