#include #include #include typedef struct itemTag item; /* inventory item record */ struct itemTag { char name[64]; /* item name */ int quantity; /* # of items in stock */ float cost; /* cost per item */ item *nextItem; /* pointer to the next item in the list */ }; /* add item to the list */ int AddItem(item **head, char *name, int q, float cost); /* delete item from the list (by name) */ int DeleteItem(item **head, char *name); /* insert item in alphabetical order */ int InsertItem(item **head, char *name, int q, float cost); /* compute total cost of the inventory */ float CalculateInventoryCost(item *head); /* print list */ void PrintList(item *head); /* delete list */ void FreeList(item **head); /* find item */ item *FindItem(item *head, char *name); int main(int argc, char *argv[]) { item *inventory = NULL; float totalCost = 0.0f; PrintList(inventory); InsertItem(&inventory, "part3", 10, 1.0f); InsertItem(&inventory, "part1", 1, 2.50f); InsertItem(&inventory, "part2", 5, 1.50f); PrintList(inventory); totalCost = CalculateInventoryCost(inventory); printf("Inventory cost %f\n\n", totalCost); printf("looking for item part3: %p\n\n", FindItem(inventory, "part3")); DeleteItem(&inventory, "part2"); InsertItem(&inventory, "part0", 4, 0.50f); DeleteItem(&inventory, "part3"); InsertItem(&inventory, "part4", 1, 4.50f); PrintList(inventory); totalCost = CalculateInventoryCost(inventory); printf("New Inventory cost %f\n\n", totalCost); FreeList(&inventory); PrintList(inventory); return 0; } int AddItem(item **head, char *name, int q, float cost) { item *new_item; /* allocate new item */ new_item = (item *)malloc(sizeof(item)); if (new_item == NULL) return 0; /* copy data */ strcpy(new_item->name, name); new_item->cost = cost; new_item->quantity = q; new_item->nextItem = NULL; /* attach it n front of the head */ new_item->nextItem = *head; *head = new_item; return 1; } int InsertItem(item **head, char *name, int q, float cost) { item *new_item; if ((*head == NULL) || (strcmp((*head)->name, name) > 0)) { /* found spot to insert */ /* allocate new item */ new_item = (item *)malloc(sizeof(item)); if (new_item == NULL) return 0; /* copy data */ strcpy(new_item->name, name); new_item->cost = cost; new_item->quantity = q; new_item->nextItem = NULL; /* attach it n front of the head */ new_item->nextItem = *head; *head = new_item; return 1; } return InsertItem(&(*head)->nextItem, name, q, cost); } /* delete item by its name */ int DeleteItem(item **head, char *name) { item *removed = NULL; /* if tail, there is nothign to delete */ if (*head == NULL) return 0; /* is this the right item to delete? */ if (strcmp((*head)->name, name) == 0) { /* if so, delete it */ removed = *head; *head = (*head)->nextItem; free(removed); return 1; } return DeleteItem(&(*head)->nextItem, name); } /* compute inventory */ float CalculateInventoryCost(item *head) { item *current_item = head; float totalCost = 0.0f; while (current_item != NULL) { totalCost += current_item->quantity * current_item->cost; current_item = current_item->nextItem; } return totalCost; } /* print list */ void PrintList(item *head) { item *current_item = head; int ic = 0; printf("Printing linked list:\n"); while (current_item != NULL) { printf("Item #%i @ %p: ", ic, current_item); printf("%s %d %f %p\n", current_item->name, current_item->quantity, current_item->cost, current_item->nextItem); current_item = current_item->nextItem; ic++; } printf("\n"); } /* delete list */ void FreeList(item **head) { item *removed = NULL; if (*head == NULL) /* nothing to delete */ return; removed = *head; *head = (*head)->nextItem; free(removed); FreeList(head); } /* find item */ item *FindItem(item *head, char *name) { if (head == NULL) /* does not exist */ return NULL; if (strcmp(head->name, name) == 0) return head; /* we found it! */ return FindItem(head->nextItem, name); }