Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char depart;
char arrive;
int depart_time;
int arrive_time;
struct node *next;
};
struct node* mknode(char depstat, char arrstat, int dtime, int atime)
{
struct node* np;
np=(struct node*)malloc(sizeof(struct node));
// if(np)
// {
np -> depart=depstat;
np -> arrive=arrstat;
np -> depart_time=dtime;
np -> arrive_time=atime;
np -> next=NULL;
// }
return np;
}
int main(void)
{
int hours_a, minutes_a,c=0,d=0,start=0,check=1;
char arrive[20],depart[20],a[25],b[25];
FILE *ttimes;
struct node *n,*n_start,*n2;
ttimes=fopen("times.txt","r");
if(ttimes == NULL)
{
printf("There was an error opening the train timetable\nNow exitting...\n");
return 1;
}
printf("Enter the name of the station you will be departing from:\n");
scanf("%s", depart);
printf("Enter the name of the station you will be arriving at:\n");
scanf("%s", arrive);
printf("****ARRIVE: %s****\n****DEPART: %s****\n", arrive, depart);
printf("Please enter the time (HH MM) at which you would like to arrive at %s:\n", arrive);
scanf("%d %d", &hours_a, &minutes_a);
printf("****ARRIVE: %d:%d\n", hours_a, minutes_a);
while(fscanf(ttimes,"%s %s %d %d",a,b,&c,&d) != EOF)
{
if(check==1)
{
printf("++++++++++\n");
n_start=n;
}
if(start == 0)
{
// printf("AAA\n");
n=mknode(a,b,c,d);
start=1;
}
if(start == 1)
{
// printf("BBB\n");
n2=mknode(a,b,c,d);
n->next=n2;
n=n2;
// printf("B2B\n");
// start=0;
}
printf("CHECK #%d:: A=%s,B=%s,C=%d,D=%d\n",check,a,b,c,d);
printf("NODES #%d:: A=%s,B=%s,C=%d,D=%d\n",check,n->depart,n->arrive,n->depart_time,n->arrive_time);
check++;
}
printf("***EXITED SCANF***\n");
fclose(ttimes);
return 0;
}
Note - I have used alot of printf's for testing the program when I run it.