usaa19cv7/a_train.c

96 lines
1.9 KiB
C
Raw Permalink Normal View History

2019-11-04 09:36:45 +00:00
#include "a_train.h"
#include <stdio.h>
2019-11-30 19:28:04 +00:00
#include <stdlib.h>
#include <string.h>
2019-11-04 09:36:45 +00:00
struct car* add_car(struct car* first,const char* target) {
2019-11-30 19:28:04 +00:00
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;
2019-11-04 09:36:45 +00:00
}
void print_train(struct car* first) {
2019-11-30 19:28:04 +00:00
struct car* this = first;
while(this != NULL){
printf("%s \n",this->value);
this = this->next;
}
printf("\n");
2019-11-04 09:36:45 +00:00
}
void cancel_train(struct car* first) {
2019-11-30 19:28:04 +00:00
/* 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;
2019-11-04 09:36:45 +00:00
}
struct car* clear_train(struct car* first, const char* target) {
2019-11-30 19:28:04 +00:00
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;
2019-11-04 09:36:45 +00:00
}