I am woundring if you can help me with this program Write a C program that asks
ID: 3800036 • Letter: I
Question
I am woundring if you can help me with this program Write a C program that asks the user for information about a particular collection of things that they own (for example: CDs, clothes, books, videos). For each item in the collection, ask for the name of the item, one other characteristics of the item, and the money value of the item. Store the information in a linked list of structures using the structure below (replace the xxx’s with the name of your structure; replace yyy and zzz too). After getting all the data in the linked list, print out the information in a table format. At the end, print the total number of items and the sum of the values of the items.
Also i need a to know how did you do it so i can understand
Explanation / Answer
Hi, below is the complete code for item catelogue.
it is working fine with gcc version 4.8.4.
do comment if you want more explanation.
/*
* C Program to Implement Item catalogue using linked list.
* author : vishal patel
* program tested on gcc version 4.8.4 ubuntu
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
/*
below structure will be used in link list to linking one node to another
*/
struct item
{
char name[MAX];
char companyName[MAX];
int price;
struct item *next;
};
// creating name for structure
typedef struct item item;
// these variables will be used by addItem and display method
item *newItem, *ptr, *temp;
item *first = NULL, *last = NULL;
/*
* Creating Item
*/
item* createItem(char name[], char companyName[], int price)
{
// first allocated memory to newItem which is variable of type item structure
newItem = (item *)malloc(sizeof(item));
if (newItem == NULL)
{
// if os unable to allocated memory
printf(" Memory was not allocated");
return 0;
}
else
{
// used strcpy method because we want to copy one string into char[](string)
strcpy(newItem->name, name);
strcpy(newItem->companyName, companyName);
newItem->price = price;
newItem->next = NULL;
return newItem;
}
}
/*
* Inserting item
*/
void addItem()
{
char name[MAX];
char companyName[MAX];
int price;
printf(" Enter the Name of item: ");
scanf("%s",name);
printf(" Enter the CompanyName for item: ");
scanf("%s",companyName);
printf(" Enter the price for item: ");
scanf("%d", &price);
newItem = createItem(name,companyName,price);
// check if first & lst node is null, that means first and last node will gonna be same as newItem.
// also update it's next pointer to NULL as there is only one node in linked list
if (first == last && first == NULL)
{
first = last = newItem;
first->next = NULL;
last->next = NULL;
}
else
{
// if linked list already having nodes then
temp = first;
first = newItem;
first->next = temp;
}
printf(" ----INSERTED----");
}
void display()
{
if (first == NULL)
{
printf("No Items in the catelog to display ");
}
else
{
// sun variable will store the total sun and count variable will store the count of items in list
int sum = 0;
int count = 0;
printf(" -------------------------START OF LISTING----------------------- ");
printf(" ItemName Company Name price ");
printf(" -------- ------------- ------ ");
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
printf(" %-10s %s %d ", ptr->name, ptr->companyName, ptr->price);
sum += ptr->price;
count++;
}
printf(" Total Items: %d Total Price: %d", count,sum);
printf(" -------------------------END OF LISTING----------------------- ");
}
}
int main()
{
int ch;
char ans = 'Y';
while (ans == 'Y'||ans == 'y')
{
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ");
printf(" Operations ");
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ");
printf(" 1. Add Item");
printf(" 2. Display Item List");
printf(" 0. Exit ");
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ");
printf(" Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf(" ...Adding item.... ");
addItem();
break;
case 2:
printf(" ...listing your items... ");
display();
break;
case 0:
printf(" ...Exiting... ");
return 0;
break;
default:
printf(" ...Invalid Choice... ");
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.