Partially working function - linked list

This is a discussion on Partially working function - linked list within the C++ Programming forums, part of the General Programming Boards category; Thanks to Zuk for helping me past the sticking point yesterday, but now that I've modified the code a little ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    Partially working function - linked list

    Thanks to Zuk for helping me past the sticking point yesterday, but now that I've modified the code a little to better fit the program requirements, I've run into problems. I stopped the code from simply advancing to the next node in the list for adding and modifying the info portions, and tried to make it ask the user which particular node he/she wants to add/modify. The outcome was that it would only add the first node, no matter which desired node number the user input, and the same with modifying the list thereafter. I got to thinking, and came up with the idea that it was because I hadn't created any nodes yet, so I decided to create a function that ran first in int main() called createEmptyList, which would initialize 5 nodes (length of list), with "" (empty) as the info portion of the nodes. The only problem with that is that I get the same old error, 'object reference not set to an instance of an object', which points to
    temp->nextPtr = new linkedListNode(node);
    . So, what is it I am doing wrong when I declare these new nodes? I never did find out what the problem was with my declarations. Relevant code below, entire .cpp file attached, if someone wants to look at it, don't want to post the whole thing here, would be lengthy and probably discourage posts.

    createEmptyList() function:
    __________________________________________________ ______
    Code:
    void linkedList::createEmptyList()
    {
    	char data[255]= ""; 
    	linkedListNode node(data);
    	linkedListNode *temp = new linkedListNode(node);
    	temp = headPtr;
    	for(int i = 1; i < 4; i++)
    	{
    		temp->nextPtr = new linkedListNode(node);  //boom!
    		temp = temp->nextPtr;
    	}
    }
    add() function:
    __________________________________________________ ____
    Code:
    void linkedList::add(const linkedListNode& node, int i) //add/replace blank/existing on line i text w/node->value
    {
    	if(headPtr == 0) //if empty
    	{
    		headPtr = new linkedListNode(node); //node = head
    		tailPtr = headPtr;//node also = tail
    	}
    	else //if not empty
    	{
    		linkedListNode *current = new linkedListNode(node); //create a tmp node with values of 'node' passed to function
    		current = headPtr; //start at the head of the list
    		static int count = 0; //set static counter to 0
    		while(current->nextPtr) //while nextPtr (next element) exists
    		{  
    			count++; //increment counter (start at 1, since first element = 1)
    		    if(count == i) //if line number == counter
    			{
    				break; //break out of the loop, because this is the line number we want to insert/overwrite
    			}
    			else // if (count != i)
    			{
    			current = current->nextPtr; //move on down the list, one element at a time
    			}
    		}
            int j; //new counter int
    		for(j = 0; j <= i-count; j++) //if sequence wasn't completed with i-loop
    		{
    		tailPtr->nextPtr = new linkedListNode(); //go on creating empty nodes???? should I do this?
    		tailPtr = tailPtr->nextPtr; //set tail = to new node just created at end
    		}
    	}
    }
    delete() function:
    __________________________________________________ __________
    Code:
    void linkedList::deleteNode()
    {
    	 int lineNum;
    	 cout << "Which line number would you like to delete?: " << endl;
    	 cin >> lineNum;
         cin.ignore();
         linkedListNode *pred, *tmp;
    	 if(lineNum == 1 && headPtr == tailPtr)
    	 {
    		 linkedListNode *tmp = headPtr;
    		 headPtr = tailPtr = 0;
    		 delete tmp;
    	 }
         int i;
    	 pred = headPtr;
    	 tmp = headPtr->nextPtr;
         for(i = 1; i < lineNum; i++)
         {
               pred = pred->nextPtr;
               tmp = tmp->nextPtr;
         }
         if(tmp != 0)
         {
                pred->nextPtr = tmp->nextPtr;
                if(tmp == tailPtr)
                tailPtr = pred;
                delete tmp;
         }
    }
    Attached Files Attached Files

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    void linkedList::createEmptyList()
    {
    	char data[255]= ""; 
    	linkedListNode node(data);         // create a node on the stack
    	linkedListNode *temp = new linkedListNode(node); // dynamically create a copy of node ???
    	temp = headPtr;                    // now you have overwritten the pointer to the dynamically created node
                                               // you cannot access it anyore --> memory leak
                                               // from the the name of the function I assume that the list is empty at this point 
                                               // so headPtr points to NULL
    	for(int i = 1; i < 4; i++)
    	{
    		temp->nextPtr = new linkedListNode(node);  // dereference NULL --->> boom!
    		temp = temp->nextPtr;
    	}
    }
    guess that function should look like this
    Code:
    void linkedList::createEmptyList()
    {
    	linkedListNode node("");         // create a node on the stack
    	for(int i = 1; i < 4; i++)       // put 3 copies into the list
               add(node);
    }
    Kurt
    Edit: I did not see that you modified the add member of the list.
    The whole idea behind that node class is that you do not allocate nodes with new. ( that's why there are copy constructor and assignement operator).
    Last edited by ZuK; 10-27-2006 at 09:13 AM.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    Thumbs up

    OK, so, then, correct me if I'm wrong here, but what I was doing wrong was: a backwards assignment of headPtr and the temp variable, (d'oh!) and I shouldn't have even been using the temp pointer for a new object, anyways(duh!). I should have created a new linkedListNode object and used the copy constructor to give it x's values. The pointers are for pointer walking only in this case, used to traverse the list to access using get() or to add, delete, etc. You know, now that I look at it, I feel kind of silly for not having seen what I did wrong, but on the other hand, I guess I shouldn't be too hard on myself, because we really only touched on these concepts: pointers, copy constructors/assignment operators, and just talked about them in the second C++ class, and now we are expected to use them in our projects, and they are all over the place in the text. Thanks again! I've got the program appending text to lines, deleting multiple lines, all that it's supposed to do. I'm learning a lot more here than in class! (distance learning is a crappy way to learn this stuff)

    -Patrick

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Guess you got it now. And you really should not be too hard on yourself. The implementation of this nodeclass is not really the way how it is usually done by beginners. Most examples would propably allocate the nodes dynamically and link them into the list. The advantage of this approach is that when using the list you don't have to deal with pointers so much reducing the risk of creating memory leaks.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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