Linked List: Unhandled Exception

This is a discussion on Linked List: Unhandled Exception within the C++ Programming forums, part of the General Programming Boards category; The following (partial) code will display two linked lists followed by a menu requesting an action from the user. The ...

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Linked List: Unhandled Exception

    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
    Last edited by wbeasl; 04-15-2004 at 10:19 AM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    Use your debugger or post some code that compiles so we can use our debugger.

    gg

  3. #3
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36
    I figured it out. It appears I was passing a reference to all three lists of the array using the "->" as in "list->", and then passing all three lists of the array as values in a parameter list:

    list->mergeList (list[0], list[1], list[2]);

    This does not make sense. Instead I passed just the array itself referenced by the single merge list:

    listM->mergeList(list);

    Also, the mergeList member function needed a pointer reference (see below) and not a value reference.

    Code:
    //Driver:
    
    listM->mergeList(list);
    
    /**********************************************************************
     *                          m e r g e L i s t                         *
     **********************************************************************/
    template <class T>
    void const LinkedList<T>::mergeList (LinkedList<T> *list)   {
    
    	ListNode *nodePtr1 = list[0].head;
    	ListNode *nodePtr2 = list[1].head;
    	
    	sizeOfMerge = 0;
    
    	while (nodePtr1 && nodePtr2) {
    
            if (nodePtr1->value <= nodePtr2->value)  {
    			this->appendNode (nodePtr1->value);		
    			nodePtr1 = nodePtr1->next;   }
    		else  {
    			this->appendNode (nodePtr2->value);
    			nodePtr2 = nodePtr2->next;  }
    		
    		sizeOfMerge++;  // count of size of the new list
    
    	} // end while
    
    	// Search for remaining nodes in both lists:
    	while (nodePtr1)  {
    		this->appendNode (nodePtr1->value);	
    		sizeOfMerge++;
    		nodePtr1 = nodePtr1->next;  }
    
    	while (nodePtr2)   {
    		this->appendNode (nodePtr2->value);
    		sizeOfMerge++;
    		nodePtr2 = nodePtr2->next;  }
    
    }  // end mergeList

  4. #4
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36
    I figured it out and posted it. Thanks for checking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 12:13 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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