jeudi 12 mars 2015

Elements are lost in linked lists


This should be an easy one, but I can't spot the mistake and I am going nuts. I have the following structs:



struct bucket {
o_list::Node* pointers;
unsigned int size;
unsigned int length;
};

struct Node {

short data[N];
long norm;
Node *next;
Node *next_in_bucket;
char lock;

};


and the following functions:



void bucket_add(bucket* b, o_list::Node* v){

v->next_in_bucket = b->pointers;
b->pointers = v;
b->length++;

}

void bucket_remove(bucket* b, o_list::Node* v){

o_list::Node *sentinel = b->pointers, *prev;

if(b->pointers==v){
b->pointers = b->pointers->next_in_bucket;
b->length--;
return;
}
else{

prev = sentinel;
sentinel = sentinel->next_in_bucket;

while(sentinel){
if(sentinel == v){
prev->next_in_bucket = sentinel->next_in_bucket;
b->length--;
return;
}
else{
prev = sentinel;
sentinel = sentinel->next_in_bucket;
}
}
}
}


At some point I have bigger "sizes" than elements in the lists. I do not free the elements on purpose! Is there any error here?




Aucun commentaire:

Enregistrer un commentaire