This commit is contained in:
Oleksandr Vyshniakov 2025-11-04 18:33:27 +01:00
parent ff6a322c19
commit 53f048b481
2 changed files with 19 additions and 9 deletions

Binary file not shown.

View File

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