I'm posting this again because the answer wasn't correct.
**Please read this carefully and the prompt carefully**
Hello All, I need help with Operations 4, 5, and 7. Make sure it's in C programming language.
Operations needed:
Operation 4 is Delete the entire list: When 4 is pressed, the program should delete each products
information by removing all the nodes in the Linked List, including the head node that was
created when you pressed 1.
Operation 5 is Search a product: When 5 is pressed, the program should take the users keyboard
input for the products name, search this product in the Linked List, and return the search result
(Found, or Not Found).
and Operation 7 is Purchase a product: When 7 is pressed, the program should take users
keyboard input for the products name, and increase this products quantity by 1.
I provided a code for what I have so far.
Code
#include
#include
#include
// define a product struct
typedef struct Product {
char name[20];
int price;
int quantity;
struct Product* next;
} Product;
// declare global variables
Product* head = NULL; // start of the linked list
Product* tail = NULL; // end of the linked list
// function prototypes
void createList();
void insertProduct();
void removeProduct();
void deleteList();
void searchProduct();
void displayProduct();
void purchaseProduct();
void sellProduct();
void saveToFile();
int main() {
char choice;
do {
printf("\n\nPlease choose an option:\n");
printf("1. Create a new list\n");
printf("2. Create a new product\n");
printf("3. Remove a product\n");
printf("4. Delete a list\n");
printf("5. Search a product\n");
printf("6. Display a product\n");
printf("7. Purchase a product\n");
printf("8. Sell a product\n");
printf("9. Save products to file\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case '1':
createList();
break;
case '2':
insertProduct();
break;
case '3':
removeProduct();
break;
case '4':
deleteList();
break;
case '5':
searchProduct();
break;
case '6':
displayProduct();
break;
case '7':
purchaseProduct();
break;
case '8':
sellProduct();
break;
case '9':
saveToFile();
break;
case '0':
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != '0');
return 0;
}
// function to insert a new product into the linked list
void insertProduct() {
Product* newProduct = (Product*) malloc(sizeof(Product));
printf("Enter the name of the product: ");
scanf("%s", newProduct->name);
printf("Enter the price of the product: ");
scanf("%d", &newProduct->price);
printf("Enter the quantity of the product: ");
scanf("%d", &newProduct->quantity);
if (newProduct->price <= 0 || newProduct->quantity <= 0) {
printf("Price and quantity must be positive integers. Product not added.\n");
free(newProduct);
return;
}
newProduct->next = NULL;
// check if the product already exists in the linked list
Product* current = head;
while (current != NULL) {
if (strcmp(current->name, newProduct->nam.
Im posting this again because the answer wasnt correct.Please .pdf
1. I'm posting this again because the answer wasn't correct.
**Please read this carefully and the prompt carefully**
Hello All, I need help with Operations 4, 5, and 7. Make sure it's in C programming language.
Operations needed:
Operation 4 is Delete the entire list: When 4 is pressed, the program should delete each products
information by removing all the nodes in the Linked List, including the head node that was
created when you pressed 1.
Operation 5 is Search a product: When 5 is pressed, the program should take the users keyboard
input for the products name, search this product in the Linked List, and return the search result
(Found, or Not Found).
and Operation 7 is Purchase a product: When 7 is pressed, the program should take users
keyboard input for the products name, and increase this products quantity by 1.
I provided a code for what I have so far.
Code
#include
#include
#include
// define a product struct
typedef struct Product {
char name[20];
int price;
int quantity;
struct Product* next;
} Product;
// declare global variables
Product* head = NULL; // start of the linked list
Product* tail = NULL; // end of the linked list
// function prototypes
void createList();
void insertProduct();
void removeProduct();
void deleteList();
void searchProduct();
void displayProduct();
void purchaseProduct();
2. void sellProduct();
void saveToFile();
int main() {
char choice;
do {
printf("nnPlease choose an option:n");
printf("1. Create a new listn");
printf("2. Create a new productn");
printf("3. Remove a productn");
printf("4. Delete a listn");
printf("5. Search a productn");
printf("6. Display a productn");
printf("7. Purchase a productn");
printf("8. Sell a productn");
printf("9. Save products to filen");
printf("0. Exitn");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case '1':
createList();
break;
case '2':
insertProduct();
break;
case '3':
removeProduct();
break;
case '4':
deleteList();
break;
case '5':
searchProduct();
break;
case '6':
displayProduct();
3. break;
case '7':
purchaseProduct();
break;
case '8':
sellProduct();
break;
case '9':
saveToFile();
break;
case '0':
printf("Exiting program.n");
break;
default:
printf("Invalid choice. Please try again.n");
break;
}
} while (choice != '0');
return 0;
}
// function to insert a new product into the linked list
void insertProduct() {
Product* newProduct = (Product*) malloc(sizeof(Product));
printf("Enter the name of the product: ");
scanf("%s", newProduct->name);
printf("Enter the price of the product: ");
scanf("%d", &newProduct->price);
printf("Enter the quantity of the product: ");
scanf("%d", &newProduct->quantity);
if (newProduct->price <= 0 || newProduct->quantity <= 0) {
printf("Price and quantity must be positive integers. Product not added.n");
free(newProduct);
return;
}
newProduct->next = NULL;
// check if the product already exists in the linked list
4. Product* current = head;
while (current != NULL) {
if (strcmp(current->name, newProduct->name) == 0) {
printf("Product already exists. Product not added.n");
free(newProduct);
return;
}
current = current->next;
}
// add the new product to the end of the linked list
if (head == NULL) {
head = newProduct;
tail = newProduct;
} else {
tail->next = newProduct;
tail = newProduct;
}
printf("Product added.n");
}
// function to remove a product from the linked list
void removeProduct() {
if (head == NULL) {
printf("No products in the list.n");
return;
}
char name[20];
printf("Enter the name of the product to remove: ");
scanf("%s", name);
Product* current = head;
Product* previous = NULL;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
if (current == head) {
head = current->next;
} else if (current == tail) {
previous->next = NULL;
5. tail = previous;
} else {
previous->next = current->next;
}
free(current);
// return after removing the product
printf("Product removed.n");
return;
}
previous = current;
current = current->next;
}
printf("Product not found.n");
}
// function to sell a product from the linked list
void sellProduct() {
if (head == NULL) {
printf("No products in the list.n");
return;
}
char name[20];
printf("Enter the name of the product to sell: ");
scanf("%s", name);
Product* current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
if (current->quantity == 0) {
printf("Product is out of stock.n");
}
else {
current->quantity--;
printf("Product sold. %d remaining.n", current->quantity);
}
return;
}
6. current = current->next;
}
printf("Product not found.n");
}
// function to save the products in the linked list to a file
void saveToFile() {
if (head == NULL) {
printf("No products in the list.n");
return;
}
char filename[20];
printf("Enter the filename to save to: ");
scanf("%s", filename);
FILE* file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file.n");
return;
}
Product* current = head;
while (current != NULL) {
fprintf(file, "%s %d %dn", current->name, current->price, current->quantity);
current = current->next;
}
fclose(file);
printf("Products saved to file.n");
}
void createList() {
// delete the current linked list, if it exists
deleteList();
printf("New list created.n");
}
void displayProduct() {
if (head == NULL) {
printf("No products in the list.n");
return;
}
7. printf("Product NametPricetQuantityn");
Product* current = head;
while (current != NULL) {
printf("%stt%dt%dn", current->name, current->price, current->quantity);
current = current->next;
}
}
void searchProduct() {
//**Please enter here**//
}
void purchaseProduct() {
//**Please enter here**//
}
void deleteList() {
//**Please enter here**//
} In this project, you will simulate a simple product management system using Linked Lists in
C. You need to create and maintain a Linked List that manages the current products' information.
Each product's information is stored as a node in this Linked List, including product's name, unit,
price and quantity. Product's name: The name of a product (e.g., water, milk, beef). It is a string.
Product's unit: The unit of a product (e.g., bottle, gallon, pound). It is a string. Product's price:
The price of a product (e.g., 3). It is of type int. Product's quantity: The quantity of a product
(e.g., 100). It is of type int. First, your program should have a menu for choices as follows. Also,
it should support the following operations. - Create an empty list: When ' 1 ' is pressed, an empty
list will be created. (For the implementation, you can create a Linked List with only one node as
the head.) This is the starting point of the program, before operations 29 can be executed. - Insert
a product: When ' 2 ' is pressed, the system should take user's keyboard input for a new product's
information (e.g., name, unit, price, quantity), create a new node, and add it into the Linked List.
- Delete a product: When ' 3 ' is pressed, the program should take user's keyboard input for the
product's name, and delete this product (the node) from the Linked List. - Delete the entire list:
When ' 4 ' is pressed, the program should delete each product's information by removing all the
nodes in the Linked List, including the head node that was created when you pressed ' 1 '. -
Search a product: When ' 5 ' is pressed, the program should take user's keyboard input for the
product's name, search this product in the Linked List, and return the search result (Found, or
Not Found). - Display products in the list: When ' 6 ' is pressed, every product in the Linked List
will be displayed in the screen, including its name, unit, price and quantity. - Purchase a product:
When ' 7 ' is pressed, the program should take user's keyboard input for the product's name, and
8. increase this product's quantity by 1.
- Sell a product: When ' 8 ' is pressed, the program should take user's keyboard input for the
product's name, and decrease this product's quantity by 1 . If this product's quantity becomes 0 ,
this product should be removed from the Linked List. - Save to File: When ' 9 ' is pressed, the
current products' information should be written to a file. Each product's information takes one
line in the file. - Exit: When ' 0 ' is pressed, the program exits. Assumptions: To simplify the
problem, we can have the following assumptions. - Product's name and unit are of types "string".
We can assume there are no blank spaces in these strings, e.g., a product's name cannot be "wa
ter". - Product's price and quantity are of types "int". We can assume the inputs from the user for
these two variables are integers, rather than other data types. - If "Save to File" is executed
multiple times, the latest information of products will be saved to a file (i.e., File overwrite
happens). - Product's name is the only identifier of a product and it is case-sensitive. In other
words, we simply assume "Water" and "water" are two different products. Robustness and Error
handling: The program must have the following considerations. - Operations 2 9 cannot be
executed if an empty list has not been created (i.e., Operation 1 should be started first). - For
"Insert a product" (when ' 2 ' is pressed), when a product's name is provided, the program should
search it among the existing products in the Linked List. If the product already exists in the
Linked List, then it cannot be inserted into the system. (A message should be printed instead.) -
For operations 3,7 and 8 , the program also needs to first search the product by its name. If the
product does not exist in the Linked List, these operations cannot proceed. (Some proper
messages should be printed instead.) - The program should keep running until ' 0 ' is pressed, if
the user provides the correct input. - For "Insert a product", the program should not allow
negative numbers or zeros for a product's price and quantity. (A message should be printed if the
user does provide negative numbers and zeros.) - For "Sell a product", if a product's quantity
becomes 0 after it is decreased by 1 , this product should be removed from the Linked List.