Code:
void loadMatrix(){
char buffer [1000];
fgets(buffer,1000,inputFile);
int count = 1;
while (feof(inputFile)==0)
{
nodes *tNode = (nodes *)malloc(sizeof(nodes));
sscanf(buffer,"%d%d%d%d",&tNode->v[0],&tNode->v[1],&tNode->v[2],&tNode->v[3]);
tNode->rid=count;
tNode->used=0;
if (list->head==NULL) {
list->head=list->tail=tNode;
list->head->next=list->tail->prev=NULL;
} else {
tNode->prev=list->tail;
list->tail->next=tNode;
list->tail=tNode;
};
count++;
fgets(buffer,1000,inputFile);
};
printf("OK: %d records inserted in list.\n", count);
};
While in this case using feof() is okay, the whole thing would be much more readable if you just did this:
Code:
void loadMatrix(){
char buffer [1000];
int count = 1;
while (fgets(buffer,1000,inputFile))
{
nodes *tNode = (nodes *)malloc(sizeof(nodes));
sscanf(buffer,"%d%d%d%d",&tNode->v[0],&tNode->v[1],&tNode->v[2],&tNode->v[3]);
tNode->rid=count;
tNode->used=0;
if (list->head==NULL) {
list->head=list->tail=tNode;
list->head->next=list->tail->prev=NULL;
} else {
tNode->prev=list->tail;
list->tail->next=tNode;
list->tail=tNode;
};
count++;
};
printf("OK: %d records inserted in list.\n", count);
};
I'd also use sizeof(buffer) rather than hard-coding the value 1000 into the fgets() call.
The new code also puts the declaration of count right at the beginning of the block, making the code C89. As you originally had it, count was declared in the middle of a block, making the code C99, which is a newer standard that fewer compilers support to any degree. Unless you're using other C99-specific code, it's probably best to stick with C89.
Code:
nodes *tNode = (nodes *)malloc(sizeof(nodes));
Casting malloc() is a bad idea. I don't feel like going over the details here -- search the boards if you're curious. Leaving out the cast works just fine.
Also, using sizeof(*tNode) instead of sizeof(nodes) is a good idea, in case you change the type of tNode or something. They're both compile-time constants, so no worry there.
So I'd use:
Code:
nodes *tNode = malloc(sizeof(*tNode));
Also note that the parentheses around sizeof(*tNode) are optional; they always are when you're taking the sizeof a variable. You need them in when you're taking the sizeof a type like nodes.
It would be a good idea to check the return value of sscanf() to see if it encountered any errors. In this case, it should return 4 on success (because there are four format specifiers, %d and %f etc).
You don't need the semicolons after the closing curly braces. The only place you do need them is after compound initializers or struct/union/enum declarations.
Shouldn't count be initialized to zero rather than to one?
Code:
typedef struct nodes {
int v[DIMS*2];
struct nodes *next;
struct nodes *prev;
int rid;
int used;
struct dimlist *xmin;
} nodes;
typedef struct dimlist {
struct nodes *head;
struct nodes *tail;
} dimlist;
You don't need to use struct nodes inside the declaration for dmlist. You can use just plain nodes, because the typedef has taken effect by then.
I don't understand why you've declared xmin as you have. First let's make sure I have its use down properly. You want a doubly-linked list. You also want a way to transverse this list in minimum order, while retaining the order of the original doubly-linked list as well. Is that right?
Well, the way you have it set up, the first struct nodes would have to be the smallest before you sort it. If the nodes are in "random" order, and you still want a way to transverse them in order, I'd suggest fields like this:
Code:
typedef struct nodes {
int v[DIMS*2];
struct nodes *next;
struct nodes *prev;
int rid;
int used;
struct nodes *xmin;
} nodes;
typedef struct dimlist {
struct nodes *head;
struct nodes *tail;
struct nodes *xmin;
} dimlist;
That way, the first (and smallest) struct nodes can be anywhere in the original list, and you can transverse the list with head to go forwards, tail to go backwards, or xmin to go in sorted order.