Thread: doubly linked lists conversion

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

    doubly linked lists conversion

    Hey guys im trying to convert this singly linked list code into a doubly linked list could someone have a look at the code and see if its correct and maybe give some advice.

    Thankyou


    Code:
    typedef struct category* CategoryTypePtr;
    typedef struct item* ItemTypePtr;
    
    /* Structure definitions. */
    typedef struct price
    {
       unsigned dollars;
       unsigned cents;
    } PriceType;
    
    typedef struct item
    {
       char itemID[ID_LEN + 1];
       char itemName[MAX_NAME_LEN + 1];
       PriceType prices[NUM_PRICES];
       char itemDescription[MAX_DESC_LEN + 1];
       ItemTypePtr nextItem;
       ItemTypePtr prevItem;
       
    } ItemType;
    
    typedef struct category
    {
       char categoryID[ID_LEN + 1];
       char categoryName[MAX_NAME_LEN + 1];
       char drinkType;      /* (H)ot or (C)peold. */
       char categoryDescription[MAX_DESC_LEN + 1];
       CategoryTypePtr nextCategory;
       CategoryTypePtr prevCategory;
       ItemTypePtr headItem;
       unsigned numItems;
    } CategoryType;
    
    typedef struct gjc
    {
       CategoryTypePtr headCategory;
       unsigned numCategories;
    } GJCType;




    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;
                   }
                   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;
                      }
    
                      /* If adding to the end of the list. */
                      else if(submenuCurrent == NULL)
                      {
                         submenuPrevious->nextItem = submenuNode;
                         submenuCurrent = submenuNode;
                         submenuCurrent->nextItem = NULL;
                      }
    
                      /* If adding somewhere in between the list. */
                      else
                      {
                         submenuPrevious->nextItem = submenuNode;
                         submenuNode->nextItem = submenuCurrent;
                      }
                   }
                }
                current = current->nextCategory;
             }
          }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In other words, "I didn't write the single linked list code, so I have no idea how linked lists work, so I just stuck "->prev" all over the place. Can you fix it for me?" If you actually did write it yoursef, then just make some functions that print your list backwards and forewards. If it prints everything you've input, you're probably doing it right.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 01:01 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM