LinkedList Error

This is a discussion on LinkedList Error within the C++ Programming forums, part of the General Programming Boards category; After your done adding the node set the pointer to NULL. Do a test at the beginning on the added ...

  1. #31
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    After your done adding the node set the pointer to NULL. Do a test at the beginning on the added node for NULL and return if True(or better yet throw an exception since you shouldn't be passing it NULL pointers to add). And you have no need to be embarassed
    Last edited by manofsteel972; 11-28-2004 at 06:33 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  2. #32
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you can accurately describe what you want to do, it can usually be done. Here are two options for what I think you might want to be do:
    Code:
    void Link::AddNode(Node *W){
      if(W != NULL)
      {
    	 Node * newNode = new Node;
    	 newNode->name = W->name;
    	 newNode = NULL;
     
    	 if(start == NULL)
    	   start = newNode;
    	 else
    	 {
    	   //if W has the same address or the same name as start don't add it to the list
    	   if(W != start && W->name != start->name)
    	   {
    		  newNode->next = start; 
    		  start = newNode;
    	   }
    	 }
       }
    }
     
    //Version two
    void Link::AddNode(Node *W){
       if(W != NULL)
       {
    	  //search list for W->name
    	  Node * current = start;
     
    	  while(current != NULL && current->name != start->name)
    		 current = current->name;
     
    	  if(current == NULL) //name not in list
    	  {
    		 //add name in W to front of list
    		 Node * newNode = new Node;
     
    		 newNode->name = W->name;
    		 newNode = NULL;
    		 
    		 if(start == NULL)
    			start = newNode;
    		 else
    		 {
    			newNode->next = start;
    			 start = newNode;
    		 }
    	   }
       }
    }
    Last edited by elad; 11-29-2004 at 06:48 AM.

Page 3 of 3 FirstFirst 123
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, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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