-
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.
ADONA,AR,72001,92.90,35.04,494
AGNOS,AR,72513,91.61,36.21,1433)
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.
Code:
//Header structure
typedef struct
{
struct Node *psHead;
struct Node *psTail;
int node_count;
}Header;
//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;
}Node;
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");
}
else
{
while ( fgets(line,100,readfile) != NULL )
{
psnew = malloc(sizeof(struct node_d));
z++;
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;
}
}
fclose(readfile);
-
sorry there is supposed to be an
right before FILE *readfile;
-
Ideally you need to start splitting your code up, moving stuff out of main().
There is some good help here for link lists:
http://cslibrary.stanford.edu/
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:
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
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.
-
doesn't this statement
Code:
while ( fgets(line,100,readfile) != NULL )
prevent it from returning NULL?
-
No. strtok() is used to break down a string into sections. If there are no more "tokening characters", strtok() will return NULL.
Code:
#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);
return(0);
}
/*
Output
0012FF70 >this
0012FF75 >is
0012FF78 >a
0012FF7A >string
00000000
*/