This commit is contained in:
Džubara 2024-10-24 19:14:24 +02:00
parent 30c39258d5
commit ed44d7bd3c
2 changed files with 11 additions and 39 deletions

View File

@ -4,30 +4,24 @@
#include <string.h> #include <string.h>
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* new_car = (struct car*)malloc(sizeof(struct car)); struct car* new_car = (struct car*)malloc(sizeof(struct car));
if (new_car == NULL) { if (new_car == NULL) {
printf("Chyba: Nepodarilo sa alokovať pamäť pre nový vozeň.\n"); printf("Chyba: Nepodarilo sa alokovať pamäť pre nový vozeň.\n");
return first; return first;
} }
// Inicializacia hodnot
strncpy(new_car->value, target, SIZE); strncpy(new_car->value, target, SIZE);
new_car->next = NULL; new_car->next = NULL;
// Ak vlak neexistuje, novy vozen sa stane prvym
if (first == NULL) { if (first == NULL) {
return new_car; return new_car;
} }
// Najdenie posledneho vozna
struct car* temp = first; struct car* temp = first;
while (temp->next != NULL) { while (temp->next != NULL) {
temp = temp->next; temp = temp->next;
} }
// Pridanie noveho vozna na koniec
temp->next = new_car; temp->next = new_car;
return first; return first;
} }

View File

@ -1,9 +1,8 @@
#include <string.h>
#include <stdio.h>
#ifndef TRAIN_H #ifndef TRAIN_H
#define TRAIN_H #define TRAIN_H
#define SIZE 100 #define SIZE 100
#include <stdlib.h>
/** /**
* Jeden vozen vlaku * Jeden vozen vlaku
*/ */
@ -18,34 +17,13 @@ struct car {
struct car* next; struct car* next;
}; };
/**
struct car* add_car(struct car* first, const char* target) { * Prida vozen na koniec vlaku.
// Vytvorenie noveho vozna *
struct car* new_car = (struct car*)malloc(sizeof(struct car)); * @arg nazov cielovej stanice, ktory sa ma priradit novemu voznu.
if (new_car == NULL) { * @return smernik na zaciatok vlaku.
printf("Chyba: Nepodarilo sa alokovať pamäť pre nový vozeň.\n"); */
return first; struct car* add_car(struct car* first,const char* target);
}
// Inicializacia hodnot
strncpy(new_car->value, target, SIZE);
new_car->next = NULL;
// Ak vlak neexistuje, novy vozen sa stane prvym
if (first == NULL) {
return new_car;
}
// Najdenie posledneho vozna
struct car* temp = first;
while (temp->next != NULL) {
temp = temp->next;
}
// Pridanie noveho vozna na koniec
temp->next = new_car;
return first;
}
/** /**