1
This commit is contained in:
parent
b9d90d148d
commit
2a4ead8c91
@ -1,79 +1,57 @@
|
||||
#include "a_train.h"
|
||||
|
||||
struct car* add_car(struct car* first, const char* target) {
|
||||
// Vytvorenie nového vozňa
|
||||
struct car* newcar = calloc(1, sizeof(struct car));
|
||||
struct car* newcar = malloc(sizeof(struct car));
|
||||
if (!newcar) return first;
|
||||
|
||||
strcpy(newcar->value, target);
|
||||
strncpy(newcar->value, target, SIZE - 1);
|
||||
newcar->value[SIZE - 1] = '\0';
|
||||
newcar->next = NULL;
|
||||
|
||||
// Ak bol zoznam prázdny — použijeme ternárny operator
|
||||
first = (first == NULL) ? newcar : first;
|
||||
if (first == NULL) {
|
||||
return newcar;
|
||||
}
|
||||
|
||||
// Ak bol prázdny, máme hotovo
|
||||
if (first == newcar)
|
||||
return first;
|
||||
struct car* last = first;
|
||||
while (last->next)
|
||||
last = last->next;
|
||||
|
||||
// Nájdeme koniec zoznamu
|
||||
struct car* this = first;
|
||||
while (this->next != NULL)
|
||||
this = this->next;
|
||||
|
||||
this->next = newcar;
|
||||
last->next = newcar;
|
||||
return first;
|
||||
}
|
||||
|
||||
void print_train(struct car* first) {
|
||||
// ZMENA: jednoduchší zápis cyklu
|
||||
for (struct car* c = first; c != NULL; c = c->next) {
|
||||
void print_train(const struct car* first) {
|
||||
for (const struct car* c = first; c; c = c->next)
|
||||
printf("%s\n", c->value);
|
||||
}
|
||||
}
|
||||
|
||||
void cancel_train(struct car* first) {
|
||||
while (first) {
|
||||
struct car* next = first->next;
|
||||
first->next = NULL; // ochrana proti cyklom alebo neplatnému next
|
||||
struct car* tmp = first->next;
|
||||
free(first);
|
||||
first = next;
|
||||
first = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct car* clear_train(struct car* first, const char* name) {
|
||||
|
||||
// Ak je zoznam prázdny
|
||||
if (first == NULL) return NULL;
|
||||
|
||||
// Odstraňovanie prvkov zo začiatku
|
||||
while (first && strcmp(first->value, name) == 0) {
|
||||
struct car* clear_train(struct car* first, const char* target) {
|
||||
// Odstráni vozne zo začiatku
|
||||
while (first && strcmp(first->value, target) == 0) {
|
||||
struct car* tmp = first->next;
|
||||
free(first);
|
||||
first = tmp;
|
||||
}
|
||||
|
||||
// Ak sa všetko odstránilo
|
||||
if (first == NULL) return NULL;
|
||||
if (!first) return NULL;
|
||||
|
||||
struct car* prev = first;
|
||||
|
||||
// ZMENA: prepis vnútornej logiky bez zmeny výsledku
|
||||
while (prev->next) {
|
||||
|
||||
// Ak sa má prvok vymazať
|
||||
if (strcmp(prev->next->value, name) == 0) {
|
||||
|
||||
struct car* to_delete = prev->next;
|
||||
|
||||
// Preskočíme uzol a uvoľníme ho
|
||||
prev->next = to_delete->next;
|
||||
free(to_delete);
|
||||
|
||||
if (strcmp(prev->next->value, target) == 0) {
|
||||
struct car* del = prev->next;
|
||||
prev->next = del->next;
|
||||
free(del);
|
||||
} else {
|
||||
// ZMENA: iný štýl zápisu inkrementácie
|
||||
prev = prev->next;
|
||||
}
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
@ -1,28 +1,28 @@
|
||||
#ifndef A_TRAIN_H
|
||||
#define A_TRAIN_H
|
||||
#ifndef TRAIN_H
|
||||
#define TRAIN_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SIZE 128 // Maximálna dĺžka názvu stanice
|
||||
#define SIZE 100
|
||||
|
||||
|
||||
// Štruktúra jedného vozňa v spojkovom zozname
|
||||
struct car {
|
||||
char value[SIZE]; // Cieľová stanica
|
||||
struct car* next; // Ukazovateľ na ďalší vozeň (NULL = koniec zoznamu)
|
||||
char value[SIZE]; // Názov cieľovej stanice
|
||||
struct car* next; // Smerník na ďalší vozeň
|
||||
};
|
||||
|
||||
// Pridá nový vozeň na koniec spojkového zoznamu
|
||||
|
||||
struct car* add_car(struct car* first, const char* target);
|
||||
|
||||
// Vypíše všetky vozne v poradí
|
||||
void print_train(struct car* first);
|
||||
|
||||
// Uvoľní všetku dynamickú pamäť spojkového zoznamu
|
||||
void print_train(const struct car* first);
|
||||
|
||||
|
||||
void cancel_train(struct car* first);
|
||||
|
||||
// Odstráni všetky vozne s daným názvom stanice
|
||||
struct car* clear_train(struct car* first, const char* name);
|
||||
|
||||
#endif
|
||||
struct car* clear_train(struct car* first, const char* target);
|
||||
|
||||
#endif // TRAIN_H
|
||||
|
||||
31
du4/main.c
31
du4/main.c
@ -1,45 +1,34 @@
|
||||
#include "a_train.h"
|
||||
|
||||
int main() {
|
||||
struct car* train = NULL; // Начало списка
|
||||
int main(void) {
|
||||
struct car* train = NULL;
|
||||
char buffer[SIZE];
|
||||
char numbuf[SIZE];
|
||||
int passengers;
|
||||
|
||||
printf("Zadajte zoznam cieľových staníc a počet cestujúcich.\n");
|
||||
printf("Zoznam zakončite prázdnym riadkom.\n");
|
||||
|
||||
while (1) {
|
||||
// Načítame názov stanice
|
||||
// Názov stanice
|
||||
if (!fgets(buffer, SIZE, stdin)) break;
|
||||
if (buffer[0] == '\n') break; // prázdny riadok → koniec
|
||||
if (buffer[0] == '\n') break;
|
||||
buffer[strcspn(buffer, "\n")] = '\0';
|
||||
|
||||
buffer[strcspn(buffer, "\n")] = 0; // odstránenie '\n'
|
||||
|
||||
// Načítame počet cestujúcich
|
||||
// Počet cestujúcich (iba pre čítanie, neukladá sa)
|
||||
if (!fgets(numbuf, SIZE, stdin)) break;
|
||||
passengers = atoi(numbuf); // používame atoi (údaj sa aj tak nepoužíva)
|
||||
|
||||
// Pridáme vozeň
|
||||
// Pridáme vozeň do zoznamu
|
||||
train = add_car(train, buffer);
|
||||
}
|
||||
|
||||
// Zistenie stanice, ktorú treba odstrániť
|
||||
printf("\nZadajte stanicu, ktorá sa má vyradiť:\n");
|
||||
if (fgets(buffer, SIZE, stdin)) {
|
||||
buffer[strcspn(buffer, "\n")] = 0;
|
||||
buffer[strcspn(buffer, "\n")] = '\0';
|
||||
train = clear_train(train, buffer);
|
||||
printf("\nVýsledný vlak bez stanice %s bude:\n", buffer);
|
||||
}
|
||||
|
||||
// Odstránenie
|
||||
train = clear_train(train, buffer);
|
||||
|
||||
// Výsledok
|
||||
printf("\nVýsledný vlak bez stanice %s bude:\n", buffer);
|
||||
print_train(train);
|
||||
|
||||
// Uvoľnenie pamäte
|
||||
cancel_train(train);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user