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

I started this project but i need to modify it toworks with the direction below

ID: 3615826 • Letter: I

Question

I started this project but i need to modify it toworks with the direction below
/* Mini-data base. Somewhat more than was asked for withprogram 10 */
#include <stdio.h> #include <stdlib.h>
#define nameSize 50 #define arraySize 250
/* Data for one person */ typedef struct {   int id, birthYear, height, weight, salary;   char firstName[nameSize],lastName[nameSize]; } Person;
/* The database: array of structs, arranged in order of id*/ Person data[arraySize];
#define bufferSize 132 char buffer[bufferSize]; /* input buffer */
/* Print nicely formatted data for one person */ void printRecord( Person *p ) {   printf("id: %03d birth: %04d height: %02d weight:%03d salary: %06d name: %s %s ",    p->id,p->birthYear,p->height,p->weight,      p->salary,p->firstName,p->lastName); }
/* Ask the user for the id of a person */ int getID() {   int id;      printf("id? (enter -1 to quit):");   gets( buffer );   while ( sscanf( buffer, " %d", &id ) != 1)   {    printf("re-enter id: ");    gets( buffer );   }   return id; }
void readData( FILE* input ) {   Person p; /* data from one input record*/   int j = 0; /* count of records read in*/   int flag; /* count of successfulconversions in scanf */
  /* Read in the File, copying data into the array*/   while ( fgets( buffer, bufferSize, input) )   {    j++ ;
   /* Read in the data */    flag = sscanf( buffer, "%d %d %d %d %d %s%s",    &p.id,&p.birthYear, &p.height, &p.weight,&p.salary,    p.lastName,p.firstName );
   if ( flag != 7 )    {    fprintf( stderr, "Data not formattedcorrectly: Record %d ", j );    fclose( input );    exit( EXIT_FAILURE );    }
   /* copy data into the database */    if ( p.id < arraySize )    {    if ( data[p.id].id != 0 )    fprintf( stderr, "Record %d:duplicate id %d; record skipped ", j, p.id );    else    data[p.id] = p;    }    else    fprintf( stderr, "Record %d: id %dis out of range; record skipped ", j, p.id );   } }
int main() {   FILE *input;   int j=0;   char fileName[bufferSize];
  /* Ask user for input file; open file */   printf("Name of input file: ");   gets( buffer );   sscanf( buffer, "%s", fileName );
  /* Open input file for reading */   if ( (input=fopen( fileName, "r"))==NULL )   {    perror("input file did not open ");    exit( EXIT_FAILURE );   }      /* Read in the data */   readData( input );      /* Process Queries until user enters -1 */   int id = 0 ;   id = getID();      while ( id != -1 )   {    /* Linear search */    for ( j=0; j<arraySize &&data[j].id < id; j++ );       if ( data[j].id == id )    printRecord( &data[j] );    else    printf("id %3d not found ", id);       id = getID();   }
  /* Write out the data in order of id */   printf(" All Records: ");   for ( j=0; j<arraySize; j++ )    if ( data[j].id != 0 )    printRecord( &data[j] );
  fclose( input );   system("pause"); }






1. Modify the struct so that it includes a link to thenext node (a pointer to the next stuct in a linked list.) 2. Add an insertFirst() function and a freeList() function.Modify the rest of the code so that it works as before, but uses a linked list implementation of thedatabase.

thank you for any help

I started this project but i need to modify it toworks with the direction below
/* Mini-data base. Somewhat more than was asked for withprogram 10 */
#include <stdio.h> #include <stdlib.h>
#define nameSize 50 #define arraySize 250
/* Data for one person */ typedef struct {   int id, birthYear, height, weight, salary;   char firstName[nameSize],lastName[nameSize]; } Person;
/* The database: array of structs, arranged in order of id*/ Person data[arraySize];
#define bufferSize 132 char buffer[bufferSize]; /* input buffer */
/* Print nicely formatted data for one person */ void printRecord( Person *p ) {   printf("id: %03d birth: %04d height: %02d weight:%03d salary: %06d name: %s %s ",    p->id,p->birthYear,p->height,p->weight,      p->salary,p->firstName,p->lastName); }
/* Ask the user for the id of a person */ int getID() {   int id;      printf("id? (enter -1 to quit):");   gets( buffer );   while ( sscanf( buffer, " %d", &id ) != 1)   {    printf("re-enter id: ");    gets( buffer );   }   return id; }
void readData( FILE* input ) {   Person p; /* data from one input record*/   int j = 0; /* count of records read in*/   int flag; /* count of successfulconversions in scanf */
  /* Read in the File, copying data into the array*/   while ( fgets( buffer, bufferSize, input) )   {    j++ ;
   /* Read in the data */    flag = sscanf( buffer, "%d %d %d %d %d %s%s",    &p.id,&p.birthYear, &p.height, &p.weight,&p.salary,    p.lastName,p.firstName );
   if ( flag != 7 )    {    fprintf( stderr, "Data not formattedcorrectly: Record %d ", j );    fclose( input );    exit( EXIT_FAILURE );    }
   /* copy data into the database */    if ( p.id < arraySize )    {    if ( data[p.id].id != 0 )    fprintf( stderr, "Record %d:duplicate id %d; record skipped ", j, p.id );    else    data[p.id] = p;    }    else    fprintf( stderr, "Record %d: id %dis out of range; record skipped ", j, p.id );   } }
int main() {   FILE *input;   int j=0;   char fileName[bufferSize];
  /* Ask user for input file; open file */   printf("Name of input file: ");   gets( buffer );   sscanf( buffer, "%s", fileName );
  /* Open input file for reading */   if ( (input=fopen( fileName, "r"))==NULL )   {    perror("input file did not open ");    exit( EXIT_FAILURE );   }      /* Read in the data */   readData( input );      /* Process Queries until user enters -1 */   int id = 0 ;   id = getID();      while ( id != -1 )   {    /* Linear search */    for ( j=0; j<arraySize &&data[j].id < id; j++ );       if ( data[j].id == id )    printRecord( &data[j] );    else    printf("id %3d not found ", id);       id = getID();   }
  /* Write out the data in order of id */   printf(" All Records: ");   for ( j=0; j<arraySize; j++ )    if ( data[j].id != 0 )    printRecord( &data[j] );
  fclose( input );   system("pause"); }






1. Modify the struct so that it includes a link to thenext node (a pointer to the next stuct in a linked list.) 2. Add an insertFirst() function and a freeList() function.Modify the rest of the code so that it works as before, but uses a linked list implementation of thedatabase.

thank you for any help

Explanation / Answer

Right off from the start, you need to make a node class somewhere.I'm sure your textbook has an example. The code would be verysimple and small. A node simply carries information and haspointers to the next items in a list. So, your pointers would benull so they can be assigned other objects in the list. Linked lists are a way to store data with structures so that theprogrammer can automatically create a new place to store datawhenever necessary. Specifically, the programmer writes a struct orclass definition that contains variables holding information aboutsomething, and then has a pointer to a struct of its type. Each ofthese individual struct or classes in the list is commonly known asa node. Think of it like a train. The programmer always stores the firstnode of the list. This would be the engine of the train. Thepointer is the connector between cars of the train. Every time thetrain adds a car, it uses the connectors to add a new car. This islike a programmer using the keyword new to create a pointer to anew struct or class. In memory it is often described as looking like this: ---------- ----------- Data - - Data - ---------- ---------- - Pointer- - - -> - Pointer- ---------- ---------- The representation isn't completely accurate, but it will sufficefor our purposes. Each of the big blocks is a struct (or class)that has a pointer to another one. Remember that the pointer onlystores the memory location of something, it is not that thing, sothe arrow goes to the next one. At the end, there is nothing forthe pointer to point to, so it does not point to anything, itshould be a null pointer or a dummy node to prevent it fromaccidentally pointing to a totally arbitrary and random location inmemory (which is very bad). So far we know what the node struct should look like: This so far is not very useful for doing anything. It is necessaryto understand how to traverse (go through) the linked list beforegoing further. Think back to the train. Lets imagine a conductor who can onlyenter the train through the engine, and can walk through the traindown the line as long as the connector connects to another car.This is how the program will traverse the linked list. Theconductor will be a pointer to node, and it will first point toroot, and then, if the root's pointer to the next node is pointingto something, the "conductor" (not a technical term) will be set topoint to the next node. In this fashion, the list can be traversed.Now, as long as there is a pointer to something, the traversal willcontinue. Once it reaches a null pointer (or dummy node), meaningthere are no more nodes (train cars) then it will be at the end ofthe list, and a new node can subsequently be added if sodesired. Here's what that looks like: That is the basic code for traversing a list. The if statementensures that there is something to begin with (a first node). Inthe example it will always be so, but if it was changed, it mightnot be true. If the if statement is true, then it is okay to tryand access the node pointed to by conductor. The while loop willcontinue as long as there is another pointer in the next. Theconductor simply moves along. It changes what it points to bygetting the address of conductor->next. Finally, the code at the end can be used to add a new node to theend. Once the while loop as finished, the conductor will point tothe last node in the array. (Remember the conductor of the trainwill move on until there is nothing to move on to? It works thesame way in the while loop.) Therefore, conductor->next is setto null, so it is okay to allocate a new area of memory for it topoint to. Then the conductor traverses one more element (like atrain conductor moving on the the newly added car) and makes surethat it has its pointer to next set to 0 so that the list has anend. The 0 functions like a period, it means there is no morebeyond. Finally, the new node has its x value set. (It can be setthrough user input. I simply wrote in the '=42' as an example.) To print a linked list, the traversal function is almost the same.It is necessary to ensure that the last element is printed afterthe while loop terminates. For example: The final output is necessary because the while loop will not runonce it reaches the last node, but it will still be necessary tooutput the contents of the next node. Consequently, the last outputdeals with this. Because we have a pointer to the beginning of thelist (root), we can avoid this redundancy by allowing the conductorto walk off of the back of the train. Bad for the conductor (if itwere a real person), but the code is simpler as it also allows usto remove the initial check for null (if root is null, thenconductor will be immediately set to null and the loop will neverbegin):