#include <stdio.h> #include \"readline.h\" #include \"equipment.h\" int main(voi
ID: 3712926 • Letter: #
Question
#include <stdio.h>
#include "readline.h"
#include "equipment.h"
int main(void)
{
char code;
struct equipment *e_list = NULL;
printf("Operation Code: a for appending to the list, u for updating an equipment, p for printing the list; q for quit. ");
for (;;)
{
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code)
{
case 'a':
e_list = append_to_list(e_list);
break;
case 'u':
update(e_list);
break;
case 'p':
printList(e_list);
break;
case 'q':
clearList(e_list);
return 0;
default:
printf("Illegal code ");
}
printf(" ");
}
}
#include <stdio.h>
#include <stdlib.h>
#include "equipment.h"
#include <string.h>
#include "readline.h"
#define NAME_LEN 30
/*****************************************************
* append_to_list: allows user to input equipment type
* description and quantity. Then searches list and if
* already found does not append to list.
* **************************************************/
struct equipment *append_to_list(struct equipment *list)
{
struct equipment *new_node = (struct equipment *)malloc(sizeof(struct equipment));
printf("Enter equipment type: ");
read_line(new_node->type, NAME_LEN);
printf("Enter equipment Description: ");
read_line(new_node->description, NAME_LEN);
printf("Enter Equipment quantity: ");
scanf("%d", &new_node->quantity);
new_node->next = NULL;
if (list == NULL)
{
list = new_node;
}
else
{
struct equipment *temp = list, *prev;
while (temp != NULL)
{
if (strcmp(temp->type, new_node->type) == 0)
{
if (strcmp(temp->description, new_node->description) == 0)
{
printf("Equipment Already Exists ");
free(new_node);
return list;
}
if (strcmp(temp->description, new_node-> description) > 0)
{
if(strcmp(temp-> description, new_node ->description)) >0)
prev = temp;
temp = temp->next;
}
prev->next = new_node;
}
return list;
}
/***********************************************
* update: Allows user to update an item quantity
* that is already on the list
**********************************************/
void update(struct equipment *list)
{
char type[NAME_LEN + 1], descrip[NAME_LEN + 1];
int quantity;
printf("Enter Equipment type: ");
read_line(type, NAME_LEN);
printf("Enter Equipment Description: ");
read_line(descrip, NAME_LEN);
struct equipment *temp = list;
while (temp != NULL)
{
if (strcmp(temp->type, type) == 0 && strcmp(temp->description, descrip) == 0)
{
printf("Enter New equipment quantity: ");
scanf("%d", &quantity);
temp->quantity += quantity;
break;
}
temp = temp->next;
}
if (temp == NULL)
printf("Equipment Does not exist. ");
}
/*************************************************
* printlist: allows user to print all current
* items that have been added to list
************************************************/
void printList(struct equipment *list)
{
struct equipment *p = list;
printf(" TYPE DESCRIPTION QUANTITY ");
while (p != NULL)
{
printf("%-20s %-10s %-6d ", p->type, p->description, p->quantity);
p = p->next;
}
}
/*************************************************
* clearlist: Allows user to clear items in the
* list.
************************************************/
void clearList(struct equipment *list)
{
struct equipment *p = list, *prev = NULL;
while (p != NULL)
{
prev = p;
p = p->next;
free(prev);
}
}
struct equipment *delete_from_list(struct equipment *list)
{
char type[NAME_LEN + 1], descrip[NAME_LEN + 1];
printf("Enter Equipment type to be deleted: ");
read_line(type, NAME_LEN);
printf("Enter Equipment Description to be deleted: ");
read_line(descrip, NAME_LEN);
struct equipment *curr = list, *prev = NULL;
while (curr != NULL)
{
if (strcmp(curr->type, type) == 0 && strcmp(curr->description, descrip) == 0)
break;
prev = curr;
curr = curr->next;
}
if (curr == NULL)
printf("Equipment does not exist. ");
if (curr != NULL)
{
if(curr == list)
list=list->next;
else
prev->next = curr->next;
free(curr);
}
return list;
}
Project 11, Program Design 1. (70 points) Modify project 10 by adding and modifying the following functions: 1) Add a delete function in equipment.c that delete an equipment from the list. The function should delete an equipment by its type and description. Type and description will be entered by the user. The function should have the following prototype struct equipment* delete_from_list (struct equipment list) You will also need to add the function prototype to the header file; modify the main function in group_equip.c to add 'd' for delete option in the menu and it calls the delete function when the 'd' option is selected. 2) Modify the append to list function so an equipment is inserted into an ordered list (by type, then by description) and the list remains ordered after the insertion. For example, dumbbeiï should be before stability ball in the list; stability ball, large should be before stability ball, small in the list.Explanation / Answer
You have provided the code but not told what exactly you need. The code already has both the functions implemented. The delete_from_list() appears to be perfect.
But the append_to_list() to insert in sorted order seemed to have some issues. I have fixed the append_to_list() function.
Since you have not provided the other header files, I could not test the implementation.
Please use the following code in place of your append_to_list() and let me know if any issues, I'll help
/*****************************************************
* append_to_list: allows user to input equipment type
* description and quantity. Then searches list and if
* already found does not append to list.
* **************************************************/
struct equipment *append_to_list(struct equipment *list)
{
struct equipment *new_node = (struct equipment *)malloc(sizeof(struct equipment));
printf("Enter equipment type: ");
read_line(new_node->type, NAME_LEN);
printf("Enter equipment Description: ");
read_line(new_node->description, NAME_LEN);
printf("Enter Equipment quantity: ");
scanf("%d", &new_node->quantity);
new_node->next = NULL;
if (list == NULL)
{
list = new_node;
}
else
{
struct equipment *curr = list, *prev = NULL;
while(curr != NULL)
{
if(strcmp(curr->type, new_node->type) == 0)
{
if(strcmp(curr->description, new_node->description) == 0)
{
printf("Equipment Already Exists ");
free(new_node);
return list;
}
else if(strcmp(curr->description, new_node->description) > 0)
{
break;
}
}
else if(strcmp(curr->type, new_node->type) > 0)
{
break;
}
prev = curr;
curr = curr->next;
}
new_node->next = curr;
if(curr == list)
list = new_node;
else
prev->next = new_node;
}
return list;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.