118 lines
2.8 KiB
C
118 lines
2.8 KiB
C
#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* newcar = calloc(1, sizeof(struct car));
|
|
|
|
if(target == NULL){
|
|
return 0;
|
|
}
|
|
|
|
if(first == NULL){
|
|
strcpy(newcar->value, target);
|
|
newcar->next = NULL;
|
|
return newcar;
|
|
}
|
|
strcpy(newcar->value,target);
|
|
newcar->next = NULL;
|
|
struct car *this = first;
|
|
while(this->next != NULL){
|
|
this = this->next;
|
|
}
|
|
this->next = newcar;
|
|
return first;
|
|
}
|
|
|
|
void print_train(struct car* first) {
|
|
for(struct car* this = first; this != NULL; this = this->next){
|
|
printf("%s\n", this->value);
|
|
}
|
|
}
|
|
|
|
void cancel_train(struct car* first) {
|
|
if(first == NULL){
|
|
return;
|
|
}
|
|
if(first->next != NULL){
|
|
cancel_train(first->next);
|
|
first->next = NULL;
|
|
}
|
|
else if(first->next == NULL){
|
|
free(first);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
struct car* clear_train(struct car* first, const char* target) {
|
|
if(first == NULL){
|
|
return 0;
|
|
}
|
|
|
|
if(first->next == NULL){
|
|
int result = strcmp(first->value, target);
|
|
if(result == 0){
|
|
free(first);
|
|
return 0;
|
|
}
|
|
}
|
|
//struct car* this = first;
|
|
/*if(first->next->next == NULL){
|
|
int x = strcmp(this->next->value, target);
|
|
if(x == 0){
|
|
struct car* secondlast = first;
|
|
while(this->next != NULL){
|
|
secondlast = this;
|
|
this = this->next;
|
|
}
|
|
if(this == first){
|
|
first = NULL;
|
|
}
|
|
else{
|
|
secondlast->next = NULL;
|
|
}
|
|
free(this);
|
|
}
|
|
} */
|
|
|
|
else{
|
|
struct car* this = first;
|
|
while(this != NULL){
|
|
//while(this != NULL){
|
|
int x = strcmp(this->value, target);
|
|
if(x == 0){
|
|
first = this->next;
|
|
free(this);
|
|
this->next = NULL;
|
|
//free(this->next);
|
|
|
|
return first;
|
|
}
|
|
this = this->next;
|
|
//}
|
|
/*int x = strcmp(this->value, target);
|
|
if(x == 0){
|
|
struct car* curr = first;
|
|
struct car* prev = NULL;
|
|
if(first == this){
|
|
first = this->next;
|
|
free(this);
|
|
return first;
|
|
}
|
|
while(curr != this){
|
|
prev = curr;
|
|
curr = curr->next;
|
|
|
|
}
|
|
prev->next = curr->next;
|
|
free(this);
|
|
}
|
|
this = this->next;*/
|
|
}
|
|
}
|
|
return first;
|
|
}
|