2020-11-05 18:01:56 +00:00
|
|
|
#include "a_train.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
2020-11-05 18:04:10 +00:00
|
|
|
/*int main(void){
|
2020-11-05 18:01:56 +00:00
|
|
|
printf("Zadajte zoznam cieľových staníc a počet cestujúcich.\n");
|
|
|
|
printf("Zoznam zakončite prázdnym riadkom.\n");
|
2020-11-05 18:03:18 +00:00
|
|
|
|
2020-11-05 18:01:56 +00:00
|
|
|
add_car;
|
|
|
|
return 0;
|
2020-11-05 18:04:10 +00:00
|
|
|
}*/
|
2020-11-05 18:01:56 +00:00
|
|
|
|
|
|
|
struct car* add_car(struct car* first,const char* target) {
|
|
|
|
struct car* newcar = calloc(1,sizeof(struct car));
|
2020-11-05 18:39:07 +00:00
|
|
|
struct car* this=first;
|
2020-11-05 19:16:27 +00:00
|
|
|
strcpy(newcar->value, target);
|
2020-11-05 18:56:30 +00:00
|
|
|
if(this==NULL){
|
2020-11-05 18:57:27 +00:00
|
|
|
return newcar;
|
2020-11-05 18:56:30 +00:00
|
|
|
}
|
2020-11-05 19:01:54 +00:00
|
|
|
while(this->next!=NULL){
|
2020-11-05 18:39:07 +00:00
|
|
|
this=this->next;
|
2020-11-05 18:01:56 +00:00
|
|
|
}
|
2020-11-05 19:15:52 +00:00
|
|
|
this->next = newcar;
|
2020-11-05 19:06:06 +00:00
|
|
|
return first;
|
2020-11-05 18:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_train(struct car* first) {
|
2020-11-05 19:41:10 +00:00
|
|
|
struct car* this=first;
|
|
|
|
while(this->next!=NULL){
|
2020-11-05 19:29:42 +00:00
|
|
|
printf("%s",this->value);
|
2020-11-05 19:21:48 +00:00
|
|
|
this=this->next;
|
2020-11-05 19:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-05 18:01:56 +00:00
|
|
|
|
|
|
|
void cancel_train(struct car* first) {
|
2020-11-05 19:52:30 +00:00
|
|
|
if(first!=NULL){
|
|
|
|
first->next=NULL;
|
|
|
|
cancel_train(first);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return;
|
|
|
|
/*Každé vyhradenie dynamickej pamäte by malo byť neskôr uvoľnené pomocou free.
|
|
|
|
Mala by spĺňať predpis:
|
|
|
|
void cancel_train(struct car* first);
|
|
|
|
Funkcia na zrušenie spojkového zoznamu môže pracovať rekurzívne: Ak je zoznam prázdny,
|
|
|
|
potom rušenie končí. Inak sa najprv uvoľní ďalší prvok next rekurzívnym volaním.
|
|
|
|
Na konci sa uvoľní aktuálny prvok.
|
2020-11-05 19:44:57 +00:00
|
|
|
|
2020-11-05 19:52:30 +00:00
|
|
|
Nezabudnite, že ak pamäť uvoľníte pamäť, tak s ňou už nemôžete pracovať.*/
|
|
|
|
}
|
2020-11-05 18:01:56 +00:00
|
|
|
}
|
|
|
|
struct car* clear_train(struct car* first, const char* target) {
|
|
|
|
struct car* prev = first;
|
2020-11-05 19:44:57 +00:00
|
|
|
//Už sme si istí, že prev a prev->next nie sú NULL
|
2020-11-05 19:52:30 +00:00
|
|
|
/* while (prev->next->next != NULL){
|
2020-11-05 19:44:57 +00:00
|
|
|
prev = prev->next;
|
|
|
|
}
|
|
|
|
struct car* third = prev->next->next;
|
|
|
|
free(prev->next);
|
|
|
|
prev->next = third;
|
|
|
|
return first;
|
2020-11-05 19:52:30 +00:00
|
|
|
*/
|
2020-11-05 19:44:57 +00:00
|
|
|
}
|
2020-11-05 18:01:56 +00:00
|
|
|
|