loosing tail in doubly linked list

This is a discussion on loosing tail in doubly linked list within the C Programming forums, part of the General Programming Boards category; Hey guys sorry for the long extensive code but i am having some trouble with a delete function. It seems ...

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

    loosing tail in doubly linked list

    Hey guys sorry for the long extensive code but i am having some trouble with a delete function. It seems that when i delete a node in the middle its loosing the tail so its loosing the nodes which are underneath it.

    Im positive that it has nothing to do with my Delete funtion however i am very suspicious that i may not be loading the linked list properly in this function which is causing it. I cant find anything wrong when its inserting in between the list but i just want to make sure? Its suppose to work as a doubly linked list implementation


    Code:
    
         current = menu->headCategory;
    
        /* Find the right category for the item to be inserted */
        while(current != NULL)
        {
             /* If the right category has been found for the item to be
             inserted. */
             if(strcmp(current->categoryID,categoryIDcopy) == 0)
        {
        
        submenuCurrent = current->headItem;
    
       /* Increase the number of items when an item is added */
       
        current->numItems++;
    
       /* If nothing exists in the category, add to the head of the
       item list. */
    
       if(submenuCurrent == NULL)
       {
             submenuCurrent = submenuNode;
             current->headItem = submenuCurrent;
             submenuCurrent->nextItem = NULL;
             submenuCurrent->prevItem = NULL;
       }
       else
       {
             submenuPrevious = NULL;
             /* Find the right position to insert the item. */
             
            while((submenuCurrent != NULL) &&
            strcmp(submenuCurrent->itemName,
            submenuNode->itemName) < 0)
      {
            submenuPrevious = submenuCurrent;
            submenuCurrent->prevItem = submenuPrevious;
            submenuCurrent = submenuCurrent->nextItem;
      }
            /* If inserting at the head of the list. */
            if(submenuCurrent == current->headItem)
           {
               submenuNode->nextItem = submenuCurrent;
               submenuCurrent = submenuNode;
               current->headItem = submenuCurrent;
               submenuNode->prevItem = NULL;
           }
            /* If adding to the end of the list. */
           else if(submenuCurrent == NULL)
          {
               submenuPrevious->nextItem = submenuNode;
               submenuCurrent = submenuNode;
               submenuCurrent->nextItem = NULL;
               submenuNode->prevItem = submenuPrevious;
          }
            /* If adding somewhere in between the list. */
          else
          {
               submenuPrevious->nextItem = submenuNode;
               submenuNode->nextItem = submenuCurrent;
               submenuNode->prevItem = submenuPrevious;
               submenuCurrent->prevItem = submenuNode;
    
          }
      }
    }
              current = current->nextCategory;
    
    Last edited by bazzano; 05-06-2007 at 08:26 AM. Reason: indented and font size change

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    It might be easier if you could post indented 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
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I don't think anyone will examine your code in this state. You should indent it properly and increase the font size.

    Edit: Oops, a little too late :P
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Sorry guys its more readable now

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Can someone please take a look im struggling to find out where the bug is

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    This is a delete function right?

    I mean, what are these comments?
    /* Find the right position to insert the item. */
    /* If inserting at the head of the list. */
    /* If adding to the end of the list. */
    /* If adding somewhere in between the list. */

    Also, your indentation is still poor.

    Anyway, assuming you meant insert (for this looks like insert code, which is broken), here are some changes
    Code:
                if (submenuCurrent == NULL) {
                    submenuCurrent = submenuNode;
                    current->headItem = submenuCurrent;
                    submenuCurrent->nextItem = NULL;
                    submenuCurrent->prevItem = NULL;
                } else {
                    submenuPrevious = NULL;
                    /* Find the right position to insert the item. */
    
                    while ((submenuCurrent != NULL) &&
                           strcmp(submenuCurrent->itemName, submenuNode->itemName) < 0) {
                        submenuPrevious = submenuCurrent;
                        submenuCurrent = submenuCurrent->nextItem;
                    }
    
                    /* If inserting at the head of the list. */
                    if (submenuCurrent == current->headItem) {
                        submenuNode->nextItem = submenuCurrent;
                        submenuNode->prevItem = NULL;
                        current->headItem = submenuCurrent;
                    }
                    /* If adding to the end of the list. */
                    else if (submenuCurrent == NULL) {
                        submenuNode->nextItem = NULL;
                        submenuNode->prevItem = submenuPrevious;
                        submenuPrevious->nextItem = submenuNode;
                    }
                    /* If adding somewhere in between the list. */
                    else {
                        submenuNode->nextItem = submenuCurrent;
                        submenuNode->prevItem = submenuPrevious;
                        submenuPrevious->nextItem = submenuNode;
                        submenuCurrent->prevItem = submenuNode;
                    }
                }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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