Originally Posted by
Sebastiani
you should probably post the MList allocation/initialization code.
Thanks for the quick reply.
OK.. this is the function that creates the structure from a text file; outputs a pointer to a Matrix structure:
Code:
Matrix *CreateFromFile(char *filename)
{
// variable declarations
Matrix *m;
FILE *FP;
char snRows[50], snCols[50];
int nRows, nCols;
char srow[50], scol[50], sdata[50];
int rnum, cnum, data;
// allocate space in memory for m
m = (Matrix *) malloc(sizeof(Matrix));
FP = fopen(filename, "r");
// read in number of rows and columns intially, and convert to int
fscanf(FP, "%s %s", snRows, snCols);
m->nRows = atoi(snRows);
m->nCols = atoi(snCols);
// Initialise arrays of MList
struct MList *rows[m->nRows];
struct MList *cols[m->nCols];
// process the file on a line-by-line basis
while( fscanf(FP, "%s %s %s", srow, scol, sdata) != EOF )
{
rnum = atoi(srow);
cnum = atoi(scol);
data = atoi(sdata);
rows[rnum] = list_insert(cnum, data, rows[rnum]);
cols[cnum] = list_insert(rnum, data, cols[cnum]);
}
m->rows = rows;
m->cols = cols;
return m;
}
and the function list_insert()
Code:
MList *list_insert( int index, int value , MList *pl ) {
MList *t = calloc( 1, sizeof( MList ) );
t->index = index;
t->value = value;
// Set pointer to previous list
t->next = pl;
return t;
}
(this function outputs a pointer to a new MList structure)
The number of columns and rows are specified in the first line of the input file (supplied to this function as filename)