Hi, I need to represent a sparse matrix using liked list in C:
The structure definition is the following;
Code:
typedef struct matrix{
struct matrix *right;
struct matrix *below;
int rowptr, colptr;
double val;
}Matrix;
I know that right is pointing to the next non-zero element of the row (below points to the non-zero element of the column).
I need to define this functions:
1.- Matrix* initiate(void), which uses stdin to read the non-zero element from a .txt file (which only contains the non-zero values).
2.- void print(Matrix*), it prints the matrix using stdout.
3.- Matrix* add(Matrix*, Matrix*);
4.- void set_elemnt(Matrix* A, int i, int j, double x): assigns A(i,j)=x.
5.- double get_element(Matrix * A, int i, int j); returns A(i,j).
I don't know how to define the header cells (wich contain nothing), the other cells represents the non-zero values.
What I have so far:
Code:
Matrix* initiate(void){
Matrix*A=malloc(sizeof(*A));
A->rowptr=rowptr;
A->colptr=colptr;
A->right=malloc(rowptr *sizeof(*rowptr->right));
A->below=malloc(n *sizeof(*n->col));
return A;
}
For the header (the one containing nothing), I tried to do this, but I have no idea if it is useful:
Code:
Matrix* initiate_1(void){
Matrix*A=malloc(sizeof(*A));
A->rowptr=-1;
A->colptr=-1;
A->val=nan;
A->right=A;
A->below=A;
}
To add matrix:
Code:
Matrix* add(Matrix*,Matrix*){
int i;
Matrix*C = initiate();//I don't know what to put here
for(int i =0;; i++){//don't know what to put in the middle space
// don't know what to put here
}
}
Also, I think I can create a matrix with zeros only, but I'm not sure on how I can do that and if it is of any help. I would appreciate if you could help me with this, I barely understand data structures. Thank you!