Hi everyone,
I'm trying to print an array of linked lists and I'm getting into a bit of a bind. Before I pass it to the print function I know the list has been populated properly since the function prints my array of linked lists in order however somewhere in the function it sets a head pointer to NULL which throws everything after that.
Code:
printSparse(matrix1,matrix1->rows);
printf("\n");
printSparse(matrix2,matrix2->rows);
printf("\n");
Code:
void printSparse (Matrix *m, int dimensionRow)
{
int i=0;
Node** rowptr = m->rootnodes;
for (int i=0; i<m->rows; i++)
{
while(rowptr[i] != NULL)
{
printf("%d,%d,%d\n",i,rowptr[i]->col,rowptr[i]->val);
rowptr[i] = (rowptr[i])->next;
}
}
}
In case you need to know what my structs look like:
Code:
typedef struct Node
{
int col,val; // No need to store row
struct Node *next; // Used to point to next node in the linked list.
} Node;
typedef struct Matrix
{
int rows,cols; //Records of number of rows and columns required.
Node **rootnodes;//will fill this with rows later
} Matrix;
The problem is that once printSparse has finished the array of pointers (rootnodes) seems to be blank.
So running through GDB before printSparse call:
print matrix->rootnodes[0] = Node * (memory address)
print matrix->rootnodes[1] = Node * (memory address)
etc..
Afterwards I get:
print matrix->rootnodes[0] = Node * (0x0)
So I'm doing something stupid in my printSparse function but I can't figure out what. Obviously I'm setting a headpointer to NULL somewhere but I can't figure out how to restructure or do things different. I'm no a wizard c programmer I've been plugging away on the overall program for weeks trying to get to grips with it so please bear with me.
Thanks all.