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

A database of insurance records is maintained in a linked list. A link node is d

ID: 3850972 • Letter: A

Question

A database of insurance records is maintained in a linked list. A link node is declared by typedef struct node { char name[25];//name of the insured double premium;//premium amount } NODE: The following functions are to be written: NODE * inputRec(): Ask a user to enter the name and the premium of an insured, create a new storage for the record with malloc(), and return the pointer to the storage. When mallac() does not find enough storage, it returns NULL. Also, when -1 is entered for the premium value, inputRec() returns -1, indicating that no more data needs to be entered. void insert(NODE ptr, NODE *newN): To the exiting list pointed by ptr, save the new node pointed by newN as the new first node in the list. If newN is NULL, no new node is inserted. void traverse (NODE *ptr): Print name and premium of all the records in the list. The basic structure of the main() function looks like the following: int main(void) { NODE * start = NULL, newRec: newRec = inputRec() if (newRec = = NULL) return -1: start = newRec: while (newRec = NULL) { newRec = inputRec (): insert (start, newRec): traverse (start): } } include the following at the top of the program.

Explanation / Answer

Here is the code and output for the question. In case of any issues, please post a comment and I shall respond. Please don't forget to rate the answer if it helped. Thank you very much.

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef struct node

{

char name[25];

double premium;

struct node* next;

}NODE;

NODE* inputRec();

void insert(NODE* ptr, NODE* newN);

void traverse(NODE* ptr);

int main(void)

{

NODE *start = NULL, *newRec;

newRec = inputRec();

if(newRec == NULL) return -1;

start = newRec;

  

while(newRec != NULL)

{

newRec = inputRec();

insert(start, newRec);

if(newRec != NULL)

start = newRec;

traverse(start);

}

return 0;

}

NODE* inputRec()

{

char name[25];

double premium;

NODE *rec = NULL;

  

printf(" Enter name: ");

scanf("%s",name);

printf("Enter premium: ");

scanf("%lf", &premium);

  

if(premium == -1)

return NULL;

  

rec = (NODE*)malloc(sizeof(NODE));

if(rec != NULL)

{

strcpy(rec->name, name);

rec->premium = premium;

rec->next = NULL;

}

return rec;

}

void insert(NODE* ptr, NODE* newN)

{

   if(newN != NULL)

   {

   newN->next = ptr;

   }

}

void traverse(NODE* ptr)

{

printf(" =======");

while(ptr != NULL)

{

printf(" Name: %s Premium: %lf", ptr->name, ptr->premium);

ptr = ptr->next;

}

printf(" ======= ");

}

output


Enter name: John
Enter premium: 1000

Enter name: Alice
Enter premium: 900

=======
Name: Alice    Premium: 900.000000
Name: John    Premium: 1000.000000
=======


Enter name: Bob
Enter premium: 1500

=======
Name: Bob    Premium: 1500.000000
Name: Alice    Premium: 900.000000
Name: John    Premium: 1000.000000
=======


Enter name: Peter
Enter premium: 1200

=======
Name: Peter    Premium: 1200.000000
Name: Bob    Premium: 1500.000000
Name: Alice    Premium: 900.000000
Name: John    Premium: 1000.000000
=======


Enter name: none
Enter premium: -1

=======
Name: Peter    Premium: 1200.000000
Name: Bob    Premium: 1500.000000
Name: Alice    Premium: 900.000000
Name: John    Premium: 1000.000000
=======