Thread: Need Help on Linked List Prog!

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Question Need Help on Linked List Prog!

    Hello,
    Thank you for taking the time to read this. I am writing a function that will replace a user-entered value in a list. It will replace every single instance of that value with a new value. My problem is my program changes every user-entered value successfully except if that # is the last one in the list.

    I will post the 2 functions it depends on here because I think that's where the error is. The rest of the files including lab3.c containing main() are attachments to this thread. So here it is...

    Code:
    Position Find( ElementType X, List L, Position P )
            {   
       
            while( P != NULL && P->Element != X )
             P = P->Next;
    
             return P;
    
    Replace(ElementType old_val, ElementType new_val, List L)
    {	
    	 Position P = L->Next; // initialize Position to first position of list
    
    	 do {
    	  
    		P = Find(old_val, L, P); // find a position of the value to replace
    		                     // search from the value after the one replaced
    		
          P->Element = new_val;  // replace old value with a new value
    		P = P->Next;      // skip a spot to find other instances of same value
    
    	 }while (P != NULL );  // test if the next spot is a NULL and hence end of list
    
    }

    Note: the file containing main() is lab3.c

    I appreciate your time in taking a look at this. It really means a lot to me.

    Yev

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's the hard way, and the easy way. I prefer the easy way.
    Code:
    Node *find( Node *here, type value )
    {
        Node *n = here;
        while( n && n->value != value )
            n = n->next;
    
        return n;
    }
    
    int replace( Node **list, type value )
    {
        Node *n = list ? *list : NULL;
    
        while( n != NULL )
        {
            n = find( n, value );
            if( n )
            {
                n->value = value;
                n = n->next;
            }
        }
    }
    Of course, then there's the really easy way, using double linked lists if you really feel the need to do node replacement rather than value replacement.

    But really, is there any point to replacing the node? In single linked lists, I avoid node replacement at all costs. But then, I never use single linked lists.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Question Question on your code

    Hi,
    Thanks a lot for the help. It does work now, but I have a question. The if statement if (n) is what solved it, but I am not sure why this works.

    When it fails to find another value in the list, my code would try to write a new value to a nonexisting element of a NULL pointer, and that is not allowed. Am I correct in my thinking above??

    Thanks again for your time.

    Yev

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by YevGenius
    When it fails to find another value in the list, my code would try to write a new value to a nonexisting element of a NULL pointer, and that is not allowed. Am I correct in my thinking above??
    No when it checks if(p) it would return false if P is NULL.. so it wouldnt execute... so nothing would be tried to insert into the NULL pointer... and i dont see the use of us9ing two loops .. it can be done efficiently using one loop....


    Code:
    	while( n != NULL )
    	{
    
    			
    		 if(n->value==old_value)
    		  n->value = new_value;
    
       n = n->next;
    
     }

    the above code should be enough to do your operation...

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    actually, no.
    the if(n) is acutally checking to see whether your n is pointing to a NULL or not.
    if it is pointing to a NULL (which is commonly defined as 0), the if statment becomes false and doesn't do the replacing part. However, as long as n is pointing to an address, the if(n) will be true and it'll execute the replace part.

    I think that if(n) is actually not very good programing... It's a shorthand that works, but when people try too read your code, it gets abit harder. after all, when reading that line, people will go "if n? if n what?"
    IMHO, the better code for that is if(n != NULL). This makes it more readable in the sense that you know that n is probrably a pointer type and you want it to execute something when it's pointing at something.(It's also a good idea to make sure all your pointers are initialised with NULL...)

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by tzuchan
    actually, no.
    the if(n) is acutally checking to see whether your n is pointing to a NULL or not.
    if it is pointing to a NULL (which is commonly defined as 0), the if statment becomes false and doesn't do the replacing part. However, as long as n is pointing to an address, the if(n) will be true and it'll execute the replace part.

    I think that if(n) is actually not very good programing... It's a shorthand that works, but when people try too read your code, it gets abit harder. after all, when reading that line, people will go "if n? if n what?"
    IMHO, the better code for that is if(n != NULL). This makes it more readable in the sense that you know that n is probrably a pointer type and you want it to execute something when it's pointing at something.(It's also a good idea to make sure all your pointers are initialised with NULL...)
    I dont see anything wrong with if(p) where p is a pointer... and i agree its good to initilize it to NULL when its created since you might use it in your link list or something where it is pointing to some junk...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM