List - Why my delete function doesn't delete?
Hello everyone. I hope you can help me because I really don't know what to do :frown:
I have to manage a Clinic. I need to delete a booking (for example, if John said he's coming on March 22nd at 15:30 but then he say he's not going to come, I have to delete that booking so another person can use it).
idSearched: the id of the person that is not going to come.
I have a lot of specialties and each one has a list. So I ask for the speciality to delete the node (the node contains John's booking). If I don't find it, I return (-1).
searchSpecByID return a pointer to the list where the speciality is. So head will point to the first node of the list.
In nodeToDelete I have the node I want to delete.
The program detects OK when is the first in the list and when not, but it doesn't delete the node.
Code:
typedef struct bookingList{
tSpecialty specialty;
struct nodo* intro;
}tList;
int deleteNode(tClinic* clinic, char idSearched[MDNI])
{
tNode* nodeToDelete;
tNode* previous=NULL;
tNode* head=NULL;
tList* searchedSpec;
int esp;
printf("\nSpecialty: ");
scanf("%d", &esp);
searchedSpec=searchSpecByID(clinica, esp);
head=searchedSpec->intro;
nodeToDelete=searchedNode(clinica, idSearched, esp);
if(nodeToDelete==NULL)
return -1;
//If the list is empty
if((head->next)==NULL)
{
printf("\nThe list is empty");
return -1;
}
// if I want to delete the first one
if(strcmp(head->dato->patient->dni, nodeToDelete->dato->patient->dni)==0)
{
head=nodeToDelete->next;
searchedSpec->intro=head;
free(nodeToDelete);
return 0;
}
// the one I want to delete is not the first one
previous=searchedSpec->intro;
while(previous->next!=NULL && previous->next!=nodeToDelete)
previous=previous->next;
previous->next=previous->next->next;
free(nodeToDelete);
return 0;
}
I hope you can help me. I'm going to cry