I am trying to insert some elements on a skiplist
typedef struct skiplist {
struct node * header;
struct node * termnode;
}skiplist;
typedef struct node
{
int key;
entr value;
struct node *next[MaxLevel];
}node;
in which the nodes are sorted by their key.However,when i try to insert a node with a bigger key than the previous one i get a segfault and i found that it happens in the insert function
void insert(list l,int skey,entr newvalue) {
nod update[MaxLevel-1];
for(i=0;i<=MaxLevel-1;i++) {
update[i]=(nod)malloc(sizeof(struct node));
}
nod x=l->header;
for(i=MaxLevel-1;i>=0;i--) {
while (x->next[i]->key<skey) {
x=x->next[i];
}
update[i]=x;
}
x=x->next[0];
if(x->key=skey) x->value=copyvalue(newvalue);
else
{
int lvl=randl();
x=makenode(l,lvl,skey,newvalue);
for(i=0;i<=lvl;i++) {
x->next[i]=update[i]->next[i];
update[i]->next[i]=x;
}
}
printf("\ninserted to list\n");
}
in
while (x->next[i]->key<skey)
because
next[i]
is NULL. I can't understand why this is happening and any help would be appreciated