The following (partial) code will display two linked lists followed by a menu requesting an action from the user. The user can insert and delete at will and when they are done, the program will redisplay both lists over and over. However, if "Merge" is selected, the lists are not displayed and an "Unhandeled Exception" error occurs. It appears that the merge is working okay because I can print a line showing the value being added in the append Node function, but if I print the the merged list after the merge operation is completed, it shows up blank. Afterwards, the screen refresh, which is supposed to redisplay the initial two lists, crashes. Somewhere, the nodes are being added to a phantom list and are not actually being added to the third list but I cannot figure out why! Please Help!
Code:
/***************************************************
* d r i v e r *
***************************************************/
// list[2] is the list to be merged!
case 'M': list->mergeList (list[0], list[1], list[2]);
list[2].displayList();
system ("pause");
break;
/***************************************************
* a p p e n d N o d e *
***************************************************/
template <class T>
void LinkedList<T>::appendNode (T num) {
ListNode *newNode, *nodePtr;
newNode = new ListNode;
newNode->value = num;
// Used to verify the append has a value to use!
// but where is the value being sent to???
cout << endl << "Append num " << newNode->value << endl;
newNode->next = NULL;
if (!head)
head = newNode;
else {
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
} // end appendNode
/***************************************************
* m e r g e L i s t *
***************************************************/
template <class T>
void LinkedList<T>::mergeList (LinkedList<T> list1, LinkedList<T> list2, LinkedList<T> listM)
{
ListNode *nodePtr1;
ListNode *nodePtr2;
// Reset each list to the beginning
nodePtr1 = list1.head;
nodePtr2 = list2.head;
while (nodePtr1 && nodePtr2) {
if (nodePtr1->value <= nodePtr2->value) {
listM.appendNode (nodePtr1->value);
nodePtr1 = nodePtr1->next; }
else {
listM.appendNode (nodePtr2->value);
nodePtr2 = nodePtr2->next; }
} // end while
// Search for remaining nodes in both lists:
while (nodePtr1) {
listM.appendNode (nodePtr1->value);
nodePtr1 = nodePtr1->next; }
while (nodePtr2) {
listM.appendNode (nodePtr2->value);
nodePtr2 = nodePtr2->next; }
system ("pause");
} // end mergeList
/***************************************************
* p r i n t *
***************************************************/
template <class T>
void LinkedList<T>::displayList() {
short num = 0;
ListNode *nodePtr;
nodePtr = head;
cout << "[" << setw(5);
while (nodePtr) {
// Print new line if number of elements is greater than 10
if ((++num % 10) == 0)
cout << "]" << endl << "[";
// Unhandled Exception error occurs on the next line and only
// happens after the merge is completed and the menu attempts
// to redisplay the two initial lists
cout << nodePtr->value;
nodePtr = nodePtr->next;
if (nodePtr)
cout << ", ";
} // end while nodePtr
cout << "]" << endl;
} // end of print