weevilofdoom
Programmer
well, I've got this generic doubly linked list written, all except for the InsertAfter method, this seems to be giving me some trouble. Anyways, I think I'll share what I've got, and if anybody has any input, please please please help.
What I'm doing here is inserting a node after the node
that contains "data". So, we've got access to the node class via friends.
template <typename U>
void DLList<U>::InsertAfter(U newData, U data)
{
DLListNode<U>* temp = new DLListNode<U>;
DLListNode<U>* tempHead = head;
temp->m_data = newData;
if(IsEmpty())
throw "Empty List!";
//if head and tail are the same:
else if(head->m_data == tail->m_data)
{
temp->prev = tail;
temp->next = 0;
tail = temp;
}
//if the node is off the tail:
else if(tail->m_data == data)
{
temp->prev = tail;
if(tail != NULL)
{
tail->next = temp;
}
tail = temp;
}
//else it's in the middle (TROUBLE HERE !!! )
else
{
while(tempHead->m_data != data)
{
/*
if(tempHead->m_data == data)
{
temp->next = tempHead;
tempHead->next = temp;
temp->prev = tempHead;
}
*/
tempHead = tempHead->next;
/*
if(tempHead == NULL)
{
throw "Node not found.";
return;
}
*/
}
head = temp;
}
//ARGH! Couldn't figure this out for the life of me, I AM SO STUPID!
}
What I'm doing here is inserting a node after the node
that contains "data". So, we've got access to the node class via friends.
template <typename U>
void DLList<U>::InsertAfter(U newData, U data)
{
DLListNode<U>* temp = new DLListNode<U>;
DLListNode<U>* tempHead = head;
temp->m_data = newData;
if(IsEmpty())
throw "Empty List!";
//if head and tail are the same:
else if(head->m_data == tail->m_data)
{
temp->prev = tail;
temp->next = 0;
tail = temp;
}
//if the node is off the tail:
else if(tail->m_data == data)
{
temp->prev = tail;
if(tail != NULL)
{
tail->next = temp;
}
tail = temp;
}
//else it's in the middle (TROUBLE HERE !!! )
else
{
while(tempHead->m_data != data)
{
/*
if(tempHead->m_data == data)
{
temp->next = tempHead;
tempHead->next = temp;
temp->prev = tempHead;
}
*/
tempHead = tempHead->next;
/*
if(tempHead == NULL)
{
throw "Node not found.";
return;
}
*/
}
head = temp;
}
//ARGH! Couldn't figure this out for the life of me, I AM SO STUPID!
}