(I edited the programm since I was asked for a Minimum Reproducible Example) I have to build a dinamically linked list (specifically), each node having a value of the type float and it's location (row and column) in a sparce matrix. The matrix itself doesn't have a defined size, so I have to find it to print the matrix with the values in a format like:
0.0 0.0 3.0
1.0 0.0 0.0
0.0 0.0 0.0
The value is zero if it's not in the list, otherwise, I have to print it's value in the corresponding location from the "node->row" and "node->col" data from it's node.
Until now, I have tryed to find the size by searching the list with two functions, one for finding the biggest row value, another for finding the biggest column value:
/*
Matrix is defined as:
typedef struct node* Matrix;
in .h file
*/
struct node{
float value;
int row, col;
struct node *next;
};
typedef struct node node;
Matrix* create_matrix(){
Matrix* m = (Matrix*) malloc(sizeof(Matrix));
*m = NULL;
return m;
}
void free_matrix(Matrix* m){
if(m != NULL){
node *n;
while((*m != NULL)){
n = *m;
*m = (*m)->next;
free(n);
}
free(m);
}
}
/*int size_r(Matrix* m){
if(empty_matrix(m)) return 0;
int r = 0;
node *n = *m;
while(n != NULL){
if(n->row > r){
r = n->row;
}
n = n->next;
}
return r;
}
int size_c(Matrix* m){
if(empty_matrix(m)) return 0;
int c = 0;
node *n = *m;
while(n != NULL){
if(n->col >= c)
c = n->col;
n = n->next;
}
return c;
}*/
//The size functions are commented since I have been trying
//to implement their method inside the new print_matrix
int empty_matrix(Matrix *m){
if(m == NULL) return 1;
if(*m == NULL) return 1;
return 0;
}
int insert_value(Matrix *m, float d, int r, int c){
if(m == NULL) return 0;
node *n = (node*) malloc(sizeof(node));
if(n == NULL) return 0;
n->value = d;
n->row = r;
n->col = c;
n->next = *m;
*m = n;
return 1;
}
/*
//I thought this one was working, but after testing around, it returns 1
//even if only row or column matches the argument
int verify_position(Matrix *m, int r, int c){
if(empty_matrix(m)) return 0;
if(r < 0 || c < 0) return 1;
node *n = *m;
while(n != NULL && n->row != r && n->col != c){
n = n->next;
}
if(n == NULL) return 0;
else{
return 1;
}
}*/
/*int print_matrix(Matrix* m){
if(empty_matrix(m)) return 0;
int sr = size_r(m), sc = size_c(m);
float matrix [sl][sc];
for(int i = 0; i <= sr; i++){
for(int j = 0; j <= sc; j++){
node *n = *m;
while(n != NULL && n->row != r && n->col != c){
n = n->next;
}
if(n == NULL)
matrix[i][j] = 0.f;
else
matrix[i][j] = n->value;
printf("%2.f ",matrix[i][j]);
}
printf("\n");
}
return 1;
}*/
int print_matrix(Matrix* m){
if(empty_matrix(m)) return 0;
int r = 0, c = 0;
node *n = *m;
while(n != NULL){
if(n->row > r)
r = n->row;
if(n->col > c)
c = n->col;
n = n->next;
}
float matrix [r][c];
for(int i = 0; i <= r; i++){
for(int j = 0; j <= c; j++){
node *aux = *m;
while(aux != NULL && aux->row != i && aux->col != j){
aux = aux->next;
}
if(aux == NULL)
matrix[i][j] = 0.f;
else{
matrix[i][j] = aux->value;
}
printf("%2.f ",matrix[i][j]);
}
printf("\n");
}
return 1;
}
int dump_matriz(const char *tag, const Matriz *m) {
printf("%s (%p):\n", tag, m);
if (m == NULL) return;
node *n = *m;
while (n != NULL) {
printf("\n%p: [%d][%d] = %f\n", (void*)n, n->row, n->col, n->value);
n = n->next;
}
return 1;
}
When I try my printing function in the console, it just closes it. If I try using the size functions, the return value is never one of the rows or columns. After using the dump_matrix, suggested in a comment, it printed:
Result:
(00000000006F16E0):
00000000006F19A0: [1073741824][1073741824] = 1091567616.000000
00000000006F1950: [1073741824][1065353216] = 1090519040.000000
00000000006F1900: [1073741824][0] = 1088421888.000000
00000000006F18B0: [1065353216][1073741824] = 1086324736.000000
00000000006F1860: [1065353216][1065353216] = 1084227584.000000
00000000006F1810: [1065353216][0] = 1082130432.000000
00000000006F17C0: [0][1073741824] = 1077936128.000000
00000000006F1770: [0][1065353216] = 1073741824.000000
00000000006F1720: [0][0] = 1065353216.000000
for values 1 through 9 in a 3x3 matrix.