// broken_inventory.c // // Written by YOUR-NAME (YOUR-ZID) // on TODAYS-DATE // // Manages a simple game inventory as a linked list of items. // The game keeps crashing! Find and fix the bugs below. #include #include #include #define MAX_NAME_LENGTH 20 struct item { char name[MAX_NAME_LENGTH]; int quantity; struct item *next; }; struct item *create_item(char *name, int quantity); struct item *append_item(struct item *inventory, char *name, int quantity); struct item *use_item(struct item *inventory, char *name); void print_inventory(struct item *inventory); void free_inventory(struct item *inventory); // ----------------------------------------------------------------------------- //////////////// DO NOT CHANGE THE MAIN FUNCTION /////////////////////////// int main(void) { struct item *inventory = NULL; inventory = append_item(inventory, "Potion", 3); inventory = append_item(inventory, "Sword", 1); printf("== Starting inventory ==\n"); print_inventory(inventory); printf("\nUsing the Potion.\n"); inventory = use_item(inventory, "Potion"); print_inventory(inventory); printf("\nUsing the Sword.\n"); inventory = use_item(inventory, "Sword"); print_inventory(inventory); free_inventory(inventory); return 0; } // --------------------------------------------------------------------------- // BUGGY FUNCTIONS - FIX THESE // --------------------------------------------------------------------------- // Creates a new item and returns a pointer to it. struct item *create_item(char *name, int quantity) { struct item new_item; strcpy(new_item.name, name); new_item.quantity = quantity; new_item.next = NULL; return &new_item; } // Adds a new item to the inventory and returns the updated head of // the inventory list. struct item *append_item(struct item *inventory, char *name, int quantity) { struct item *new_item = create_item(name, quantity); struct item *current = inventory; while (current->next != NULL) { current = current->next; } current->next = new_item; return inventory; } // Decreases the quantity of the item with the given name by 1. // If the quantity reaches 0, the item is removed from the inventory. struct item *use_item(struct item *inventory, char *name) { struct item *current = inventory; struct item *prev = NULL; while (current != NULL && strcmp(current->name, name) != 0) { prev = current; current = current->next; } current->quantity--; if (current->quantity <= 0) { free(current); if (prev == NULL) { inventory = current->next; } else { prev->next = current->next; } } return inventory; } // Free all items in the inventory. void free_inventory(struct item *inventory) { free(inventory); } // --------------------------------------------------------------------------- // PROVIDED FUNCTIONS - DO NOT CHANGE THESE // --------------------------------------------------------------------------- void print_inventory(struct item *inventory) { printf("====== Inventory ======\n"); struct item *current = inventory; if (current == NULL) { printf(" (empty)\n"); return; } while (current != NULL) { printf(" %s x%d\n", current->name, current->quantity); current = current->next; } }