I'm working on the insert function for a linked list. I can't figure out what's wrong with it. I'm trying to create a phonebook. Here's what I have...it just crashes on me when the function is called in main. I know that it's this function because of some debugging, but can't figure out what the problem is because no warnings are popping up. Well, here's the code.
Code:
void insert(Entryptr newnodeptr)
{
Entryptr trailing = head, target = head;
if (head->next == NULL) //List is empty
head->next = newnodeptr;
else if (strcmp(newnodeptr->lastname, head->lastname)>0)
{
newnodeptr->next = head;
head = newnodeptr;
}
else
{
target = trailing->next;
while(target != NULL)
{
if (strcmp(newnodeptr->lastname, target->lastname)>0)
{
newnodeptr->next = target;
trailing->next = newnodeptr;
}
else if (strcmp(newnodeptr->lastname, target->lastname)==0)
{
if (strcmp(newnodeptr->firstname, target->firstname)>0)
{
newnodeptr->next = target;
trailing->next = newnodeptr;
}
else
{
trailing = target;
target = trailing->next;
}
}
else
{
trailing = target;
target = trailing->next;
}
}
}
}