Linked List Infinite Loop

I need help getting this insert function to work. As it is, the program will compile, but when it runs, it doesn't stop and I have to close my editor to recover. I've narrowed down the issue to the area between the comment bars. What I'm trying to do here is add a new ListNode onto a SetList. A SetList is a linked list with one member variable, head, that is a pointer to a ListNode. A ListNode has two member variables: info, some value of type T, and next, a pointer to another ListNode.

Here is the insert function in question:

pair<iterator,bool> insert( T data )//, iterator target )
{
ListNode<T>* p;
for ( p = head ; p != nullptr ; p = p->next )
{
if ( p->info == data ){ return pair<iterator,bool>(iterator(p),false);}
}
//////////////////////////////////////////////////////////////////////////////////////
if ( head != nullptr )
{
ListNode<T>* holder;
holder = head;
holder = ListNode<T>::append(head, new ListNode<T>(data,nullptr));
ListNode<T>::deleteList(head);
head = holder;
}
/////////////////////////////////////////////////////////////////////////////////////
else
{
head = new ListNode<T>(data,nullptr);
}

return pair<iterator,bool>(iterator(head),true);
}


Any help would be greatly appreciated.
Registered users can post here. Sign in or register to post.