Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A gym would like to maintain a list of the exercise equipment for its group fitn

ID: 3701361 • Letter: A

Question

A gym would like to maintain a list of the exercise equipment for its group fitness classes. Each exercise equipment was stored with the type, description (such as size or weight), and quantity. The program group_equip.c contains the equipment struct declaration, function prototypes, and the main function. Complete the function definitions so it uses a dynamically allocated linked list to store the equipment. Complete the following functions:
1. append_to_list: ask the user to enter an equipment’s type, description, and quantity (in the exact order), then add the equipment to the end of the linked list.
a. It should check whether the equipment has already existed by type and description. If so, the function should print a message and exit.
b. If the equipment does not exist, allocate memory for it, store the data, and append it to the end of the linked list.
c. If the list is empty, the function should return the pointer to the newly created equipment. Otherwise, add the equipment to the end of the linked list and return the pointer to the linked list.
2. update: update an equipment’s quantity. Ask the user to enter the equipment’s type and description, and quantity to be incremented. Find the matching equipment and update the quantity. If not found, print a message.
3. printList: print the type, description, and quantity of all equipment in the list.
4. clearList: when the user exists the program, all the memory allocated for the linked list should be deallocated.
Note: use read_line function included in the program for reading in type and description.

Group_equip.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#define NAME_LEN 30

struct equipment{

char type[NAME_LEN+1];

char description[NAME_LEN+1];

int quantity;

struct equipment *next;

};

struct equipment *append_to_list(struct equipment *list);

void update(struct equipment *list);

void printList(struct equipment *list);

void clearList(struct equipment *list);

int read_line(char str[], int n);

/**********************************************************

* main: Prompts the user to enter an operation code, *

* then calls a function to perform the requested *

* action. Repeats until the user enters the *

* command 'q'. Prints an error message if the user *

* enters an illegal code. *

**********************************************************/

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(" ");

}

}

struct equipment *append_to_list(struct equipment *list){

//add your code

return NULL;

}

void update(struct equipment *list)

{

//add your code

}

void printList(struct equipment *list){

struct equipment *p;

//add your code

}

void clearList(struct equipment *list)

{

//add your code

}

int read_line(char str[], int n)

{

int ch, i = 0;

while (isspace(ch = getchar()))

;

str[i++] = ch;

while ((ch = getchar()) != ' ') {

if (i < n)

str[i++] = ch;

}

str[i] = '';

return i;

}

Explanation / Answer

here is your code : ------------->>>>>>>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NAME_LEN 30
struct equipment{
char type[NAME_LEN+1];
char description[NAME_LEN+1];
int quantity;
struct equipment *next;
};
struct equipment *append_to_list(struct equipment *list);
void update(struct equipment *list);
void printList(struct equipment *list);
void clearList(struct equipment *list);
int read_line(char str[], int n);
/**********************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**********************************************************/
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(" ");
}
}
struct equipment *append_to_list(struct equipment *list){
//add your code
struct equipment *temp = (struct equipment *)malloc(sizeof(struct equipment));
printf(" Enter The Equipment type : ");
scanf("%s",&temp->type);
printf(" Enter The Description of Equipment : ");
scanf("%s",&temp->description);
printf(" Enter The Quantity of Equipment : ");
scanf("%d",&temp->quantity);
if(list == NULL){
  list = temp;
  temp->next = NULL;
  return list;
}
struct equipment *tt = list;
while(tt != NULL){
  if(strcmp(tt->description,temp->description) == 0 && strcmp(tt->type,temp->type) == 0){
   printf(" Equipment Already Exists !!! ");
   return list;
  }
  if(tt->next == NULL){
   break;
  }
  tt = tt->next;
}
tt->next = temp;
temp->next = NULL;
return list;
}
void update(struct equipment *list)
{
//add your code
if(list == NULL){
  return;
}
int tquan = 0;
struct equipment *temp = (struct equipment *)malloc(sizeof(struct equipment));
printf(" Enter The Equipment type : ");
scanf("%s",&temp->type);
printf(" Enter The Description of Equipment : ");
scanf("%s",&temp->description);
printf(" Enter The Quantity to update : ");
scanf("%d",&tquan);
struct equipment *tt = list;
while(tt != NULL){
  if(strcmp(tt->description,temp->description) == 0 && strcmp(tt->type,temp->type) == 0){
   tt->quantity = tt->quantity + tquan;
   free(temp);
   return;
  }
  tt = tt->next;
}
printf(" Not Found Equipmnet : ");
free(temp);
}
void printList(struct equipment *list){
struct equipment *p = list;
//add your code
while(p != NULL){
  printf(" Type = %s Description = %s Quantity = %d",p->type,p->description,p->quantity);
  p = p->next;
}
}
void clearList(struct equipment *list)
{
//add your code
struct equipment *temp = list;
struct equipment *prev = NULL;
while(temp != NULL){
  prev = temp;
  temp = temp->next;
  free(prev);
}
free(list);
}
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote