dost
This commit is contained in:
parent
db9350231b
commit
75f2fc12de
@ -3,38 +3,26 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "a_train.h"
|
#include "a_train.h"
|
||||||
|
|
||||||
/**
|
|
||||||
* Prida vozen na koniec vlaku.
|
|
||||||
*/
|
|
||||||
struct car* add_car(struct car* first, const char* target) {
|
struct car* add_car(struct car* first, const char* target) {
|
||||||
// vytvorenie noveho vozna
|
|
||||||
struct car* newcar = calloc(1, sizeof(struct car));
|
struct car* newcar = calloc(1, sizeof(struct car));
|
||||||
if (newcar == NULL) {
|
if (newcar == NULL) {
|
||||||
return first; // ak zlyha alokacia, vrat povodny zoznam
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(newcar->value, target);
|
strcpy(newcar->value, target);
|
||||||
newcar->next = NULL;
|
newcar->next = NULL;
|
||||||
|
|
||||||
// ak je vlak prazdny
|
|
||||||
if (first == NULL) {
|
if (first == NULL) {
|
||||||
return newcar;
|
return newcar;
|
||||||
}
|
}
|
||||||
|
|
||||||
// najdi posledny vozen
|
|
||||||
struct car* this = first;
|
struct car* this = first;
|
||||||
while (this->next != NULL) {
|
while (this->next != NULL) {
|
||||||
this = this->next;
|
this = this->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
// pripoj novy vozen
|
|
||||||
this->next = newcar;
|
this->next = newcar;
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Vypise vsetky vozne vo vlaku.
|
|
||||||
*/
|
|
||||||
void print_train(struct car* first) {
|
void print_train(struct car* first) {
|
||||||
struct car* this = first;
|
struct car* this = first;
|
||||||
while (this != NULL) {
|
while (this != NULL) {
|
||||||
@ -42,40 +30,26 @@ void print_train(struct car* first) {
|
|||||||
this = this->next;
|
this = this->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Zrusenie vsetkych voznov vo vlaku.
|
|
||||||
*/
|
|
||||||
void cancel_train(struct car* first) {
|
void cancel_train(struct car* first) {
|
||||||
if (first == NULL) return;
|
if (first == NULL) return;
|
||||||
|
|
||||||
// rekurzivne uvolnenie
|
|
||||||
cancel_train(first->next);
|
cancel_train(first->next);
|
||||||
free(first);
|
free(first);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Vyradenie vsetkych voznov, ktorych cielova stanica je target.
|
|
||||||
*/
|
|
||||||
struct car* clear_train(struct car* first, const char* target) {
|
struct car* clear_train(struct car* first, const char* target) {
|
||||||
struct car* current = first;
|
struct car* current = first;
|
||||||
struct car* prev = NULL;
|
struct car* prev = NULL;
|
||||||
|
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
if (strcmp(current->value, target) == 0) {
|
if (strcmp(current->value, target) == 0) {
|
||||||
// treba zmazat tento vozen
|
|
||||||
struct car* to_delete = current;
|
struct car* to_delete = current;
|
||||||
if (prev == NULL) {
|
if (prev == NULL) {
|
||||||
// mazeme prvy prvok
|
|
||||||
first = current->next;
|
first = current->next;
|
||||||
} else {
|
} else {
|
||||||
// mazeme stredny alebo posledny prvok
|
|
||||||
prev->next = current->next;
|
prev->next = current->next;
|
||||||
}
|
}
|
||||||
current = current->next;
|
current = current->next;
|
||||||
free(to_delete);
|
free(to_delete);
|
||||||
} else {
|
} else {
|
||||||
// presun dalej
|
|
||||||
prev = current;
|
prev = current;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user