my head hurts so much...
I'm having a lot of problems with this. I know the answer is so simple, but I just can't figure it out. I'm trying to take two presorted linked lists, and combine them into one sorted linked list. Here's what I've got so far. As always, it's my logic that's the error.
list definition:
struct DblNode
{
int Data;
DblNode *Next, *Prev;
};
merging function:
void MergeLists(DblNode* &resList, DblNode* &fListOne, DblNode* &fListTwo)
{
DblNode *curRes, *curFirst, *curSecond;
resList = new DblNode;
curRes = resList;
curFirst = fListOne;
curSecond = fListTwo;
while(curFirst->Next != NULL || curSecond->Next != NULL)
{
curRes = new DblNode;
if(curFirst->Data < curSecond->Data)
{
curRes = curFirst;
curRes->Next = curSecond;
curRes = curRes->Next;
}
else
{
curRes = curSecond;
curRes->Next = curFirst;
curRes = curRes->Next;
}
curFirst = curFirst->Next;
curSecond = curSecond->Next;
}
if(curFirst->Next == NULL)
{
while(curSecond->Next != NULL)
{
curRes = new DblNode;
curRes = curSecond;
curRes = curRes->Next;
curSecond = curSecond->Next;
}
}
else
{
while(curSecond->Next != NULL)
{
curRes = new DblNode;
curRes = curSecond;
curRes = curRes->Next;
curSecond = curSecond->Next;
}
}
curRes->Next = NULL;
}
the code I've got here so far is only supposed to link the list with the Next pointer, not the Prev. The reason for this is because if I can't figure out how to do that, why should I clutter it up with some more errorous code? Thanks for the help guys.