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;
}
}