Thread: problems creating a linked list

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    14

    problems creating a linked list

    I am having trouble creating a linked list, if anyone could point me in the right direction I will be really grateful. The errors I get is on the insert line (not enough parameters) and I am not sure how to go about reading in the file and creating a new structure based on the various structures I have created. This is the code:



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <list.h>
    
    
    
    #define NAME_SIZE       30
    #define CITY_SIZE         15
    
    #define USAGE_ERROR          1
    #define CREATE_LIST_FAILED   2
    #define FILL_LIST_FAILED 3
    
    
    /* structure definitions */
    
    struct name
    {
    	 char surname[NAME_SIZE];
    	 char firstname[NAME_SIZE];
    
    };
    
    struct loc
    {
    	 char city[CITY_SIZE];
    	 unsigned hour;
    };
    
    struct client
    {
     unsigned long id;
     struct name name;
     struct loc loc;
    };
    
    
    /* function prototypes */
    
    int main(int argc, char **argv);
    struct client *populate_list(struct list *client, char *filename);
    
    
    /* function definitions */
    
    int main(int argc, char **argv)
    {
    
    
        struct list *students;
    
        if(argc != 2)
    	 {
            printf("usage: %s csv_file\n", argv[0]);
            exit(USAGE_ERROR);
        }
    
    	 if((clients = create_list()) == NULL)
        {
    		  printf("error: create_list() failed\n");
            exit(CREATE_LIST_FAILED);
        }
    
        if(populate_list(clients, argv[1]) == NULL)
    	 {
            printf("error: populate_list() failed\n");
    		  exit(POPULATE_LIST_FAILED);
        }
    
    
        free_students(clients);    /* Deallocate students allocated by malloc */
        free_list(clients);        /* Deallocate the list */
        return(0);
    }
    
    struct client *populate_list(struct list *clients, char *filename)
    {
    
    	FILE *file;
    	int i;
    
    
    	 if ((file= fopen(filename, "r"))==NULL)
    		{
    			printf("Error:fopen() failed on %s\n", filename);
    			return(NULL);
    		}
    
    
    	 while ( file != EOF)
    		{
    
    		clients = malloc(sizeof(struct client));
    		insert(clients);
    
    		i = fscanf (file, "%lu, %[^,],%[^,],%3s,%u\n",
    								&client ->data.id,
    								&students ->surname,
    								&student -> firstname,
    								&student -> city,
    								&student -> hour);
    	)
    
        fclose(file);
    	 return (i);
    }
    Any help would be appreciated, I have been pulling my hair out all day!

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    [EDIT]
    What's that '#include <list.h>'. You could've pointed that out ... my "corrections" may be wrong.
    My compiler gives me the following error:
    Fatal F1003 \BCC55\include\stdcomp.h 5: Error directive: Must use C++ for STDCOMP.H
    [/EDIT]
    Code:
    int main(int argc, char **argv)
    {
        struct list *students;
        ....
    }
    No structure 'list' has been declared previously. I am sure you mean:
    Code:
    int main(int argc, char **argv)
    {
        struct client *students;
        ....
    }
    Another possible error ist:
    Code:
    struct client *populate_list(struct list *client, char *filename);
    It should be:
    Code:
    client *populate_list(struct list *client, char *filename);
    (The same goes for the function definition)
    Last edited by Sargnagel; 10-22-2002 at 11:50 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're wrong. This is C not C++. Since there are no typedefs, this is how you return a structure:

    struct mystruct function( );

    This is incorrect:

    mystruct function( );

    This is how you return a pointer to a structure:

    struct mystruct * function( );

    This is incorrect:

    mystruct * function( );

    In C you must use the keyword 'struct' unless you typedef/#define.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Yes, you're right, quzah!
    Sorry, I mixed that up. I just checked the sourcecode of my current project and found that I'm using 'typedef'.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    It is in C, the obvious mistake I made was that I should have written clients instead of students (too many programs with student lists!) here:


    Code:
    int main(int argc, char **argv)
    {
    
    
        struct list *clients;
    That is how it should be!

    As for my other queries, does anyone have any ideas?

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    Thanks for the tips Salem,one thing I am having trouble with is that I get an error whenever I try to populate the new structure. I have tried several different types of syntax for this

    Code:
     
    
    i = fscanf (file, "%lu, %[^,],%[^,],%3s,%u\n",
    								&clients ->data.id,
    								&clients ->surname,
    								&clients -> firstname,
    								&clients -> city,
    								&clients-> hour);

    Ithe names are kept in a structure called name, the city and hour is kept in a structure called loc, a structure called client holds the id and pointers to the other 2 structures. I have tried:

    Code:
     
    
    &clients ->data.id
    it does not recognize id

    Code:
     
    
    &clients ->client.name.surname,
    This doesn't work either. I am really not sure what the syntax should be in this case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Creating a global linked list?
    By TwistedJester in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 03:23 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM