du4
This commit is contained in:
parent
1f9493b17e
commit
07240659e8
24
du4/Makefile
Normal file
24
du4/Makefile
Normal file
@ -0,0 +1,24 @@
|
||||
TARGET = train
|
||||
|
||||
CC = gcc
|
||||
|
||||
CFLAGS = -Wall -Wextra -std=c11 -g
|
||||
|
||||
OBJS = main.o a_train.o
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS)
|
||||
|
||||
main.o: main.c a_train.h
|
||||
$(CC) $(CFLAGS) -c main.c
|
||||
|
||||
a_train.o: a_train.c a_train.h
|
||||
$(CC) $(CFLAGS) -c a_train.c
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
|
||||
run: $(TARGET)
|
||||
./$(TARGET)
|
||||
79
du4/a_train.c
Normal file
79
du4/a_train.c
Normal file
@ -0,0 +1,79 @@
|
||||
#include "a_train.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
//pridaj vozen na koniec spojkového zoznamu
|
||||
struct car* add_car(struct car* first, const char* target) {
|
||||
// alokacia noveho prvku
|
||||
struct car* newcar = calloc(1, sizeof(struct car));
|
||||
if (newcar == NULL) {
|
||||
fprintf(stderr, "Chyba: nepodarilo sa alokovať pamäť pre nový vozeň.\n");
|
||||
return first;
|
||||
}
|
||||
|
||||
// kopirujeme cielovu stanicu
|
||||
strcpy(newcar->value, target);
|
||||
newcar->next = NULL;
|
||||
|
||||
// vlak prazdny -> novy vozen je prvy
|
||||
if (first == NULL) {
|
||||
return newcar;
|
||||
}
|
||||
|
||||
// inak pridame na koniec
|
||||
struct car* this = first;
|
||||
while (this->next != NULL) {
|
||||
this = this->next;
|
||||
}
|
||||
this->next = newcar;
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
// vypis voznov
|
||||
void print_train(struct car* first) {
|
||||
struct car* this = first;
|
||||
while (this != NULL) {
|
||||
printf("%s\n", this->value);
|
||||
this = this->next;
|
||||
}
|
||||
}
|
||||
|
||||
// zrusit vlak a uvolnit pamat
|
||||
void cancel_train(struct car* first) {
|
||||
if (first == NULL) return;
|
||||
|
||||
// Rekurzívne uvoľnenie
|
||||
cancel_train(first->next);
|
||||
free(first);
|
||||
}
|
||||
|
||||
// odstranit vozne s target stanicou
|
||||
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) {
|
||||
// vymazat zaznam
|
||||
struct car* to_delete = current;
|
||||
current = current->next;
|
||||
|
||||
if (prev == NULL) {
|
||||
// vymazat prvy prvok
|
||||
first = current;
|
||||
} else {
|
||||
prev->next = current;
|
||||
}
|
||||
|
||||
free(to_delete);
|
||||
} else {
|
||||
// Pokračujeme ďalej
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
return first;
|
||||
}
|
||||
51
du4/a_train.h
Normal file
51
du4/a_train.h
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef TRAIN_H
|
||||
#define TRAIN_H
|
||||
#define SIZE 100
|
||||
|
||||
/**
|
||||
* Jeden vozen vlaku
|
||||
*/
|
||||
struct car {
|
||||
/**
|
||||
* Nazov cielovej stanice
|
||||
*/
|
||||
char value[SIZE];
|
||||
/**
|
||||
* Smenik na dalsi vozen
|
||||
*/
|
||||
struct car* next;
|
||||
};
|
||||
|
||||
/**
|
||||
* Prida vozen na koniec vlaku.
|
||||
*
|
||||
* @arg nazov cielovej stanice, ktory sa ma priradit novemu voznu.
|
||||
* @return smernik na zaciatok vlaku.
|
||||
*/
|
||||
struct car* add_car(struct car* first,const char* target);
|
||||
|
||||
|
||||
/**
|
||||
* Vypise vsetky vozne vo vlaku
|
||||
*
|
||||
* @arg smernik na prvy vozen
|
||||
*/
|
||||
void print_train(struct car* first);
|
||||
|
||||
/**
|
||||
* Zrusenie vsetkych voznov vo vlaku.
|
||||
* @arg smernik na prvy vozen
|
||||
*/
|
||||
void cancel_train(struct car* first);
|
||||
|
||||
/**
|
||||
* Vyradenie vsetkych voznov, ktorych cielova stanica je target
|
||||
*
|
||||
* @arg smernik na prvy vozen
|
||||
* @arg cielova stanica, ktora sa ma vyradit z vlaku.
|
||||
* @return smernik na novy prvy vozen
|
||||
*
|
||||
*/
|
||||
struct car* clear_train(struct car* first,const char* target);
|
||||
|
||||
#endif // TRAIN_H
|
||||
30
du4/main.c
Normal file
30
du4/main.c
Normal file
@ -0,0 +1,30 @@
|
||||
#include "a_train.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
struct car* train = NULL;
|
||||
char name[SIZE];
|
||||
|
||||
printf("Zadajte zoznam cieľových staníc (ukončite prázdnym riadkom):\n");
|
||||
while (1) {
|
||||
if (fgets(name, SIZE, stdin) == NULL) break;
|
||||
// odstranit '\n'
|
||||
name[strcspn(name, "\n")] = '\0';
|
||||
if (strlen(name) == 0) break;
|
||||
train = add_car(train, name);
|
||||
}
|
||||
|
||||
printf("\nZadajte stanicu, ktorá sa má vyradiť:\n");
|
||||
fgets(name, SIZE, stdin);
|
||||
name[strcspn(name, "\n")] = '\0';
|
||||
|
||||
train = clear_train(train, name);
|
||||
|
||||
printf("\nVýsledný vlak bez stanice %s bude:\n", name);
|
||||
print_train(train);
|
||||
|
||||
cancel_train(train);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user