Přidat du4/a_train.c
This commit is contained in:
parent
21fe2c9e2a
commit
78b02ae136
74
du4/a_train.c
Normal file
74
du4/a_train.c
Normal file
@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "a_train.h"
|
||||
|
||||
struct car* add_car(struct car* first, const char* target) {
|
||||
struct car* new_car = (struct car*)malloc(sizeof(struct car));
|
||||
if (!new_car) {
|
||||
fprintf(stderr, "Chyba: nepodarilo sa alokovat pamat pre novy vozen.\n");
|
||||
return first;
|
||||
}
|
||||
|
||||
strncpy(new_car->value, target, SIZE - 1);
|
||||
new_car->value[SIZE - 1] = '\0';
|
||||
new_car->next = NULL;
|
||||
|
||||
if (first == NULL) {
|
||||
return new_car;
|
||||
}
|
||||
|
||||
struct car* current = first;
|
||||
while (current->next != NULL) {
|
||||
current = current->next;
|
||||
}
|
||||
current->next = new_car;
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
void print_train(struct car* first) {
|
||||
if (first == NULL) {
|
||||
printf("Vlak je prazdny.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
struct car* current = first;
|
||||
while (current != NULL) {
|
||||
printf("%s\n", current->value);
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
void cancel_train(struct car* first) {
|
||||
struct car* current = first;
|
||||
while (current != NULL) {
|
||||
struct car* next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
struct car* clear_train(struct car* first, const char* target) {
|
||||
struct car* current = first;
|
||||
struct car* prev = NULL;
|
||||
|
||||
while (current != NULL) {
|
||||
if (strcmp(current->value, target) == 0) {
|
||||
struct car* to_delete = current;
|
||||
if (prev == NULL) {
|
||||
first = current->next;
|
||||
current = first;
|
||||
} else {
|
||||
prev->next = current->next;
|
||||
current = current->next;
|
||||
}
|
||||
free(to_delete);
|
||||
} else {
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user