c - Getting the values of linked list with loops -


i have create function can value of matrix wich take form of double linked list. here structures of matrix

 typedef struct row {     unsigned int indicerow;     struct row * next;     struct col * data; } row;  typedef struct col{     double value;     unsigned int indicecolumn;     struct col * next; } col;  typedef struct matrix{     int nrows;     int ncols;     struct row * rowfirst; }matrix; 

the structure matrix represent top of linked list , contain total number of rows , columns , variable row wich point first node of list of row nodes. row nodes contain number of row of matrice, variable row called next wich represent next line of matrix , variable data point list of col nodes. col nodes contains number of column, value @ coordonates(row,column) , col next. values different of 0 have in col linked list.

to value of precise point of matrix created function sp_get. take structure matrix, line , column i'm looking , double variable argument. returns 0 when works , update variable double *val value i'm looking for.

int sp_get( struct matrix *mat, unsigned int rows, unsigned int col, double *val){         row * temps = (row*)malloc(sizeof(row));         temps = mat->rowfirst;         while(temps->indicerow!= rows){             temps = temps->next;         }          while(temps->data!= null && temps->data->indicecolumn!= col && temps->data->next!=null){             temps->data = temps->data->next;         }          if(temps->data->indicecolumn == col){             *(val) = temps->data->value;         }         else{         *(val) = 0.0;         }          return 0; 

first create row variable run through matrix, row , column. if can't find column means value 0.

when use function 1 value, works well, , return value.(tempmatrix matrix variable , contain linked list)

        double * vall =(double*)malloc(sizeof(double));         sp_get(tempmatrix, 2, 3, vall); 

but when i'm using function double loop don't have same results , can't not explain why...

double * vall =(double*)malloc(sizeof(double));         int i;         int j;         for(i=1;i<=tempmatrix->nrows;i++){             for(j=1; j<=tempmatrix->ncols;j++){                 sp_get(tempmatrix,i,j,vall);                 printf(" %f ", *(vall));             }             printf("\n");         } 

here result loops

and here results should get

it might proble of memory leak, don't know comes from.

thanks in advance help!

just in sp_get alone following problems abound:

memory first 2 lines.

anytime see in successive lines in c:

ptr = malloc(...) ptr = <something else> 

it always memory leak.

updating column header rather enumerating it

once find row seek, this:

while(temps->data!= null &&        temps->data->indicecolumn!= col &&        temps->data->next!=null)     {         temps->data = temps->data->next;     } 

ask yourself, what is temps->data = ... actually updating? changing temps->data pointer point own next, means temps->data pointed prior gone. that's fine if temps->data temporary pointer, but isn't. data member in row struct worked hard find in prior loop.

potential null pointer dereference

you may think having this:

while(temps->data!= null &&        temps->data->indicecolumn!= col &&        temps->data->next!=null) 

for while-condition in loop harbor safety temp-data being null code follows:

if(temps->data->indicecolumn == col) {     *(val) = temps->data->value; } 

but if did, why bother first clause (which correct, btw). appears addition of last clause (temps->data->next!=null) effort stave off crashes. isn't way it.

minor: hiding type col parameter col

needs little explanation. see var names.

minor: there no need dynamically allocate out-parameter you're using it.

your code this:

double * vall =(double*)malloc(sizeof(double)); int i, j; for(i=1;i<=tempmatrix->nrows;i++) {     for(j=1; j<=tempmatrix->ncols;j++)     {         sp_get(tempmatrix,i,j,vall);         printf(" %f ", *(vall));     }     printf("\n"); } 

can this:

double val = 0.0; int i, j; for(i=1;i<=tempmatrix->nrows;i++) {     for(j=1; j<=tempmatrix->ncols;j++)     {         sp_get(tempmatrix,i,j,&val); // note address-of operator         printf(" %f ", val);     }     printf("\n"); } 

updated sp_get

i'm pretty sure you're trying do. following return 0 if indexed values found , retrieved, otherwise returns -1 , out-parameter set 0.0.

int sp_get( struct matrix const *mat, unsigned int rows, unsigned int cols, double *val) {     // prime 0.0     *val  = 0.0;     if (!mats)         return -1;      // walk row table     struct row const *row_ptr = mat->rowfirst;     while (row_ptr && row_ptr->indicerow != rows)         row_ptr = row_ptr->next;      // leave if didn't find row.     if (!row_ptr)         return -1;      struct col const *col_ptr = row_ptr->data;     while (col_ptr && col_ptr->indicecolumn != cols)         col_ptr = col_ptr->next;      if (!col_ptr)         return -1;      *val = col_ptr->value;     return 0; } 

note modify nothing in actual matrix, entire thing, including pointers use index within it, can const (and should be).

best of luck.


Comments

Popular posts from this blog

Error while updating a record in APEX screen -

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

ios - Xcode 5 "No such file or directory" -