Why does function removeFirst give Segmentation fault?

Asked

Viewed 28 times

-2

//remove da lista a primeira ocorrência do Item i
void removeFirst(list* l, Item i){
   link cursor = l->first;
   if (cursor == NULL){ //se a lista for vazia
       printf("Não há elementos nessa lista");
   }

   if (itemCompare(i, l->first->item)==0){ 
//se o primeiro da lista for igual ao item i
        cursor = cursor->next;
        cursor->next->prev = NULL;
        deleteItem(l->first->item);
        exit(1);
}
   while(cursor->next!=NULL){
        if(itemCompare(i, l->first->item)==0)
        {
           cursor->next->prev = cursor->prev;
           cursor->prev->next = cursor->next;
           deleteItem(l->first->item);
           exit(1);
        }
        cursor = cursor->next;
    }
}
  • 3

    If cursor is null, what will be the value of cursor->next?

  • post an entire program, compileable

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.