# Linked Lists in C Homework Sample

Using a linked list, write a program to provide inventory control for a small store. There are several categories of items, grocery, office, pharmacy and hobby. Items should have a minimum number before restocking, and a maximum which is the number to restock to. You need to write functions to create a restock list (after selling items, if the quantity falls below the restock amount), to find an item by name or id. For more C programming assignments contact us for a quote.

#### Solution:

main.c

#include
#include
#include
#include
#include “hw6.h”

product_info *create_product(char *name, char *category, uint id, uint current, uint mn, uint mx){
product_info *p = (product_info*) malloc(sizeof(product_info));
strcpy(p->name, name);
if(strcmp(category, “Grocery”) == 0) p->category = GROCERY;
else if(strcmp(category, “Office”) == 0) p->category = OFFICE;
else if(strcmp(category, “Pharmacy”) == 0) p->category = PHARMACY;
else if(strcmp(category, “Hobby”) == 0) p->category = HOBBY;
p->productID = id;
p->current_stock = current;
p->min_for_restock = mn;
p->max_after_restock = mx;
return p;
}

node* n = (node*) malloc(sizeof(node));
n->product = pinfo;
return n;
}

while(n != NULL){
if(n->product->productID == pID) return n->product;
n = n->next;
}
return NULL;
}

p->current_stock = p->max_after_restock;
}

p->current_stock–;
}

if(n == NULL){
}
//if new product id is smaller than head
if(n->product->productID > pinfo->productID){
node* nw = (node*) malloc(sizeof(node));
nw->product = pinfo;
return nw;
}
while(n != NULL){
if(n->product->productID > pinfo->productID){
node* nw = (node*) malloc(sizeof(node));
nw->product = pinfo;
nw->next = n;
prev->next = nw;
}
n = n->next;
}
else{
prev = prev->next;
n = n->next;
}
}
if(n == NULL){
node* nw = (node*) malloc(sizeof(node));
nw->product = pinfo;
nw->next = NULL;
prev->next = nw;
}
}

//categorize comparison
bool compare(product_info* n1, node* n2){
if(n1->category == n2->product->category){
return (n1->productID < n2->product->productID);
}
else if(n1->category > n2->product->category){
return false;
}
else{
return true;
}
}

if(n == NULL){
}
node* nw = (node*) malloc(sizeof(node));
nw->product = pinfo;
return nw;
}
while(n != NULL){
if(compare(pinfo, n)){
node* nw = (node*) malloc(sizeof(node));
nw->product = pinfo;
nw->next = n;
prev->next = nw;
}
n = n->next;
}
else{
prev = prev->next;
n = n->next;
}
}
if(n == NULL){
node* nw = (node*) malloc(sizeof(node));
nw->product = pinfo;
nw->next = NULL;
prev->next = nw;
}
}

node* restock = NULL;
while(n != NULL){
if(n->product->current_stock < n->product->min_for_restock){
}
n = n->next;
}
return restock;
}

node* n = restocked_list;
while(n != NULL){
p->current_stock = p->max_after_restock;
n = n->next;
}
}

int main(int argc, char *argv[])
{
// make sure there are two arguments – hw5 and filename
if (argc < 2)
{
printf(“Usage: ./hw6 filename. Missing filename\n”);
exit(1);
}
char *filename = argv;

FILE *fp = fopen(filename, “r”);
if (fp == NULL)
{
printf(“Could not open file %s for reading\n”,filename);
exit(2);
}

// read in each record from the file
char buffer[BUFSIZ];
while (!feof(fp) && (fgets(buffer,500,fp) != NULL))
{
// parse the line to split up the fields
char *category;
char *name;
uint pID;
uint current_stock;
uint min_stock;
uint max_stock;
char *tmp_s;

// first field is category
category = strtok(buffer,” ,\n\r”);
// second field is name
name = strtok(NULL,” ,\n\r”);
// third is pID;
tmp_s = strtok(NULL,” ,\n\r”);
pID = atoi(tmp_s);
// fourth is current
tmp_s = strtok(NULL,” ,\n\r”);
current_stock = atoi(tmp_s);
// fifth is min
tmp_s = strtok(NULL,” ,\n\r”);
min_stock = atoi(tmp_s);
// sixth is max
tmp_s = strtok(NULL,” ,\n\r”);
max_stock = atoi(tmp_s);

printf(“Read in record: %s, %s, %u, %u, %u, %u\n”,
category, name, pID, current_stock, min_stock,
max_stock);

// now you need to write the code to create a record
// and put that record into the linked list
// product_info* p = create_product(name, category, pID, current_stock, min_stock, max_stock);

}

}

hw6.h

#include
#include

typedef unsigned int uint;
enum Category { GROCERY=0, OFFICE, PHARMACY, HOBBY};
#define NUM_CATEGORIES 4
typedef struct {
enum Category category;
char name;
int 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);

hw6.c

#include “hw6.h”

char *category_strings[] = { “Grocery”, “Office”, “Pharmacy”, “Hobby”};

void print_product(product_info *p, FILE *fp) {
printf(“%s (%u) %s:”,p->name, p->productID, category_strings[p->category]);
printf(“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”);
}
}