Remember to create a new directory in your repository for this lab. (Refer to previous labs for how if you have forgotten)
Review your notes about strings, structs, and linked lists.
During this warmup, you are going to implement several functions that exercise linked lists. Some of this code will be used in the homework.
You will be adding to these functions in the homework, so do not create warmup files - instead, place all of your code in hw6.h and hw6.c.
All of the linked list operations will hold a pointer to a product inventory record. rather than plain integers. The data declarations necessary for this assignment are below. These go in hw6.h, NOT hw6.c.
typedef unsigned int uint; enum Category { GROCERY=0, OFFICE, PHARMACY, HOBBY}; #define NUM_CATEGORIES 4 typedef struct { enum Category category; char name[40]; uint productID; uint current_stock; uint min_for_restock; uint max_after_restock; } product_info; // for the linked list, we will use the following struct typedef struct _node node; struct _node{ product_info *product; node *next; }; extern char *category_strings[]; void print_product(product_info *g, FILE *fp); void print_list(node *head, FILE *fp);
char *category_strings[] = { "Grocery", "Office", "Pharmacy", "Hobby"}; void print_product(product_info *p, FILE *fp) { fprintf(fp,"%s (%u) %s:",p->name, p->productID, category_strings[p->category]); fprintf(fp, "current: %u, min: %u, max: %u", p->current_stock, p->min_for_restock, p->max_after_restock); } void print_list(node *head, FILE *fp) { node *tmp; printf("Product Status:\n"); for(tmp = head; tmp != NULL; tmp = tmp->next) { print_product(tmp->product,fp); printf("\n"); } }
I have created an input file of a few products you can start with so you can see the format of the input file: products.txt. I have written a simple main program that reads in all fields of each line and prints them out in hw6_main.c. You will need to add code to take what it chopped into pieces and feed it to your create_product and other functions for your testing purposes. We will have our own test program.
Write a function that allocates memory and fills in the fields for a product. It returns a pointer to the newly allocated and initialized memory.
product_info *create_product(char *name, char *category, uint id, uint current, uint mn, uint mx);
Write a function that adds a product to the "head" or beginning of a list. The new first node of the list will point to pinfo. It returns a pointer to the first node in the modified list.
node* insert_head(node *head, product_info *pinfo);
Write a function that, given a productID, finds the record for the product and returns the pointer to that record.
product_info *find(node *head, uint pID);