Thread: in the middle delete list

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    in the middle delete list

    Hey guys im trying to delete the middle node in a doubly linked list but i seem to getting errors when i check for memory leaks.

    From what i have drawn im making submenuCurrent point in the middle then the previous pointer pointing to the left and next pointing to the node after submenuCurrent.

    I cant figure out what im doing wrong

    Code:
    do
       {
          printf("\nItem ID (5 characters): ");
          fgets(itemID, ID_LEN+EXTRA_SPACES,stdin);
          if(itemID[0] == '\n')
             return;
          valid = isValid(itemID);
    
          if(valid == TRUE)
          {
             current = menu->headCategory;
             previous = NULL;
    
             while(current != NULL)
             {
                submenuCurrent = current->headItem;
                submenuPrevious = NULL;
    
                /* If categoryID matches user's input. */
                if(strcmp(current->categoryID, input) == 0)
                {
                   while(submenuCurrent != NULL)
                   {
                      /* If itemID matches user's input. */
                      if(strcmp(submenuCurrent->itemID, itemID) == 0)
                      {
                         printf("\nMenu item '%s  %s' deleted from system.\n",
                         submenuCurrent->itemID, submenuCurrent->itemName);
    
                         /* If deleting at the head. */
                         if(submenuCurrent == current->headItem)
                         {
                            submenuNext = submenuCurrent;
                            submenuPrevious = submenuCurrent->nextItem;
                            free(submenuNext);
                            submenuNext = NULL;
                            current->headItem = submenuPrevious;
                            return;
                         }
    
                         /* If deleting at the end. */
                         else if(submenuCurrent->nextItem == NULL)
                         {
                            free(submenuCurrent);
                            submenuCurrent = NULL;
                            submenuPrevious->nextItem = NULL;
                            return;
                         }
    
                         /* If deleting in between the list. */
                         else
                         {
                           /* submenuNext = submenuCurrent;
                            submenuPrevious->nextItem = submenuCurrent->nextItem;*/
    
                            submenuPrevious = submenuCurrent->prevItem;
                            submenuNext = submenuCurrent->nextItem;
                            submenuPrevious->nextItem = submenuCurrent->nextItem;
                            submenuNext->prevItem = submenuCurrent->prevItem;
                            submenuNext->nextItem = NULL;
                            free(submenuCurrent);
                            submenuCurrent = NULL;
                            
                            
                            return;
                         }
                         break;
                      }
                      submenuPrevious = submenuCurrent;
                      submenuCurrent = submenuCurrent->nextItem;
                   }
                }
    
                previous = current;
                current = current->nextCategory;
             }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    remove this line
    Code:
    submenuNext->nextItem = NULL;
    Kurt

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Hmmm it got it down to two errors it shows no leaks but says that 32 out of 33 have been malloced im freeing the correct pointer aint i?

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    whats that meant to mean

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    what's with the colors man? ha
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    =P maybe the fact that your posts look like that ^^^
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  7. #7
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by quzah View Post
    Code:
    struct node *rem( struct node *n, type_t v )
    {
        struct node *r = n;
        while( r->v != v )
            r = r->next;
        if( r )
        {
            if( r->prev )
                r->prev->next = r->next;
            if( r->next )
                r->next->prev = r->prev;
            if( r == n )
                n = r->next;
        }
        return n;
    }
    Shouldn't the node *n be free'd at some point or do you take care of that outside of the function ? Or do you differentiate between remove, where you keep the node for further usage, and delete, where you get rid of the node for good ?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not to answer for quzah but I think that you've stated perfectly good reasons for him anyway. Code can be somewhat more usable if you don't assume what allocator or deallocator the programmer wants to use. I'm sure that you understood this, but ...yeah. Consider a list in a memory pool, or something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me debug this linked list !
    By Dark Angel in forum C Programming
    Replies: 6
    Last Post: 04-18-2008, 02:10 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM