Thread: doubly linked lists

  1. #1
    Registered User
    Join Date
    Apr 2003

    doubly linked lists

    Here is the problem. I am trying to create a doubly linked list using data that is read in from a .csv file(text file with values seperated by commas, i.e.

    I don't have any problem finding the file I want and opening it. I think my current problem exists when I am trying to read in data or create the linked list. If someone could either post an algorithm I could follow or find the problems in my code already, I would be much appreciative.

    //Header structure
    typedef struct
    struct Node *psHead;
    struct Node *psTail;
    int node_count;
    //Node structure
    typedef struct node_d
    char city[40];
    char state[2];
    char zipcode[5];
    double longitude;//double
    double latitude;//double
    long population;//long
    struct node_d *previous;
    struct node_d *next;
    int choice, file_choice, counter = 2, file_info, readfile, z=-1;
    char path_name[100], long_temp[25], lat_temp[25], pop_temp[25], city_temp[40];
    char state_temp[2], zipcode_temp[5];
    FILE *readfile;
    	Header *psList;
    	CSV *file_info;
    	Node *psnew, *pscurrent;
        char *DONE, line[100];
    	psnew = (Node*) malloc(sizeof(Node));
        pscurrent = (Node*) malloc(sizeof(Node));
    	psList = (Header*) malloc(sizeof(Header));
    	file_info = (CSV*) malloc(sizeof(CSV));
    	DONE = (char*)malloc(sizeof(char));
    	DONE[0] = 'c';
    	psList->node_count = 0;
    	psList->psHead = NULL;
    	psList->psTail = NULL;
    readfile = fopen(file_info[file_choice].name,"r");
    					if(readfile == NULL)
    					printf("\nfile could not be opened");
    while ( fgets(line,100,readfile) != NULL ) 
    							psnew = malloc(sizeof(struct node_d));
    							strcpy(psnew->city, strtok(line,","));
    							strcpy(psnew->state, strtok(NULL,","));
    							strcpy(psnew->zipcode, strtok(NULL,","));
    							strcpy(long_temp, strtok(NULL,","));
    							psnew->longitude = atof(long_temp);
    							strcpy(lat_temp, strtok(NULL,","));
    							psnew->latitude = atof(lat_temp);
    							strcpy(pop_temp, strtok(NULL,","));
                                psnew->population = atol(pop_temp);
                                psnew->previous = NULL;
                                psnew->next = NULL;
                                psnew->next = pscurrent;
    							psnew->previous = pscurrent->previous;
    							psnew->next->previous = psnew;
    							psnew->previous->next = psnew;

  2. #2
    Registered User
    Join Date
    Apr 2003
    sorry there is supposed to be an
    int main(void)
    right before FILE *readfile;

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Ideally you need to start splitting your code up, moving stuff out of main().

    There is some good help here for link lists:
    I'm not sure if it covers doubly-linked lists in detail, but once you get to grips with a single, converting it to a double is no problem

    >>char state[2];
    How many states are there that can be shortened to 1 character in length? Remember, a string array needs the room for a \0 terminator.

    >>psnew = (Node*) malloc(sizeof(Node));
    The cast isn't needed. Read here:

    Use spaces instead of tabs, or at least sort your tabs out before posting your code here. The forum doesn't display tabs too well, and your code indentation quickly looks a mess.

    >>strcpy(psnew->city, strtok(line,","));
    This is definatetly a no no. If strtok() returns NULL, where is strcpy() going to copy from?

    >>psnew->longitude = atof(long_temp);
    atof() isn't a very good way to convert to a float. Try strtod() instead, it'll be safer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2003
    doesn't this statement
    while ( fgets(line,100,readfile) != NULL )
    prevent it from returning NULL?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    No. strtok() is used to break down a string into sections. If there are no more "tokening characters", strtok() will return NULL.

    #include <stdio.h>
    #include <string.h>
    int main(void)
    	char a[] = "this:is:a:string";
    	char *p;
    	p = strtok(a, ":");
    	while (p)
    		printf ("%p >%s\n", (void*)p, p);
    		p = strtok(NULL, ":");
    	printf ("%p\n", (void*)p);
    0012FF70 >this
    0012FF75 >is
    0012FF78 >a
    0012FF7A >string
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 01:01 AM
  2. Doubly Linked Lists
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2009, 12:51 PM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM