2020-10-26 14:07:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-10-26 13:38:06 +00:00
|
|
|
#include "a_train.h"
|
|
|
|
|
|
|
|
struct car* add_car(struct car* first,const char* target) {
|
2020-11-02 14:39:29 +00:00
|
|
|
struct car* newcar = calloc(1, sizeof(struct car));
|
|
|
|
|
2020-11-02 12:13:53 +00:00
|
|
|
if(target == NULL){
|
2020-11-02 14:35:42 +00:00
|
|
|
return 0;
|
2020-11-02 12:13:53 +00:00
|
|
|
}
|
2020-11-02 14:39:29 +00:00
|
|
|
|
2020-10-26 13:38:06 +00:00
|
|
|
if(first == NULL){
|
2020-11-02 14:45:13 +00:00
|
|
|
strcpy(newcar->value, target);
|
2020-11-02 15:47:32 +00:00
|
|
|
newcar->next = NULL;
|
2020-10-26 14:49:01 +00:00
|
|
|
return newcar;
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|
2020-11-02 15:47:32 +00:00
|
|
|
strcpy(newcar->value,target);
|
|
|
|
newcar->next = NULL;
|
2020-10-26 14:49:01 +00:00
|
|
|
struct car *this = first;
|
|
|
|
while(this->next != NULL){
|
|
|
|
this = this->next;
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|
2020-10-26 14:49:01 +00:00
|
|
|
this->next = newcar;
|
2020-10-27 16:37:53 +00:00
|
|
|
return first;
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_train(struct car* first) {
|
2020-11-02 12:17:07 +00:00
|
|
|
for(struct car* this = first; this != NULL; this = this->next){
|
2020-11-02 12:16:13 +00:00
|
|
|
printf("%s\n", this->value);
|
|
|
|
}
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 17:59:58 +00:00
|
|
|
void cancel_train(struct car* first) {
|
2020-11-02 14:10:26 +00:00
|
|
|
if(first == NULL){
|
2020-11-02 16:01:30 +00:00
|
|
|
return;
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|
2020-11-02 14:10:26 +00:00
|
|
|
if(first->next != NULL){
|
|
|
|
cancel_train(first->next);
|
|
|
|
first->next = NULL;
|
2020-11-01 18:41:36 +00:00
|
|
|
}
|
2020-11-02 15:47:32 +00:00
|
|
|
else if(first->next == NULL){
|
2020-11-02 14:15:09 +00:00
|
|
|
free(first);
|
|
|
|
}
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-27 17:18:34 +00:00
|
|
|
|
2020-10-26 13:38:06 +00:00
|
|
|
struct car* clear_train(struct car* first, const char* target) {
|
2020-11-02 15:47:32 +00:00
|
|
|
if(first == NULL){
|
|
|
|
return 0;
|
2020-11-03 09:40:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 15:59:59 +00:00
|
|
|
if(first->next == NULL){
|
2020-11-03 09:41:54 +00:00
|
|
|
int result = strcmp(first->value, target);
|
|
|
|
if(result == 0){
|
2020-11-02 15:59:59 +00:00
|
|
|
free(first);
|
2020-11-03 09:34:41 +00:00
|
|
|
return 0;
|
2020-11-02 15:59:59 +00:00
|
|
|
}
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|
2020-11-03 20:20:50 +00:00
|
|
|
//struct car* this = first;
|
2020-11-03 20:12:51 +00:00
|
|
|
/*if(first->next->next == NULL){
|
2020-11-03 20:00:24 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-11-03 20:12:51 +00:00
|
|
|
} */
|
|
|
|
|
2020-11-02 15:59:59 +00:00
|
|
|
else{
|
2020-11-03 20:20:50 +00:00
|
|
|
struct car* this = first;
|
2020-11-03 19:44:35 +00:00
|
|
|
while(this != NULL){
|
2020-11-03 20:12:51 +00:00
|
|
|
while(this != NULL){
|
2020-11-03 20:23:12 +00:00
|
|
|
int x = strcmp(this->value, target);
|
2020-11-03 20:12:51 +00:00
|
|
|
if(x == 0){
|
2020-11-03 20:31:24 +00:00
|
|
|
first = this->next;
|
2020-11-03 20:32:46 +00:00
|
|
|
free(this);
|
2020-11-03 20:25:57 +00:00
|
|
|
this->next = NULL;
|
|
|
|
//free(this->next);
|
|
|
|
|
2020-11-03 20:31:24 +00:00
|
|
|
return first;
|
2020-11-03 20:12:51 +00:00
|
|
|
}
|
2020-11-03 20:19:48 +00:00
|
|
|
this = this->next;
|
2020-11-03 20:12:51 +00:00
|
|
|
}
|
|
|
|
/*int x = strcmp(this->value, target);
|
2020-11-03 09:46:03 +00:00
|
|
|
if(x == 0){
|
2020-11-03 19:43:01 +00:00
|
|
|
struct car* curr = first;
|
|
|
|
struct car* prev = NULL;
|
|
|
|
if(first == this){
|
|
|
|
first = this->next;
|
|
|
|
free(this);
|
2020-11-03 19:48:12 +00:00
|
|
|
return first;
|
2020-11-03 17:30:23 +00:00
|
|
|
}
|
2020-11-03 19:43:01 +00:00
|
|
|
while(curr != this){
|
|
|
|
prev = curr;
|
|
|
|
curr = curr->next;
|
|
|
|
|
|
|
|
}
|
|
|
|
prev->next = curr->next;
|
2020-11-03 19:48:12 +00:00
|
|
|
free(this);
|
2020-11-02 15:59:59 +00:00
|
|
|
}
|
2020-11-03 20:12:51 +00:00
|
|
|
this = this->next;*/
|
2020-11-02 15:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return first;
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|