typedef struct _node {
int value;
struct _node *next;
struct _node *previous;
} node;
typedef node* list;
typedef struct {
int nb_rows;
int nb_columns;
list *matrix;
/* array of linked lists
each list corresponds to a column,
each value of a node in this list
gives the positions of a 1 in this column */
} ldpc_matrix;
void add_rows(ldpc_matrix *m, int i1, int i2){
int j;
list l, l1;
int value_i2;
for(j=0; j<m->nb_columns; j++){
l = m->matrix[j];
l1 = NULL;
value_i2 = 0;
while(l!=NULL){
if(l->value==i2)
value_i2 = 1;
if(l->value==i1)
l1 = l;
l = l->next;
}
if((l1!=NULL) && (value_i2==1)){
if(l1->previous){
l1->previous->next = l1->next;
if(l1->next)
l1->next->previous = l1->previous;
}
else{
m->matrix[j] = l1->next;
}
free(l1);
} else {
if((l1==NULL) && (value_i2==1)){
l1 = m->matrix[j];
m->matrix[j] = (node*) malloc(sizeof(node));
m->matrix[j]->value = i1;
m->matrix[j]->next = l1;
m->matrix[j]->previous = NULL;
}
}
}
}
# Re: Petit problème en C (free)
Posté par Antoine Schweitzer-Chaput . En réponse au journal Petit problème en C (free). Évalué à 1.
typedef struct _node { int value; struct _node *next; struct _node *previous; } node; typedef node* list; typedef struct { int nb_rows; int nb_columns; list *matrix; /* array of linked lists each list corresponds to a column, each value of a node in this list gives the positions of a 1 in this column */ } ldpc_matrix; void add_rows(ldpc_matrix *m, int i1, int i2){ int j; list l, l1; int value_i2; for(j=0; j<m->nb_columns; j++){ l = m->matrix[j]; l1 = NULL; value_i2 = 0; while(l!=NULL){ if(l->value==i2) value_i2 = 1; if(l->value==i1) l1 = l; l = l->next; } if((l1!=NULL) && (value_i2==1)){ if(l1->previous){ l1->previous->next = l1->next; if(l1->next) l1->next->previous = l1->previous; } else{ m->matrix[j] = l1->next; } free(l1); } else { if((l1==NULL) && (value_i2==1)){ l1 = m->matrix[j]; m->matrix[j] = (node*) malloc(sizeof(node)); m->matrix[j]->value = i1; m->matrix[j]->next = l1; m->matrix[j]->previous = NULL; } } } }