-1

both methods work fine but I am having trouble stopping a unhandled exception after deleting all nodes, the lable I added in display should solve this but I'm still getting the exception:

  void DisplayAllEmp(struct node* head){

    struct node *temp;
    temp =(struct node*)malloc(sizeof(struct node));
    temp = head;
    if( temp == NULL ){ //check if theres anything in the list
     delerr: printf("\nDatabase is empty.\n"); // notify the DB is empty
    }
    else{   
        while( temp!= NULL ) //loop through all employees and print them out
        {
            if( temp->empId == NULL ){ goto delerr; } // for catching error following delete employee when you display the DB
            printf("\nId: %d Address: %s Department: %d DOJ: %d/%d/%d Salary: %d Email: %s", temp->empId, temp->address, temp->depart, temp->day,temp->mounth,temp->year, temp->salary, temp->email); // show the employee
            temp = temp->next;
        }//while end
    }

}   


 void DeleteEmp(struct node* head, int tempID){
    struct node *curNode = head;
    struct node *prevNode = curNode;
    while (curNode != NULL) {
        if(curNode->empId == tempID) {
            prevNode->next = curNode->next;
            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 flound it.
        }
        prevNode = curNode;
        curNode = curNode->next;
    }
 }
conor tighe.
  • 121
  • 2
  • 19

1 Answers1

0

I'm gonna take a stab at it here, but it could probably because of this:

curNode = curNode->next;

Because when it deletes everything, it will seek a node that doesn't exist. So instead i'd suggest you change

while (curNode != NULL)

to

while (curNode -> next != NULL)

Hope this helps!

  • thanks for the help but it does not work, would there be any way i could create an exception where if there are no nodes the method skips the printf? ive tryed everthing from return to break to lables and nothing works – conor tighe. Apr 28 '15 at 22:42