Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char depart[25];
char arrive[25];
int depart_time;
int arrive_time;
int id;
struct node *next;
};
struct node* mknode(char depstat[25], char arrstat[25], int dtime, int atime, int check)
{
struct node* np;
np=(struct node*)malloc(sizeof(struct node));
if(np)
{
strcpy(np->depart,depstat);
strcpy(np->arrive,arrstat);
np->depart_time=dtime;
np->arrive_time=atime;
np->id=check;
np->next=NULL;
}
return np;
}
int direct(char depart[25], char arrive[25], int arrival, struct node *n, int check)
{
int direct_value=0,time=0;
printf("\nDIRECT ROUTE: ");
while((n->id)<=(check))
{
if((strcmp(depart,n->depart)==0)&&(strcmp(arrive,n->arrive)==0)&&(n->arrive_time<=arrival)&&((arrival-200)<(n->arrive_time)))
{
time=duration(n->depart_time, n->arrive_time);
printf("\nDepart: %s at %.2d:%.2d\n",depart,n->depart_time/100,n->depart_time%100);
printf("Arrive: %s at %.2d:%.2d\nChange at: None\n",arrive,n->arrive_time/100,n->arrive_time%100);
printf("Duration: %.2d:%.2d\n",time/60,time%60);
direct_value=1;
}
if((n->next) != NULL)
n=n->next;
else
break;
}
if(direct_value==0)
printf("None\n");
return 0;
}
int indirect(char depart[25], char arrive[25], int arrival, struct node *n,int check)
{
struct node *second_node=NULL, *head=NULL;
char change_station[25];
int loops=1,change=0,indirect_value=0;
int wait_time,time;
head=n;
second_node=n;
printf("\nINDIRECT ROUTE: ");
while((second_node->id) <= check)
{
if(strcmp(depart,second_node->depart)==0)
{
strcpy(change_station,second_node->arrive);
change=1;
}
if(change==1)
{
while((n->id) <= check)
{
if((strcmp(n->arrive,arrive)==0)&&(strcmp(change_station,n->depart)==0)&&(n->arrive_time<=arrival)&&((arrival-200)<(n->arrive_time)))
{
wait_time=duration(second_node->arrive_time,n->depart_time);
if((5<=wait_time)&&(wait_time<=60))
{
time=duration(second_node->depart_time, n->arrive_time);
printf("\nDepart: %s at %.2d:%.2d\n",depart,second_node->depart_time/100,second_node->depart_time%100);
printf("Arrive: %s at %.2d:%.2d\n",arrive,n->arrive_time/100,n->arrive_time%100);
printf("Change at: %s\n",change_station);
printf("Duration: %.2d:%.2d\nWait time at %s: %.2d minutes\n",time/60,time%60, change_station,wait_time);
indirect_value=1;
}
}
if((n->next) != NULL)
n=n->next;
else
break;
}
}
n=head;
loops++;
if((second_node->next) != NULL)
second_node=second_node->next;
else
break;
change=0;
}
if(indirect_value==0)
printf("None\n");
return 0;
}
int check_station(char station[25], struct node *n, int check)
{
int real_station=0;
while((n->id)<=(check))
{
if((strcmp(station,n->depart) == 0) || (strcmp(station,n->arrive) == 0))
real_station=1;
if((n->next) != NULL)
n=n->next;
else
break;
}
if(real_station==0)
printf("THE STATION ENTERED '%s' IS NOT A VALID STATION\n",station);
return real_station;
}
int duration(int timeA, int timeB)
{
int hoursA,hoursB,minsA,minsB,timeA_mins,timeB_mins,diff_mins;
hoursA=timeA/100;
minsA=timeA-(hoursA*100);
timeA_mins=(hoursA*60)+(minsA);
hoursB=timeB/100;
minsB=timeB-(hoursB*100);
timeB_mins=(hoursB*60)+(minsB);
diff_mins=timeB_mins-timeA_mins;
return diff_mins;
}
int deallocate(struct node *n, int check)
{
struct node *n2;
int number;
for(number=1; number<=check; number++)
{
if(number==n->id)
{
n2=n;
n=n->next;
free(n2);
return 1;
}
}
}
int main(void)
{
int arrival,c=0,d=0,start=0,check=0,real_station=0;
char arrive[20],depart[20],a[25],b[25];
FILE *ttimes;
struct node *n=NULL,*n_start=NULL,*n2=NULL, *n3=NULL;
ttimes=fopen("times.txt","r");
if(ttimes == NULL)
{
printf("There was an error opening the train timetable\nNow exiting...\n");
return 1;
}
while(fscanf(ttimes,"%s %s %d %d",a,b,&c,&d) != EOF)
{
check++;
if(start == 1)
{
n2=mknode(a,b,c,d, check);
n->next=n2;
n=n2;
}
if(start == 0)
{
n=mknode(a,b,c,d, check);
start=1;
n_start=n;
}
}
fclose(ttimes);
while(real_station == 0)
{
printf("Enter the name of the station you will be departing from:\n");
scanf("%s",depart);
real_station=check_station(depart, n_start, check);
}
real_station=0;
while(real_station == 0)
{
printf("Enter the name of the station you will be arriving at:\n");
scanf("%s",arrive);
real_station=check_station(arrive, n_start, check);
}
printf("Please enter the time (HHMM) at which you would like to arrive at %s:\n", arrive);
scanf("%d", &arrival);
printf("\nUser wants to travel from %s to %s, to arrive at %d\n",depart,arrive,arrival);
direct(depart, arrive, arrival, n_start, check);
indirect(depart, arrive, arrival, n_start, check);
deallocate(&n_start, check);
printf("\n");
return 0;
}
i have left in my original deallocate function. there is an input file, but i know that this part of the code is correct, so hopefully some sense can still be made of this