comm
This commit is contained in:
parent
095d681411
commit
37cad72b8c
@ -5,25 +5,25 @@
|
|||||||
|
|
||||||
struct car* add_car(struct car* first,const char* target) {
|
struct car* add_car(struct car* first,const char* target) {
|
||||||
struct car* new = calloc(1, sizeof(struct car));
|
struct car* new = calloc(1, sizeof(struct car));
|
||||||
if(!new) return first;
|
if(!new) return first; // copirovanie cielvej stanice
|
||||||
strcpy(new->value,target);
|
strcpy(new->value,target);
|
||||||
if (first==NULL){
|
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){
|
while(sm->next!=NULL){
|
||||||
sm = sm->next;
|
sm = sm->next;
|
||||||
}
|
}
|
||||||
sm->next = new;
|
sm->next = new; // pridavanie vozna na koniec
|
||||||
return first;
|
return first;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_train(struct car* first) {
|
void print_train(struct car* first) {
|
||||||
struct car* hlp = first;
|
struct car* hlp = first; //pomocna struktura
|
||||||
while(hlp!=NULL){
|
while(hlp!=NULL){ // ak je vozen
|
||||||
printf("Cielova stanica je %s\n",hlp->value);
|
printf("Cielova stanica je %s\n",hlp->value); // vypis cielovych stanic voznov
|
||||||
hlp = hlp->next;
|
hlp = hlp->next;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -32,7 +32,7 @@ void print_train(struct car* first) {
|
|||||||
|
|
||||||
void cancel_train(struct car* first) {
|
void cancel_train(struct car* first) {
|
||||||
if(!first) return;
|
if(!first) return;
|
||||||
cancel_train(first->next);
|
cancel_train(first->next); // recursivna funkcia, ktora vymaze uplne vlak
|
||||||
free(first);
|
free(first);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,17 +42,17 @@ struct car* clear_train(struct car* first, const char* target) {
|
|||||||
if (strcmp(first->value, target)==0){
|
if (strcmp(first->value, target)==0){
|
||||||
struct car* tmp = first->next;
|
struct car* tmp = first->next;
|
||||||
free(first);
|
free(first);
|
||||||
return tmp;
|
return tmp; // vrati to co bolo vymazane ak bol iba jeden vozen
|
||||||
}
|
}
|
||||||
struct car* hlp = first;
|
struct car* hlp = first;
|
||||||
while (hlp->next!=NULL&&strcmp(hlp->next->value,target)!=0){
|
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){
|
if (hlp->next!=NULL&&strcmp(hlp->next->value,target)==0){
|
||||||
struct car* del = hlp->next;
|
struct car* del = hlp->next;
|
||||||
hlp->next=hlp->next->next;
|
hlp->next=hlp->next->next; // spoji predchadzajuci a nasledujuci vozen
|
||||||
free(del);
|
free(del); //vymaze vozen
|
||||||
}
|
}
|
||||||
return first;
|
return first; // link na zaciatok vlaku
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,7 +33,7 @@ void destroy_station(struct station* station){
|
|||||||
int select_track(struct station* station, const char* target){
|
int select_track(struct station* station, const char* target){
|
||||||
unsigned int hash = 0;
|
unsigned int hash = 0;
|
||||||
|
|
||||||
// Jednoduché hashovanie: sčítame všetky znaky
|
// sčítame všetky znaky
|
||||||
for (int i = 0; target[i] != '\0'; i++) {
|
for (int i = 0; target[i] != '\0'; i++) {
|
||||||
hash += (unsigned char)target[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);
|
int track = select_track(station, target);
|
||||||
struct car* current = station->tracks[track];
|
struct car* current = station->tracks[track];
|
||||||
|
|
||||||
// 1. Ak je spojkový zoznam prázdny
|
//Ak je spojkový zoznam prázdny
|
||||||
if (current == NULL) {
|
if (current == NULL) {
|
||||||
struct car* new_car = calloc(1, sizeof(struct car));
|
struct car* new_car = calloc(1, sizeof(struct car));
|
||||||
strcpy(new_car->value, target);
|
strcpy(new_car->value, target);
|
||||||
@ -56,7 +56,7 @@ void add_target_capacity(struct station* station,const char* target, int capacit
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Prejdeme zoznam
|
// Prejdeme zoznam ak existuje vozen z takym istym cielom
|
||||||
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) {
|
||||||
@ -67,7 +67,7 @@ void add_target_capacity(struct station* station,const char* target, int capacit
|
|||||||
current = current->next;
|
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));
|
struct car* new_car = calloc(1, sizeof(struct car));
|
||||||
strcpy(new_car->value, target);
|
strcpy(new_car->value, target);
|
||||||
new_car->capacity = capacity;
|
new_car->capacity = capacity;
|
||||||
@ -93,7 +93,7 @@ int count_targets(struct station* station){
|
|||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
for (int i = 0; i < station->track_count; i++) {
|
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) {
|
while (current != NULL) {
|
||||||
count++;
|
count++;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user