Thread: My first and probably last LINKED LIST! - Please help

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    12

    My first and probably last LINKED LIST! - Please help

    Hi everyone,

    I'm new to this site and also a beginner at C programming, I was wondering if anyone could help me with this problem that I have involving a linked list.

    I always get a 'bus error' when I try to access the data in the structure and I don't know what I have done wrong.

    Here is my code so far:

    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.


    As you can see I'm struggling, any help would be greatly appreciated. Thank you.

    Mandy

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'd be amazed if this even compiles, never mind runs

    > n=mknode(a,b,c,d);
    a and b are strings, yet the function only expects char.


    As well as a mknode() function, also write
    struct node* appendList ( struct node *head, struct node *newNode );

    > struct node *n,*n_start,*n2;
    Initialise these to NULL for a proper empty list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Here is what I got.

    $gcc --version
    gcc (GCC) 3.3.3 (SuSE Linux)
    Copyright (C) 2003 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    $gcc -Wall ll.c -o lc
    ll.c: In function `main':
    ll.c:72: warning: passing arg 1 of `mknode' makes integer from pointer without a cast
    ll.c:72: warning: passing arg 2 of `mknode' makes integer from pointer without a cast
    ll.c:78: warning: passing arg 1 of `mknode' makes integer from pointer without a cast
    ll.c:78: warning: passing arg 2 of `mknode' makes integer from pointer without a cast
    ll.c:86: warning: format argument is not a pointer (arg 3)
    ll.c:86: warning: format argument is not a pointer (arg 4)
    $./lc
    There was an error opening the train timetable
    Now exitting...
    $

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    Thank you for your help Salem, i have corrected the function to expect a char array character array. btu i still am unable to get the information from the linked list. when the program runs the n->depart etc it always crashes.

    i have absolutley no clue what is wrong the data is read in using fscanf from a file, the file is the correct format to read in the data but i just cant get the data back from the structure

    any assistance would be great. thank you.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char depart;
    Say
    char depart[50];

    > np -> depart=depstat;
    Say
    strcpy(np -> depart, depstat );

    And pass the first two parameters as char* rather than char.

    Post your latest attempt if you still can't figure it out with those changes.
    You should be pretty close to the answer...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    Thats awesome thank you sooo much

    this part of the program is working good now

    thanks a lot!!!

Popular pages Recent additions subscribe to a feed