Five cviko

This commit is contained in:
Maksym Malko 2020-11-06 07:24:19 +01:00
parent a11d84b3f8
commit 8787edcb34
2 changed files with 105 additions and 0 deletions

54
cv5/a_train.c Normal file
View File

@ -0,0 +1,54 @@
#include "a_train.h"
#include <stdio.h>
struct car* add_car(struct car* first,const char* target) {
struct car *newCar = calloc(1,sizeof(struct car));
strcpy(newCar->value,target);
if(first==NULL){
first = newCar;
}else{
struct car* this = first;
while(this->next!=NULL){
this = this->next;
}
this->next = newCar;
}
return first;
}
void print_train(struct car* first) {
for(struct car* this = first;this!=NULL;this = this->next){
printf("%s\n",this->value);
}
}
void cancel_train(struct car* first){
if(first==NULL){
return;
}
cancel_train(first->next);
free(first);
}
struct car* clear_train(struct car* first, const char* target) {
struct car *previous = NULL;
for(struct car *this = first;this!=NULL;this=this->next){
if(strcmp(this->value,target)==0){
if(previous==NULL){
first = first->next;
free(this);
break;
}else{
previous->next = this->next;
free(this);
break;
}
}
previous = this;
}
return first;
}

51
cv5/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