Question

2) Modify the append_to_list function so an equipment is inserted into an ordered list (by type,...

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 beforestability ball, small in the list.

Here is the code:

struct equipment *append_to_list(struct equipment *list){

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

printf("\nEnter equipment type : ");

read_line(temp->type, NAME_LEN);

printf("\nEnter description: ");

read_line(temp->description, NAME_LEN);

printf("\nPlease enter quantity: ");

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

  

temp->next = NULL;

  

if(list == NULL){

list = temp;

}

else{

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

  

while (temp2 != NULL){

  

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

  

printf("Equipment already Exists\n");

free(temp);

return list;

}

  

prev = temp2;

temp2 = temp2->next;

}

prev->next = temp;

}

return list;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**** I have modified the program . In youe program you have added only one condition when equipment is already present . I have added two more condition when Equipment type is smaller tthan the elements of the list and second is when equipment type is same but equipment description is smaller . I hope this function helps. ****/

struct equipment *append_to_list(struct equipment *list){

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

printf("\nEnter equipment type : ");

read_line(temp->type, NAME_LEN);

printf("\nEnter description: ");

read_line(temp->description, NAME_LEN);

printf("\nPlease enter quantity: ");

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

temp->next = NULL;

if(list == NULL){

list = temp;

}

else{

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

while (temp2 != NULL){

// If the Equipment already present

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

  printf("Equipment already Exists\n");

  free(temp);
  return list;
}

// If the type is smaller
else if(strcmp(temp2->type, temp->type) > 0){
  if(prev == NULL){
   temp->next = temp2;
   list = temp;
  }
  else{
   prev->next= temp;
   temp->next = temp2;
  }
  return list;
}
// If the type is same then ordering on the basis of description
else if(strcmp(temp2->type, temp->type) ==0 && strcmp(temp2->description, temp->description) > 0){
  if(prev == NULL){
   temp->next = temp2;
   list = temp;
  }
  else{
   prev->next= temp;
   temp->next = temp2;
  }
  return list;
}

prev = temp2;

temp2 = temp2->next;
}

prev->next = temp;

}

return list;

}

Add a comment
Know the answer?
Add Answer to:
2) Modify the append_to_list function so an equipment is inserted into an ordered list (by type,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • this is i have code for double linked list with insert at sorted list. i have...

    this is i have code for double linked list with insert at sorted list. i have some error that stdout: ------- Error: compilation stderr: ------- InsertDouble.c: In function ‘list* InsertDouble(LIST, int)’: InsertDouble.c:51:14: error: cannot convert ‘list’ to ‘list*’ in assignment Currentptr = *head; // set a pointer which is current one ^ it keep give me this error i am not sure how to fix is anyone possible to help me? #include <stdio.h> #include <stdlib.h> typedef struct list {   ...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT