This commit is contained in:
Rastislav Želonka 2019-11-30 20:28:04 +01:00
parent d8b812d9dc
commit a80f56eee8
2 changed files with 101 additions and 4 deletions

View File

@ -1,18 +1,95 @@
#include "a_train.h" #include "a_train.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct car* add_car(struct car* first,const char* target) { struct car* add_car(struct car* first,const char* target) {
return NULL; struct car* newcar = (struct car*)malloc(sizeof(struct car));
strcpy(newcar->value, target);
newcar->next = NULL;
if(first == NULL){
first = newcar;
return first;
}else{
struct car* this = first;
while(this->next != NULL){
this = this->next;
}
this->next = newcar;
}
return first;
} }
void print_train(struct car* first) { void print_train(struct car* first) {
struct car* this = first;
while(this != NULL){
printf("%s \n",this->value);
this = this->next;
}
printf("\n");
} }
void cancel_train(struct car* first) { void cancel_train(struct car* first) {
/* if (first!=NULL){
cancel_train(first->next);
free(first);
first = NULL;
}*/
struct car* next;
while (first != NULL) {
next = first->next;
free(first);
first = next;
}
first = NULL;
} }
struct car* clear_train(struct car* first, const char* target) { struct car* clear_train(struct car* first, const char* target) {
if(first == NULL){
return NULL; return NULL;
} }
if(first->next == NULL){
if(strcmp(first->value,target)== 0){
return NULL;
}else{
return first;
}
}
struct car* prev = first;
/* if(strcmp(prev->value,target)== 0){
struct car* one = prev->next;
free(prev);
prev= one;
//free(one);
//one = NULL;
return first;
}*/
while (prev->next->next != NULL){
if(strcmp(prev->next->value,target)== 0){
struct car* third = prev->next->next;
free(prev->next);
prev->next = third;
return first;
}
prev = prev->next;
}
if(strcmp(prev->next->value,target)== 0){
free(prev->next);
prev->next = NULL;
return first;
}
return first;
}

24
main.c
View File

@ -1,17 +1,37 @@
#include "a_train.h" #include "a_train.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Testovaci subor pre vlak // Testovaci subor pre vlak
int main(){ int main(){
struct car* train = NULL; struct car* train = NULL;
/* int c=0;
printf("Zadajte nazov stanice a pocet cestujucich. \n Ukoncite prazdnym riadkom \n");
char i[100];
while (strcmp(i, "\n") != 0) {
c++;
fgets(i, 100, stdin);
if(c%2 != 0){
train = add_car(train,i);
}
printf("%s \n",i);
}*/
//struct car* train = NULL;
train = add_car(train,"Presov"); train = add_car(train,"Presov");
train = add_car(train,"Bratislava");
train = add_car(train,"Levoca"); train = add_car(train,"Levoca");
train = add_car(train,"Bratislava");
train = add_car(train,"Spiska Nova Ves"); train = add_car(train,"Spiska Nova Ves");
print_train(train); print_train(train);
clear_train(train,"Levoca"); clear_train(train,"Levoca");
print_train(train); print_train(train);
cancel_train(train); cancel_train(train);
return 0; return 0;
} }