+1 (812) 783-0640 

Well elaborated linked lists in C assignment sample

If your linked lists in C assignment give you a hard time, hire our experts for quality solutions. We are well experienced and will offer solutions that will guarantee you top grades. Our tutors work day and night to ensure that they deliver your work on time. The linked lists in C assignment sample provided below shows the quality of work we offer to students. If you are looking for a reliable team for your work, hire us today and enjoy the best solutions.

Linked Lists in C Assignment 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* insert_head(node *head, product_info *pinfo){
node* n = (node*) malloc(sizeof(node));
n->product = pinfo;
n->next = head;
return n;
}

product_info *find(node *head, uint pID){
node* n = head;
while(n != NULL){
if(n->product->productID == pID) return n->product;
n = n->next;
}
return NULL;
}

void record_restocked_single(node *head, uint pID){
product_info *p = find(head, pID);
p->current_stock = p->max_after_restock;
}

void product_sold(node *head, uint pID){
product_info *p = find(head, pID);
p->current_stock–;
}

node *add_sorted_productID(product_info *pinfo, node *head){
node* n = head;
node* prev = head;
if(n == NULL){
return insert_head(head, pinfo);
}
//if new product id is smaller than head
if(n->product->productID > pinfo->productID){
node* nw = (node*) malloc(sizeof(node));
nw->product = pinfo;
nw->next = head;
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;
return head;
}
if(n == head){
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;
return head;
}
}

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

node *add_sorted_category_ID(product_info *pinfo, node *head){
node* n = head;
node* prev = head;
if(n == NULL){
return insert_head(head, pinfo);
}
if(compare(pinfo, head)){
node* nw = (node*) malloc(sizeof(node));
nw->product = pinfo;
nw->next = head;
return nw;
}
while(n != NULL){
if(compare(pinfo, n)){
node* nw = (node*) malloc(sizeof(node));
nw->product = pinfo;
nw->next = n;
prev->next = nw;
return head;
}
if(n == head){
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;
return head;
}
}

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

void record_restocked_list(node *restocked_list, node *head){
node* n = restocked_list;
while(n != NULL){
product_info* p = find(head, n->product->productID);
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[1];

// open file for reading
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];
node* head = NULL;
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);
// head = add_sorted_category_ID(p, head);

}
// record_restocked_single(head, 23765);
// product_sold(head, 92656);
// product_sold(head, 92656);
// product_sold(head, 2653);
// product_sold(head, 2653);
// product_sold(head, 2653);
// node* r = make_restock_list(head);
// record_restocked_list(r, head);
// print_list(head, fp);

// here you can add stuff to test your other functions

}

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[40];
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);
void print_list(node *head, 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”);
}
}