Thread: A question about node deletion in a linked list

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 05:43 AM.

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