I am having a problem with the following code. I need to search first the record then delete it:
It always delete the last record. Even if I want to delete the first any rcord in the middle
string NameForSearch1;
cout<<"Enter Your Friend's Name: "<<endl;
cin>>NameForSearch1;
tempRec->namePerson=NameForSearch1;
if (tempRec==firstRec){//delete the head
tempRec->next=firstRec;
firstRec=tempRec->next;
delete tempRec;
DisplayRec();
}
else if (tempRec->next==NULL ){//delete the last record
tempRec2=firstRec->next;
while(tempRec2->next!=tempRec){
tempRec2 = tempRec2->next;
}
tempRec2->next=NULL;
delete tempRec;
}
else {
//delete anyrecord
tempRec2=firstRec->next;
while(tempRec2->next!=tempRec){
tempRec2 = tempRec2->next;
}
tempRec2->next=tempRec->next;
delete tempRec;
}
After hours of debugging, I am finally able to get the right code: