87 lines
1.7 KiB
C
Executable File
87 lines
1.7 KiB
C
Executable File
#include "a_train.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
struct car* add_car(struct car* first,const char* target) {
|
|
struct car* new_car = (struct car*)calloc(1,sizeof(struct car));
|
|
strcpy(new_car->value, target);
|
|
new_car->next = NULL;
|
|
if (first == NULL){
|
|
first = new_car;
|
|
}
|
|
else {
|
|
struct car* temp=first;
|
|
while(temp->next != NULL){
|
|
temp = temp->next;
|
|
}
|
|
temp->next = new_car;
|
|
}
|
|
return first;
|
|
}
|
|
|
|
void print_train(struct car* first) {
|
|
struct car* this = first;
|
|
while(this != NULL){
|
|
printf("%s->",this->value);
|
|
this = this->next;
|
|
}
|
|
|
|
}
|
|
|
|
void cancel_train(struct car* first) {
|
|
if (first != NULL){
|
|
cancel_train(first->next);
|
|
free(first);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
struct car* clear_train(struct car* first, const char* target) {
|
|
struct car* this = first;
|
|
if (this == NULL){
|
|
return NULL;
|
|
}
|
|
if (this->next == NULL){
|
|
if(strcmp(this->value, target) == 0){
|
|
free(this);
|
|
return NULL;
|
|
}else {
|
|
return this;
|
|
}
|
|
}
|
|
if(this->next->next == NULL){
|
|
if(strcmp(this->value, target) == 0){
|
|
struct car* tmp = this->next;
|
|
free(this);
|
|
return tmp;
|
|
}else if(strcmp(this->next->value, target) == 0){
|
|
free(this->next);
|
|
this->next=NULL;
|
|
return this;
|
|
}
|
|
}
|
|
while( this->next->next != NULL){
|
|
if(strcmp(this->next->value, target) == 0){
|
|
struct car* third = this->next->next;
|
|
free(this->next);
|
|
this->next = third;
|
|
return this;
|
|
}
|
|
else {
|
|
this = this->next;
|
|
}
|
|
}
|
|
if(strcmp(this->next->value, target) == 0){
|
|
free(this->next);
|
|
this->next=NULL;
|
|
return this;
|
|
}else if(strcmp(this->next->next->value, target) == 0){
|
|
free(this->next->next);
|
|
this->next=NULL;
|
|
return this;
|
|
}
|
|
return first;
|
|
}
|