1
This commit is contained in:
parent
b9d90d148d
commit
2a4ead8c91
@ -1,79 +1,57 @@
|
|||||||
#include "a_train.h"
|
#include "a_train.h"
|
||||||
|
|
||||||
struct car* add_car(struct car* first, const char* target) {
|
struct car* add_car(struct car* first, const char* target) {
|
||||||
// Vytvorenie nového vozňa
|
struct car* newcar = malloc(sizeof(struct car));
|
||||||
struct car* newcar = calloc(1, sizeof(struct car));
|
|
||||||
if (!newcar) return first;
|
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
|
if (first == NULL) {
|
||||||
first = (first == NULL) ? newcar : first;
|
return newcar;
|
||||||
|
}
|
||||||
|
|
||||||
// Ak bol prázdny, máme hotovo
|
struct car* last = first;
|
||||||
if (first == newcar)
|
while (last->next)
|
||||||
return first;
|
last = last->next;
|
||||||
|
|
||||||
// Nájdeme koniec zoznamu
|
last->next = newcar;
|
||||||
struct car* this = first;
|
|
||||||
while (this->next != NULL)
|
|
||||||
this = this->next;
|
|
||||||
|
|
||||||
this->next = newcar;
|
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_train(struct car* first) {
|
void print_train(const struct car* first) {
|
||||||
// ZMENA: jednoduchší zápis cyklu
|
for (const struct car* c = first; c; c = c->next)
|
||||||
for (struct car* c = first; c != NULL; c = c->next) {
|
|
||||||
printf("%s\n", c->value);
|
printf("%s\n", c->value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cancel_train(struct car* first) {
|
void cancel_train(struct car* first) {
|
||||||
while (first) {
|
while (first) {
|
||||||
struct car* next = first->next;
|
struct car* tmp = first->next;
|
||||||
first->next = NULL; // ochrana proti cyklom alebo neplatnému next
|
|
||||||
free(first);
|
free(first);
|
||||||
first = next;
|
first = tmp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct car* clear_train(struct car* first, const char* target) {
|
||||||
struct car* clear_train(struct car* first, const char* name) {
|
// Odstráni vozne zo začiatku
|
||||||
|
while (first && strcmp(first->value, target) == 0) {
|
||||||
// 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* tmp = first->next;
|
struct car* tmp = first->next;
|
||||||
free(first);
|
free(first);
|
||||||
first = tmp;
|
first = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ak sa všetko odstránilo
|
if (!first) return NULL;
|
||||||
if (first == NULL) return NULL;
|
|
||||||
|
|
||||||
struct car* prev = first;
|
struct car* prev = first;
|
||||||
|
|
||||||
// ZMENA: prepis vnútornej logiky bez zmeny výsledku
|
|
||||||
while (prev->next) {
|
while (prev->next) {
|
||||||
|
if (strcmp(prev->next->value, target) == 0) {
|
||||||
// Ak sa má prvok vymazať
|
struct car* del = prev->next;
|
||||||
if (strcmp(prev->next->value, name) == 0) {
|
prev->next = del->next;
|
||||||
|
free(del);
|
||||||
struct car* to_delete = prev->next;
|
|
||||||
|
|
||||||
// Preskočíme uzol a uvoľníme ho
|
|
||||||
prev->next = to_delete->next;
|
|
||||||
free(to_delete);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// ZMENA: iný štýl zápisu inkrementácie
|
|
||||||
prev = prev->next;
|
prev = prev->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,28 +1,28 @@
|
|||||||
#ifndef A_TRAIN_H
|
#ifndef TRAIN_H
|
||||||
#define A_TRAIN_H
|
#define TRAIN_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.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 {
|
struct car {
|
||||||
char value[SIZE]; // Cieľová stanica
|
char value[SIZE]; // Názov cieľovej stanice
|
||||||
struct car* next; // Ukazovateľ na ďalší vozeň (NULL = koniec zoznamu)
|
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);
|
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);
|
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"
|
#include "a_train.h"
|
||||||
|
|
||||||
int main() {
|
int main(void) {
|
||||||
struct car* train = NULL; // Начало списка
|
struct car* train = NULL;
|
||||||
char buffer[SIZE];
|
char buffer[SIZE];
|
||||||
char numbuf[SIZE];
|
char numbuf[SIZE];
|
||||||
int passengers;
|
|
||||||
|
|
||||||
printf("Zadajte zoznam cieľových staníc a počet cestujúcich.\n");
|
printf("Zadajte zoznam cieľových staníc a počet cestujúcich.\n");
|
||||||
printf("Zoznam zakončite prázdnym riadkom.\n");
|
printf("Zoznam zakončite prázdnym riadkom.\n");
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// Načítame názov stanice
|
// Názov stanice
|
||||||
if (!fgets(buffer, SIZE, stdin)) break;
|
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'
|
// Počet cestujúcich (iba pre čítanie, neukladá sa)
|
||||||
|
|
||||||
// Načítame počet cestujúcich
|
|
||||||
if (!fgets(numbuf, SIZE, stdin)) break;
|
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);
|
train = add_car(train, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zistenie stanice, ktorú treba odstrániť
|
|
||||||
printf("\nZadajte stanicu, ktorá sa má vyradiť:\n");
|
printf("\nZadajte stanicu, ktorá sa má vyradiť:\n");
|
||||||
if (fgets(buffer, SIZE, stdin)) {
|
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);
|
print_train(train);
|
||||||
|
|
||||||
// Uvoľnenie pamäte
|
|
||||||
cancel_train(train);
|
cancel_train(train);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user