2020-10-26 14:07:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-11-23 15:19:47 +00:00
|
|
|
#include <ctype.h>
|
2020-10-26 14:07:14 +00:00
|
|
|
|
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-07 16:02:09 +00:00
|
|
|
|
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-07 16:02:09 +00:00
|
|
|
//if list is empty, then add newcar
|
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;
|
2020-11-07 16:02:09 +00:00
|
|
|
|
|
|
|
//find the end of linked list
|
2020-10-26 14:49:01 +00:00
|
|
|
while(this->next != NULL){
|
|
|
|
this = this->next;
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|
2020-11-07 16:02:09 +00:00
|
|
|
|
|
|
|
//add target in the end
|
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-05 15:07:56 +00:00
|
|
|
printf("%s\n", this->value);
|
2020-11-02 12:16:13 +00:00
|
|
|
}
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 17:59:58 +00:00
|
|
|
void cancel_train(struct car* first) {
|
2020-11-05 22:29:11 +00:00
|
|
|
if(first != NULL){
|
|
|
|
cancel_train(first->next);
|
|
|
|
free(first);
|
2020-11-02 14:15:09 +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-07 16:02:09 +00:00
|
|
|
//if there is only one element in linked list, than check if target and value are the same
|
2020-11-05 15:54:29 +00:00
|
|
|
if(first->next == NULL){
|
|
|
|
int result = strcmp(first->value, target);
|
|
|
|
if(result == 0){
|
|
|
|
free(first);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 16:02:09 +00:00
|
|
|
//check all the elemets in linked list
|
2020-11-05 14:22:56 +00:00
|
|
|
struct car* prev = first;
|
2020-11-05 15:05:45 +00:00
|
|
|
while(prev->next != NULL){
|
2020-11-05 14:27:14 +00:00
|
|
|
int x = strcmp(first->value, target);
|
2020-11-07 16:02:09 +00:00
|
|
|
//if the first element and target are the same
|
2020-11-05 14:22:56 +00:00
|
|
|
if(x == 0){
|
2020-11-05 14:41:59 +00:00
|
|
|
prev = first->next;
|
|
|
|
free(first);
|
|
|
|
return prev;
|
2020-11-05 14:22:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
x = strcmp(prev->next->value, target);
|
2020-11-07 16:02:09 +00:00
|
|
|
//else check all other elements
|
2020-11-05 14:46:53 +00:00
|
|
|
if(x == 0){
|
|
|
|
struct car* third = prev->next->next;
|
|
|
|
free(prev->next);
|
2020-11-05 14:51:07 +00:00
|
|
|
prev->next = third;
|
|
|
|
return first;
|
2020-11-05 14:46:53 +00:00
|
|
|
|
|
|
|
}
|
2020-11-05 14:22:56 +00:00
|
|
|
prev = prev->next;
|
2020-11-05 14:51:07 +00:00
|
|
|
}
|
2020-11-04 21:23:00 +00:00
|
|
|
return first;
|
2020-11-04 21:17:28 +00:00
|
|
|
|
2020-10-26 13:38:06 +00:00
|
|
|
}
|