Изменил(а) на 'cv4/a_train.c'
This commit is contained in:
parent
ac53ed8b42
commit
4859bc0ecc
@ -7,7 +7,7 @@ struct car* add_car(struct car* first, const char* target){
|
|||||||
if(!strcmp(target, ""))
|
if(!strcmp(target, ""))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if(!first){
|
if(!first || !strcmp(first->value, "")){
|
||||||
first = (struct car*)calloc(1, sizeof(struct car));
|
first = (struct car*)calloc(1, sizeof(struct car));
|
||||||
strcpy(first->value, target);
|
strcpy(first->value, target);
|
||||||
first->next = NULL;
|
first->next = NULL;
|
||||||
@ -16,12 +16,12 @@ struct car* add_car(struct car* first, const char* target){
|
|||||||
|
|
||||||
struct car* temp = first;
|
struct car* temp = first;
|
||||||
|
|
||||||
while(temp->next != NULL)
|
while(temp != NULL)
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
|
|
||||||
temp->next = (struct car*)calloc(1, sizeof(struct car));
|
temp = (struct car*)calloc(1, sizeof(struct car));
|
||||||
strcpy(temp->next->value, target);
|
strcpy(temp->value, target);
|
||||||
temp->next->next = NULL;
|
temp->next = NULL;
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,8 +59,14 @@ struct car* clear_train(struct car* first, const char* target){
|
|||||||
|
|
||||||
while(temp != NULL){
|
while(temp != NULL){
|
||||||
next = temp->next;
|
next = temp->next;
|
||||||
if(!strcmp(temp->value, target))
|
if(!strcmp(temp->value, target)) {
|
||||||
|
if(temp == first) {
|
||||||
|
free(first);
|
||||||
|
first = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
free(temp);
|
free(temp);
|
||||||
|
}
|
||||||
temp = next;
|
temp = next;
|
||||||
if(!first)
|
if(!first)
|
||||||
first = temp;
|
first = temp;
|
||||||
@ -68,3 +74,12 @@ struct car* clear_train(struct car* first, const char* target){
|
|||||||
|
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
struct car* myCar = (struct car*)calloc(1, sizeof(struct car));
|
||||||
|
myCar = add_car(myCar, "Kosice");
|
||||||
|
print_train(myCar);
|
||||||
|
myCar = clear_train(myCar, "Kosice");
|
||||||
|
print_train(myCar);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user