2024-10-24 16:53:07 +00:00
|
|
|
#include "a_train.h"
|
|
|
|
#include <stdio.h>
|
2024-10-24 17:05:08 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-10-24 16:53:07 +00:00
|
|
|
|
2024-10-24 17:05:08 +00:00
|
|
|
struct car* add_car(struct car* first, const char* target) {
|
|
|
|
struct car* new_car = (struct car*)malloc(sizeof(struct car));
|
|
|
|
if (new_car == NULL) {
|
|
|
|
printf("Chyba: Nepodarilo sa alokovať pamäť pre nový vozeň.\n");
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
strncpy(new_car->value, target, SIZE);
|
|
|
|
new_car->next = NULL;
|
2024-10-24 17:14:24 +00:00
|
|
|
|
2024-10-24 17:05:08 +00:00
|
|
|
if (first == NULL) {
|
|
|
|
return new_car;
|
|
|
|
}
|
2024-10-24 17:14:24 +00:00
|
|
|
|
2024-10-24 17:05:08 +00:00
|
|
|
struct car* temp = first;
|
|
|
|
while (temp->next != NULL) {
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
temp->next = new_car;
|
2024-10-24 17:14:24 +00:00
|
|
|
|
2024-10-24 17:05:08 +00:00
|
|
|
return first;
|
2024-10-24 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_train(struct car* first) {
|
2024-10-24 17:10:30 +00:00
|
|
|
if (first == NULL) {
|
|
|
|
printf("Vlak je prazdny.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct car* temp = first;
|
|
|
|
while (temp != NULL) {
|
|
|
|
printf("Cielova stanica: %s\n", temp->value);
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
2024-10-24 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cancel_train(struct car* first) {
|
2024-10-24 17:26:06 +00:00
|
|
|
struct car* temp;
|
|
|
|
while (first != NULL) {
|
|
|
|
temp = first;
|
|
|
|
first = first->next;
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
printf("Vsetky vozne boli zrusene.\n");
|
2024-10-24 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct car* clear_train(struct car* first, const char* target) {
|
2024-10-24 17:26:06 +00:00
|
|
|
struct car* current = first;
|
|
|
|
struct car* previous = NULL;
|
|
|
|
|
|
|
|
while (current != NULL) {
|
|
|
|
// Porovnanie hodnoty cielovej stanice
|
|
|
|
if (strcmp(current->value, target) == 0) {
|
|
|
|
// Ak sa cielova stanica zhoduje, odstráni vozeň
|
|
|
|
if (previous == NULL) {
|
|
|
|
// Ak je to prvý vozeň
|
|
|
|
first = current->next;
|
|
|
|
} else {
|
|
|
|
previous->next = current->next;
|
|
|
|
}
|
|
|
|
struct car* temp = current;
|
|
|
|
current = current->next;
|
|
|
|
free(temp);
|
|
|
|
} else {
|
|
|
|
// Ak sa nezhoduje, prejde na nasledujúci vozeň
|
|
|
|
previous = current;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return first;
|
2024-10-24 16:53:07 +00:00
|
|
|
}
|