Help with Lists?, Please :)

This is a discussion on Help with Lists?, Please :) within the C Programming forums, part of the General Programming Boards category; Code: [//* Delete a list element */ void del( ListNodePtr *sPtr) { ListNodePtr previousPtr, currentPtr, tempPtr; char testvalue[30]; printf("Enter the ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    4

    Help with Lists?, Please :)

    Code:
     [//* Delete a list element */
    void del( ListNodePtr *sPtr)
    { 
       ListNodePtr previousPtr, currentPtr, tempPtr;
    
       char testvalue[30];
    
       printf("Enter the reg of the car you wish to delete:\n");
       scanf("%s",&testvalue);
    
       if(testvalue ==(*sPtr)->reg)
       {
    	   tempPtr=*sPtr;
    	   *sPtr = (*sPtr)->nextPtr;
    	   free(tempPtr);
       }
       else
       {
    	   previousPtr = *sPtr;
    	   currentPtr = (*sPtr)->nextPtr;
    	   
    
      /* if ( value == ( *sPtr )->data ) { 
          tempPtr = *sPtr;
          *sPtr = ( *sPtr )->nextPtr;  /* de-thread the node */
      /*    free( tempPtr );             /* free the de-threaded node */
       }
      // else { 
        //  previousPtr = *sPtr;
          //currentPtr = ( *sPtr )->nextPtr;
    
          while ( currentPtr != NULL) { 
             previousPtr = currentPtr;          /* walk to ...   */
           currentPtr = currentPtr->nextPtr;  /* ... next node */
          }
    
          if ( currentPtr != NULL ) { 
             tempPtr = currentPtr;
             previousPtr->nextPtr = currentPtr->nextPtr;
             free( tempPtr );
          }                                                        
       }
    
    //   return '\0';
    Hi I'm having trouble figuring out a delete function for my link list, have several fields in the structure, as I have it now it runs but doesn't delete it. I'm also interested in instering an update function but I'm not quite sure what the coding is like on it, any help would be greatly appreciated.Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Have you drawn a list out on paper?

    Create a "storyboard" of how all the links are updated when you delete a node. When you see how it's done, do exactlly the same thing in your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    I have, when I was doing the pseudocode, linking the temp into the list and putting the value into it and detaching then using free to delete it all together. Its just that this code is giving me a headache.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Anyone out there able to help me?...please!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while ( currentPtr != NULL) { 
             previousPtr = currentPtr;          /* walk to ...   */
           currentPtr = currentPtr->nextPtr;  /* ... next node */
          }
    What is it that you think this is doing? All this does is walk through the entire list and stop once it reaches the end. Nothing more. It just goes to the end, and when it gets there, 'currentPtr' is null.

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

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Code:
     if(testvalue ==(*sPtr)->reg)
    Hmm I think you would want to use strcmp() to compare the strings.
    The cost of software maintenance increases with the square of the programmer's creativity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 07:10 PM
  2. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 01:49 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM

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