-1

Every time I try to do this I get a unhanded exception at the if statement? everything else up to that point works fine.

      void DeleteEmp(struct node* head, int tempID){
           struct node *curNode = head;
           struct node *prevNode = NULL;
           while (curNode != NULL) {
                if(curNode->empId == tempID) { // error here
                    free(curNode);
                    printf("Employee %d removed from database", tempID);
                }
                prevNode = curNode;
                curNode = curNode->next;
            }
        }
conor tighe.
  • 121
  • 2
  • 19

3 Answers3

2

You are using curNode after freeing it.

   while (curNode != NULL) {
        if(curNode->empId == tempID) { // error here
            free(curNode);
            printf("Employee %d removed from database", tempID);
        }
        prevNode = curNode;
        curNode = curNode->next; // PROBLEM HERE.
    }

Perhaps you mean to return right after the call to free.

void DeleteEmp(struct node* head, int tempID){
   struct node *curNode = head;
   struct node *prevNode = NULL;
   while (curNode != NULL) {
      if(curNode->empId == tempID) {
         if ( prevNode == NULL )
         {
            // This means curNode is the head.
            // Since curNode is about to be deleted,
            // make the next node the head.
            head = curNode->next;
         }
         else
         {
            // Since curNode is about to be deleted,
            // change the next node of prevNode.
            prevNode->next = curNode->next;
         }

         free(curNode);
         printf("Employee %d removed from database", tempID);
         return;
      }
      prevNode = curNode;
      curNode = curNode->next;
   }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

You should remove the node from the list before freeing it.

       while (curNode != NULL) {
            if(curNode->empId == tempID) {  
                if (prevNode){// remove the node from the list
                    prevNode->next = curNode->next;   
                }
                else{
                    head = curNode->next;
                } 
                free(curNode);
                printf("Employee %d removed from database", tempID);
                return;//assuming empId is unique return after you found it.
            }
            prevNode = curNode;
            curNode = curNode->next;
        }
R Sahu
  • 204,454
  • 14
  • 159
  • 270
Musa
  • 96,336
  • 17
  • 118
  • 137
2

Where you free the node, before you free it you need to do some pointer swapping. Consider this

a->b->c->NULL

Deleting b should do the following before free.

a->c->NULL 

Note the special cases are deleting c, deleting a which results in a new head and deleting the only node in a singleton list.

Note the type GList and handler functions are in library GObject it may make sense to use this list code rather than writing and debugging this.

phil
  • 561
  • 3
  • 10