A question about node deletion in a linked list

This is a discussion on A question about node deletion in a linked list within the C Programming forums, part of the General Programming Boards category; I am reading a function that deletes a node from a simple linked list from the C how to program ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    27

    A question about node deletion in a linked list

    I am reading a function that deletes a node from a simple linked list from the C how to program fifth edition:
    Code:
    char delete( ListNodePtr *sPtr, char value )
    {
       ListNodePtr previousPtr; /* pointer to previous node in list */
       ListNodePtr currentPtr;  /* pointer to current node in list */
       ListNodePtr tempPtr;     /* temporary node pointer */
    
       /* delete first node */
       if ( value == ( *sPtr )->data ) {
          tempPtr = *sPtr; /* hold onto node being removed */
          *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
          free( tempPtr ); /* free the de-threaded node */
          return value;
       } /* end if */
       else {
          previousPtr = *sPtr;
          currentPtr = ( *sPtr )->nextPtr;
    
          /* loop to find the correct location in the list */
          while ( currentPtr != NULL && currentPtr->data != value){
             previousPtr = currentPtr;         /* walk to ...   */
             currentPtr = currentPtr->nextPtr; /* ... next node */
          } /* end while */
    
          /* delete node at currentPtr */
          if ( currentPtr != NULL ) {
             tempPtr = currentPtr;
             previousPtr->nextPtr = currentPtr->nextPtr;
             free( tempPtr );
             return value;
          } /* end if */
    
       } /* end else */
    
       return '\0';
    
    } /* end function delete */
    I focus to lines 24-30:
    Code:
     /* delete node at currentPtr */
          if ( currentPtr != NULL ) {
             tempPtr = currentPtr;
             previousPtr->nextPtr = currentPtr->nextPtr;
             free( tempPtr );
             return value;
          } /* end if */
    I think that the line:
    Code:
    tempPtr = currentPtr;
    that keeps a temporary copy of the currentPtr to release later the memory that this memory position has allocate is exaggeration. There is no need for this. We can refer to this position directly with currentPtr because currentPtr's value does not change from the statement before free() function. So I think that this is a better way:
    Code:
    /* delete node at currentPtr */
           if ( currentPtr != NULL ) {
              previousPtr->nextPtr = currentPtr->nextPtr;
              free( currentPtr );
              return value;
           } /* end if */
    Last edited by nonlinearly; 11-15-2011 at 04:43 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Your approach uses a few less characters, but the net effect is the same. I doubt there's any performance difference either way, so it's more a question of style rather than anything else. The advantage to your way is less typing, the advantage in the way it is written in that there are very clear parallels between the code for deleting the head and the code for deleting in the middle. Sometimes things which aid understanding and make no difference in performance are preferable to code which is slightly denser.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    Ok... I remind you that this book is for educational purposes only. So this kind of artistry is not recommended because some new to C (and maybe in programming) can lose valuable time trying to find meaning where none exists. Or even worse to think that is really necessary!!!
    thanks any way...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List, node creation question
    By Alec0905 in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2010, 09:45 AM
  2. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  3. Linked List Node Deletion based on Data
    By gvkalra in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2008, 08:15 AM
  4. Deletion in linked list
    By jeya in forum C Programming
    Replies: 1
    Last Post: 07-24-2008, 12:36 AM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

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