This commit is contained in:
Your Name 2025-11-06 15:56:30 +01:00
parent 1fe4f011fd
commit ad3378a2f2
8 changed files with 157 additions and 0 deletions

BIN
du4/.a_train.h.swp Normal file

Binary file not shown.

13
du4/Makefile Normal file
View File

@ -0,0 +1,13 @@
CFLAGS= -std=c99 -g -Wall
all: train
%.o: %.c
gcc -c -o $@ $< $(CFLAGS)
train: main.o a_train.o
gcc main.o a_train.o -o train
clean:
rm *.o train

74
du4/a_train.c Normal file
View File

@ -0,0 +1,74 @@
#include "a_train.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct car* add_car(struct car* first,const char* target)
{
struct car* newcar=calloc(1, sizeof(struct car));
strcpy(newcar->value, target);
newcar->next=NULL;
if (first==NULL)
{
return newcar;
}
struct car* this=first;
while (this->next!=NULL)
{
this=this->next;
}
this->next=newcar;
return first;
return NULL;
}
void print_train(struct car* first)
{
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)
{
while (first!=NULL && strcmp(first->value, target)==0)
{
struct car* temp=first;
first=first->next;
free(temp);
}
if (first==NULL)
{
return NULL;
}
struct car* current=first;
while (current->next!=NULL)
{
if (strcmp(current->next->value, target)==0)
{
struct car* temp=current->next;
current->next=current->next->next;
free(temp);
}
else
{
current=current->next;
}
}
return first;
}

51
du4/a_train.h Normal file
View 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

BIN
du4/a_train.o Normal file

Binary file not shown.

19
du4/main.c Normal file
View File

@ -0,0 +1,19 @@
#include "a_train.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Testovaci subor pre vlak
int main(){
struct car* train = NULL;
train = add_car(train,"Presov");
train = add_car(train,"Bratislava");
train = add_car(train,"Levoca");
train = add_car(train,"Spiska Nova Ves");
print_train(train);
clear_train(train,"Levoca");
print_train(train);
cancel_train(train);
return 0;
}

BIN
du4/main.o Normal file

Binary file not shown.

BIN
du4/train Executable file

Binary file not shown.