Upload files to 'cv5'
This commit is contained in:
parent
e1914e11e5
commit
a850dccbb5
94
cv5/a_train.c
Normal file
94
cv5/a_train.c
Normal file
@ -0,0 +1,94 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "a_train.h"
|
||||
|
||||
|
||||
struct car *add_car(struct car *first, const char *target)
|
||||
{
|
||||
struct car *curr_car = (struct car*) calloc(1, sizeof(struct car));
|
||||
strcpy(curr_car -> value, target);
|
||||
curr_car -> next = NULL;
|
||||
if (first == NULL)
|
||||
{
|
||||
// create list if necessary
|
||||
return curr_car;
|
||||
}
|
||||
// add car to the end
|
||||
struct car *prev_car = first;
|
||||
while (prev_car -> next != NULL)
|
||||
{
|
||||
prev_car = prev_car -> next;
|
||||
}
|
||||
prev_car -> next = curr_car;
|
||||
return first;
|
||||
}
|
||||
|
||||
|
||||
void print_train(struct car *first)
|
||||
{
|
||||
struct car *curr_car = first;
|
||||
while (curr_car != NULL)
|
||||
{
|
||||
printf("%s\n", curr_car -> value);
|
||||
curr_car = curr_car -> next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void cancel_train(struct car *first)
|
||||
{
|
||||
if (first == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
cancel_train(first->next);
|
||||
free(first);
|
||||
}
|
||||
|
||||
|
||||
struct car *clear_train(struct car *first, const char *target)
|
||||
{
|
||||
if (first == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (strcmp(first -> value, target) == 0)
|
||||
{
|
||||
struct car *answer = first -> next;
|
||||
free(first);
|
||||
return answer;
|
||||
}
|
||||
|
||||
struct car *prev_car = first;
|
||||
struct car *curr_car = first -> next;
|
||||
if (prev_car == NULL)
|
||||
{
|
||||
return first;
|
||||
}
|
||||
while ((prev_car -> next) != NULL) {
|
||||
curr_car = prev_car -> next;
|
||||
if(curr_car == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
// if found target
|
||||
if (strcmp(curr_car -> value, target) == 0)
|
||||
{
|
||||
// remove it and continue
|
||||
struct car *next_car = curr_car -> next;
|
||||
free(curr_car);
|
||||
prev_car -> next = next_car;
|
||||
prev_car = next_car;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev_car = curr_car;
|
||||
}
|
||||
if (prev_car == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
Loading…
Reference in New Issue
Block a user