This commit is contained in:
Nataliia Kobryn 2025-12-01 12:15:34 +01:00
parent 095d681411
commit 37cad72b8c
2 changed files with 18 additions and 18 deletions

View File

@ -5,25 +5,25 @@
struct car* add_car(struct car* first,const char* target) {
struct car* new = calloc(1, sizeof(struct car));
if(!new) return first;
if(!new) return first; // copirovanie cielvej stanice
strcpy(new->value,target);
if (first==NULL){
return new;
return new; // ak nebolo first, new bude prvy
}
struct car* sm = first;
struct car* sm = first;
while(sm->next!=NULL){
sm = sm->next;
}
sm->next = new;
sm->next = new; // pridavanie vozna na koniec
return first;
}
void print_train(struct car* first) {
struct car* hlp = first;
while(hlp!=NULL){
printf("Cielova stanica je %s\n",hlp->value);
struct car* hlp = first; //pomocna struktura
while(hlp!=NULL){ // ak je vozen
printf("Cielova stanica je %s\n",hlp->value); // vypis cielovych stanic voznov
hlp = hlp->next;
}
@ -32,7 +32,7 @@ void print_train(struct car* first) {
void cancel_train(struct car* first) {
if(!first) return;
cancel_train(first->next);
cancel_train(first->next); // recursivna funkcia, ktora vymaze uplne vlak
free(first);
}
@ -42,17 +42,17 @@ struct car* clear_train(struct car* first, const char* target) {
if (strcmp(first->value, target)==0){
struct car* tmp = first->next;
free(first);
return tmp;
return tmp; // vrati to co bolo vymazane ak bol iba jeden vozen
}
struct car* hlp = first;
while (hlp->next!=NULL&&strcmp(hlp->next->value,target)!=0){
hlp = hlp->next;
hlp = hlp->next; //hlada vozen
}
if (hlp->next!=NULL&&strcmp(hlp->next->value,target)==0){
struct car* del = hlp->next;
hlp->next=hlp->next->next;
free(del);
hlp->next=hlp->next->next; // spoji predchadzajuci a nasledujuci vozen
free(del); //vymaze vozen
}
return first;
return first; // link na zaciatok vlaku
}

View File

@ -33,7 +33,7 @@ void destroy_station(struct station* station){
int select_track(struct station* station, const char* target){
unsigned int hash = 0;
// Jednoduché hashovanie: sčítame všetky znaky
// sčítame všetky znaky
for (int i = 0; target[i] != '\0'; i++) {
hash += (unsigned char)target[i];
}
@ -46,7 +46,7 @@ void add_target_capacity(struct station* station,const char* target, int capacit
int track = select_track(station, target);
struct car* current = station->tracks[track];
// 1. Ak je spojkový zoznam prázdny
//Ak je spojkový zoznam prázdny
if (current == NULL) {
struct car* new_car = calloc(1, sizeof(struct car));
strcpy(new_car->value, target);
@ -56,7 +56,7 @@ void add_target_capacity(struct station* station,const char* target, int capacit
return;
}
// 2. Prejdeme zoznam
// Prejdeme zoznam ak existuje vozen z takym istym cielom
struct car* prev = NULL;
while (current != NULL) {
if (strcmp(current->value, target) == 0) {
@ -67,7 +67,7 @@ void add_target_capacity(struct station* station,const char* target, int capacit
current = current->next;
}
// 3. Ak záznam neexistuje → pridáme nový na koniec
// Ak záznam neexistuje → pridáme nový na koniec
struct car* new_car = calloc(1, sizeof(struct car));
strcpy(new_car->value, target);
new_car->capacity = capacity;
@ -93,7 +93,7 @@ int count_targets(struct station* station){
int count = 0;
for (int i = 0; i < station->track_count; i++) {
struct car* current = station->tracks[i];
struct car* current = station->tracks[i]; // scitavame vsetky mozne koniecne stanice
while (current != NULL) {
count++;
current = current->next;