C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-23-2001, 06:42 PM   #1
Unregistered
Guest
 
Posts: n/a
Exclamation Linked List headache

I think my head is going to explode. I cannot figure this out for the life of me. I'm trying to create two linked lists. No problem. But the second part is to merge them together into one list. This is where my headache comes from. Let's assume that the lists the user has entered are indeed in order. Now, we've got to turn these two lists into one list that is sorted. Each time comparing the data fields to see which one is less than or greater than the other. The function in my program that is giving me a headache is the "MergeLists" function. You'll see it below. Thanks for any help you guys can give.

#include <iostream.h>
#include <stddef.h>


struct DblNode
{
DblNode *Next;
DblNode *Prev;
int Data;
};



// Appends a node
void AppendNode(DblNode *curNode, DblNode* &fList, DblNode* &lList)
{
if (fList == NULL)
{
fList = curNode;
curNode->Prev = NULL;
}
else
{
lList->Next = curNode;
curNode->Prev = lList;
}
lList = curNode;
curNode->Next = NULL;
}

// Inserts a node into the list after pAfter

void InsertNode(DblNode* &curNode, DblNode* &pAfter, DblNode* &lList)
{
curNode->Next = pAfter->Next;
curNode->Prev = pAfter;
if (pAfter->Next != NULL)
pAfter->Next->Prev = curNode;
else
lList = curNode;
pAfter->Next = curNode;
}



void CreateList(DblNode* &fList, DblNode* &lList)
{
DblNode *curNode;
int pData;
cout << "Creating List...please input data\n";
for (;;)
{
cout << "Enter Data(-999 to quit): ";
cin >> pData;
if(pData==-999)break;
curNode = new DblNode;
curNode->Data = pData;
AppendNode(curNode, fList, lList);
}
cout << curNode;
}

void PrintList(DblNode* fListOne)
{
DblNode *curNode;
cout << "List:\n-----\n";
for (curNode = fListOne; curNode != NULL; curNode = curNode->Next)
{
cout << curNode->Data << endl;
}
cout << endl;
}
//Look, it's headache central!!!
void MergeLists(DblNode* &ResultList, DblNode* &fListOne, DblNode* &fListTwo)
{

ResultList = new DblNode;
DblNode *curResNode, *fcurNode, *scurNode;
curResNode = ResultList;
fcurNode = fListOne;
scurNode = fListTwo;
while(fcurNode->Next != NULL || scurNode->Next != NULL)
{
if(fcurNode->Data < scurNode->Data)
{
curResNode=fcurNode;
curResNode->Next=scurNode;
// curResNode->Next->Prev=curResNode;
}
else
{
// InsertNode(fcurNode, scurNode, lListOne);
}
fcurNode=fcurNode->Next;
scurNode=scurNode->Next;
curResNode = new DblNode;
}

}



void main()
{
DblNode *fListOne, *lListOne, *fListTwo, *lListTwo, *resList;
fListOne = NULL;
fListTwo = NULL;
lListOne = NULL;
lListTwo = NULL;
resList = NULL;
CreateList(fListOne, lListOne);
CreateList(fListTwo, lListTwo);
MergeLists(resList, fListOne, fListTwo);
PrintList(fListOne);
PrintList(fListTwo);
}
  Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
single linked list to double linked list (help) Countfog C Programming 8 04-29-2008 08:04 PM
Reverse function for linked list Brigs76 C++ Programming 1 10-25-2006 10:01 AM
How can I traverse a huffman tree carrja99 C++ Programming 3 04-28-2003 05:46 PM
Template Class for Linked List pecymanski C++ Programming 2 12-04-2001 09:07 PM
singly linked list clarinetster C Programming 2 08-26-2001 10:21 PM


All times are GMT -6. The time now is 02:42 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22