CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2009
    Location
    Somewhere in Asia
    Posts
    36

    Unhappy Adding an element in Linked List

    Hello to all..

    I'm having a little problem with my program about linked list...when adding an element at the end of the list for a second time, a window that says that the visual c++ 6.0 had an error and needs to be close..i already thought lots of other ways to solve the problem but it just keeps happening again and again..

    i'll be posting the code here:

    Code:
     
    
    void FunctionAddData(int newData)
    {
    
    		node *currentPointer = NULL;
    		node *last = NULL;
    
    		currentPointer = root;
    
    		node *tempNode = new node;
    
    		cout<<"Enter a number: ";
    		cin>>tempNode -> data;
    
    		tempNode -> next = NULL;
    		
    		if(tempNode == NULL)
    			return;
    		else
    		{
    			tempNode -> next = NULL;
    
    			if(root == NULL)
    			{
    
    				root = tempNode;
    				last = tempNode;
    			}
    			else
    			{
    				last -> next = tempNode;
    				last = tempNode;
    			}
    		}
    
    	main();
    
    	return;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Adding an element in Linked List

    It's generally helpful to tell us what the error is and what line it occurred on.

    I see you're calling main directly which is something you should never do. I'm not ever sure why you'd want to in a function that adds a node to a list.

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Adding an element in Linked List

    Quote Originally Posted by gamer1127 View Post
    Code:
     
    		node *last = NULL;
    		// ...
    		if(root == NULL)
    		{
    			root = tempNode;
    			last = tempNode;
    		}
    		else
    		{
    			last -> next = tempNode;
    			last = tempNode;
    		}
    As last is NULL (you are never setting it to anything else), accessing last->next will crash your program.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Adding an element in Linked List

    Code:
    void FunctionAddData(int newData) // 'newData' is not used.
    {
    
            node *currentPointer = NULL;
            node *last = NULL;
    
            currentPointer = root;
    
            node *tempNode = new node;
    
            cout<<"Enter a number: ";
            cin>>tempNode -> data;
    
            tempNode -> next = NULL;
            
            if(tempNode == NULL) // If this was true then the previous two lines would have been 'undefined behaviour' (or crash)
                return;
            else
            {
                tempNode -> next = NULL; // You did that a few lines ago!
    
                if(root == NULL)
                {
    
                    root = tempNode; // I assume 'root' is some global
                    last = tempNode; // This is pointless as 'last' has only function scope and will 'disappear' on return.
                }
                else
                {
                    last -> next = tempNode; // 'last' has no value set here.
                    last = tempNode;
                }
            }
    
        main(); // 'main' should not be called.
    
        return;
    }
    What you should be doing is looping through the 'next' pointers starting at 'root' trying to find the last in the list and then adding your new node to this.

    Assuming C++, if this function were part of a link list class then the 'root' and 'last' nodes could be stored as private member variables.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured