Write a program in C that creates the following struct: product name[50]; int qu
ID: 3911332 • Letter: W
Question
Write a program in C that creates the following struct:
product name[50];
int quantity;
Float cost;
Create the following menu system:
Add a Record
Display All Records
Quit
When the user selects (1) you will prompt them for product name, quantity, and cost. You will insert the record into the linked list in alphabetically order by name.
When the user selects (2) you will display all records currently in the list.
This program will not use an array, but it will use a linked list or structs. The program will allow for an unlimited number of records. Only limit will the amount of available ram
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[50];
int quantity;
float cost;
}product;
typedef struct node
{
product item;
struct node *next;
}node;
node * addToList(node *head, product p);
void printList(node *head);
int main()
{
int choice = 0;
product p;
node *head = NULL;
while(choice != 3)
{
printf("1. Add a record ");
printf("2. Display records ");
printf("3. Quit ");
printf("Your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter product name: ");
scanf("%s", p.name);
printf("Enter product quantity: ");
scanf("%d", &p.quantity);
printf("Enter product cost: ");
scanf("%f", &p.cost);
head = addToList(head, p);
break;
case 2:
printList(head);
break;
case 3:
break;
default:
printf("Invalid menu choice! ");
}
}
}
node * addToList(node *head, product p)
{
//adds in sorted order
node *prev = NULL, *curr = head;
node *n = (node*)malloc(sizeof(node));
n->item = p;
n->next = NULL;
if(head == NULL)
head = n;
else
{
while(curr != NULL && strcmp(n->item.name , curr->item.name) > 0)
{
prev = curr;
curr = curr->next;
}
n->next = curr;
if(prev == NULL)//insert as head node in linked list
head = n;
else
prev->next = n;
}
return head;
}
void printList(node *head)
{
node *curr = head;
printf("%-20s %-10s %-10s ", "Name", "Quantity", "Cost");
while(curr != NULL)
{
printf("%-20s %-10d %-10.2f ", curr->item.name, curr->item.quantity, curr->item.cost);
curr = curr->next;
}
printf(" ");
}
output
-----
1. Add a record
2. Display records
3. Quit
Your choice: 1
Enter product name: phone
Enter product quantity: 3
Enter product cost: 500
1. Add a record
2. Display records
3. Quit
Your choice: 1
Enter product name: belt
Enter product quantity: 10
Enter product cost: 5
1. Add a record
2. Display records
3. Quit
Your choice: 2
Name Quantity Cost
belt 10 5.00
phone 3 500.00
1. Add a record
2. Display records
3. Quit
Your choice: 1
Enter product name: shoes
Enter product quantity: 3
Enter product cost: 100
1. Add a record
2. Display records
3. Quit
Your choice: 1
Enter product name: laptop
Enter product quantity: 2
Enter product cost: 1000
1. Add a record
2. Display records
3. Quit
Your choice: 2
Name Quantity Cost
belt 10 5.00
laptop 2 1000.00
phone 3 500.00
shoes 3 100.00
1. Add a record
2. Display records
3. Quit
Your choice: 3
amoeba-2:Test1 raji$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.