usaa25/du4/train.c
2025-11-04 18:28:38 +01:00

53 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "train.h"
struct car* add_car (struct car* first, const char* target) {
struct car* novyj_vagon = (struct car*) malloc(sizeof(struct car)); //создаём новую строку
if (novyj_vagon == NULL) {
printf("Chyba: nepodarilo sa vytvorit novy vozen!\n");
return first;
}
strncpy(novyj_vagon->value, target, SIZE - 1); //Потому что строка в C всегда должна заканчиваться нулём
novyj_vagon->value[SIZE-1] = '/0';
novyj_vagon->next = NULL;
if (first == NULL) {
return novyj_vagon;
}
struct car* actual = first;
while (actual->next !=NULL) {
actual = actual->next;
}
actual->next = novyj_vagon;
return first;
}
void print_train (struct car* first) {
if (first == NULL) {
printf("vlak je prazdny!");
return;
}
struct car* actual = first;
int schet = 1;
while (actual !=NULL) {
printf("%d. [%s]", actual->value);
if (actual->next != NULL) {
printf("->");
}
actual = actual->next;
schet++;
}
printf("\n");
}
int main() {
return 0;
}