doubly linked list trouble

Discussion in 'C' started by musicmancanora4, May 6, 2007.

  1. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    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;
    
    
    
    
    
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You have posted this in the articles section of C-C++ and its been quite some time you have been here and so you should not be making the same mistakes over and over again.
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I would recommend that you include the prototype for the function, any initialization that takes place in the function, the code for the structure, the statement that calls the function, and the definitions for the variables that serve as arguments in the call. That could prevent a lot of potentially wrong guesses. Help your respondents to help you by including relevant information. If I have to use my crystal ball, I charge more.
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I told you in the above response what you should provide if you want more help. Rather than do that, you have just replicated your insufficient post. I assure you that I can build a working doubly-linked list. I can also assure you that I'm not going to write your code for you, or guess at what unseen things you are doing.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    In fact if used the search feature he may had the solution as well because its already here.
     
  6. neothelord

    neothelord New Member

    Joined:
    Apr 25, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Dude, what are you doing with these statements.
    submenuPrevious = submenuCurrent;
    submenuCurrent->prevItem = submenuPrevious;
    submenuCurrent = submenuCurrent->nextItem;

    The second statement makes the previous ptr on the current node point to itself. there is also some logic missing in inserting at the head.

    I guess, in the while loop you are trying to find the position, to insert your submenu node. Then why are you altering the previous pointer of the node..
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice