96 lines
1.9 KiB
C
96 lines
1.9 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 = (struct car*)malloc(sizeof(struct car));
|
|
strcpy(newcar->value, target);
|
|
newcar->next = NULL;
|
|
if(first == NULL){
|
|
first = newcar;
|
|
return first;
|
|
}else{
|
|
struct car* this = first;
|
|
while(this->next != NULL){
|
|
this = this->next;
|
|
}
|
|
this->next = newcar;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
return first;
|
|
}
|
|
|
|
void print_train(struct car* first) {
|
|
struct car* this = first;
|
|
while(this != NULL){
|
|
printf("%s \n",this->value);
|
|
this = this->next;
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
void cancel_train(struct car* first) {
|
|
/* if (first!=NULL){
|
|
cancel_train(first->next);
|
|
free(first);
|
|
first = NULL;
|
|
}*/
|
|
|
|
struct car* next;
|
|
while (first != NULL) {
|
|
next = first->next;
|
|
free(first);
|
|
first = next;
|
|
}
|
|
first = NULL;
|
|
}
|
|
|
|
|
|
struct car* clear_train(struct car* first, const char* target) {
|
|
if(first == NULL){
|
|
return NULL;
|
|
}
|
|
if(first->next == NULL){
|
|
if(strcmp(first->value,target)== 0){
|
|
return NULL;
|
|
}else{
|
|
return first;
|
|
}
|
|
}
|
|
struct car* prev = first;
|
|
/* if(strcmp(prev->value,target)== 0){
|
|
struct car* one = prev->next;
|
|
free(prev);
|
|
prev= one;
|
|
//free(one);
|
|
//one = NULL;
|
|
return first;
|
|
}*/
|
|
while (prev->next->next != NULL){
|
|
if(strcmp(prev->next->value,target)== 0){
|
|
struct car* third = prev->next->next;
|
|
free(prev->next);
|
|
prev->next = third;
|
|
return first;
|
|
}
|
|
prev = prev->next;
|
|
}
|
|
if(strcmp(prev->next->value,target)== 0){
|
|
|
|
free(prev->next);
|
|
prev->next = NULL;
|
|
return first;
|
|
}
|
|
|
|
|
|
|
|
|
|
return first;
|
|
}
|
|
|