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

1) Add a delete function in equipment.c that delete an equipment from the list.

ID: 3713255 • Letter: 1

Question

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, dumbbell should be before stability ball in the list; stability ball, large should be before stability ball, small in the list.

equipment.c

#include "equipment.h"

#include "readline.h"

struct equipment *append_to_list(struct equipment *list)

{

struct equipment *new = (struct equipment *)malloc(sizeof(struct equipment));

printf("Enter type: ");

read_line(new->type, NAME_LEN);

printf("Enter Description: ");

read_line(new->description, NAME_LEN);

printf("Enter quantity: ");

scanf("%d", &new->quantity);

new->next = NULL;

if (list == NULL){

list = new;

}

else{

struct equipment *temp = list, *prev;

while (temp != NULL)

{

if (strcmp(temp->type, new->type) == 0 && strcmp(temp->description, new->description) == 0)

{

printf("Already Existed ");

free(new);

return list;

}

prev = temp;

temp = temp->next;

}

prev->next = new;

}

return list;

}

void update(struct equipment *list)

{

char type[NAME_LEN + 1], desc[NAME_LEN + 1];

int quantity;

printf("Enter type: ");

read_line(type, NAME_LEN);

printf("Enter Description: ");

read_line(desc, NAME_LEN);

struct equipment *temp = list;

while (temp != NULL)

{

if (strcmp(temp->type, type) == 0 && strcmp(temp->description, desc) == 0)

{

printf("Enter quantity: ");

scanf("%d", &quantity);

temp->quantity += quantity;

break;

}

temp = temp->next;

}

if (temp == NULL)

printf("Equipment Doesn't exist. ");

}

void printList(struct equipment *list)

{

struct equipment *p = list;

while (p != NULL)

{

printf("TYPE: %s DESCRIPTION: %s Quantity: %d ", p->type, p->description, p->quantity);

p = p->next;

}

}

void clearList(struct equipment *list)

{

struct equipment *p = list, *prev = NULL;

while (p != NULL)

{

prev = p;

p = p->next;

free(prev);

}

}

group_equip.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.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(" ");

}

}

Explanation / Answer

// Solution

//equipment.c

#include "equipment.h"

#include "readline.h"

//Function to delete an equipment by its type and description.

struct equipment* delete_from_list(struct equipment *list)

{

              

               char type[NAME_LEN + 1], desc[NAME_LEN + 1];

              

               //Read type from user

              

               printf("Enter type: ");

    read_line(type, NAME_LEN);

   

    //To read description from user

               printf("Enter Description: ");

    read_line(description, NAME_LEN);

   

   

               struct equipment *temp = list, *prev=list;

              

               //Search the list for the type and description read from user

    while (temp != NULL)

    {

       //if match occurs with current node

       if (strcmp(temp->type, type) == 0 && strcmp(temp->description, description) == 0)

        {

              

        if(temp==list) // if equipment is in starting node

       

        {

              

               list=temp->next;

              

               free(temp);

              

               printf("Equipment is deleted");

       

               break;

                              }

                             

                              // if equipment is in some other node

        prev->next=temp->next;

       

        free(temp);

       

        printf("Equipment is deleted");

       

        break;

        }

       

        //if no match with current node, move to next node

       

        prev= temp;

        

        temp = temp->next;

       

    }

   

    //if no match in entire list or if the list is empty

    if (temp == NULL)

               printf("Equipment Doesn't exist. ");

              

              

    return list;

}

//Function to (make insertion in) ordered list

struct equipment *append_to_list(struct equipment *list)

{

struct equipment *new = (struct equipment *)malloc(sizeof(struct equipment));

printf("Enter type: ");

read_line(new->type, NAME_LEN);

printf("Enter Description: ");

read_line(new->description, NAME_LEN);

printf("Enter quantity: ");

scanf("%d", &new->quantity);

new->next = NULL;

if (list == NULL){

list = new;

}

else

{

struct equipment *temp = list, *prev=list;

while (temp != NULL)

{

   if (strcmp(temp->type, new->type) > 0 && strcmp(temp->description, new->description) >0)

   {

      //if new node is orderly smaller than the starting node,

      //then attach the new node before the starting node

      //and make new node as starting node

     if(temp==list)

     {

              

               new->next=list;

              

               list=new;

               }

              

               //if new node is orderly smaller than any other node,

     //then insert the new node before that node

              

               prev->next=new;

              

               new->next=temp;

              

     free(new);

     return list;

   }

   //if not smaller than the current node, then move to the next node

   prev = temp;

   temp = temp->next;

}

   // if not smaller than all nodes in the list, then attach it at the end of the list

   prev->next = new;

}

return list;

}

void update(struct equipment *list)

{

char type[NAME_LEN + 1], desc[NAME_LEN + 1];

int quantity;

printf("Enter type: ");

read_line(type, NAME_LEN);

printf("Enter Description: ");

read_line(desc, NAME_LEN);

struct equipment *temp = list;

while (temp != NULL)

{

if (strcmp(temp->type, type) == 0 && strcmp(temp->description, desc) == 0)

{

printf("Enter quantity: ");

scanf("%d", &quantity);

temp->quantity += quantity;

break;

}

temp = temp->next;

}

if (temp == NULL)

printf("Equipment Doesn't exist. ");

}

void printList(struct equipment *list)

{

struct equipment *p = list;

while (p != NULL)

{

printf("TYPE: %s DESCRIPTION: %s Quantity: %d ", p->type, p->description, p->quantity);

p = p->next;

}

}

void clearList(struct equipment *list)

{

struct equipment *p = list, *prev = NULL;

while (p != NULL)

{

prev = p;

p = p->next;

free(prev);

}

}

////////////////////////////////////////////////////////////////////////////////

//group_equip.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#include "readline.h"

#include "equipment.h"

int main(void)

{

char code;

struct equipment *e_list = NULL;

printf("Operation Code: d for deleting from a list, 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){

//add ‘d’ for delete option in the menu and it calls the delete function when the ‘d’ option is selected.

case 'd': e_list = delete_from_list(e_list);

break;

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

}

}