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

code the following functions that operate on a priority list pointed to by perso

ID: 3918614 • Letter: C

Question

code the following functions that operate on a priority list pointed to by person_t *head.

void search_age_person_data(unsigned short a);

search_person_data() traverses the priority queue pointed to by head and outputs to stdout all elements which match the key (i.e., age) given by a.

void search_height_person_data(int ft1, int inch1, int ft2, int inch2);

search_height_person_data() traverses the priority queue pointed to by head and outputs to stdout all elements whose height lies strictly between ft1 feet inch1 inches and ft2 feet and inch2 inches.

void delete_person_data(unsigned short a, int b);

delete_person_data() traverses the priority queue and removes any element from the list that matches age a and identifier b. Create a dynamic library libmyplist.so and add the functions from Problem 1 and 2.

/Storing all functions in below file:
//read_person_data.c

#include <stdio.h>
#include <stdlib.h> //required for malloc function
#include <string.h>

//structure definition
typedef struct person {
   unsigned short age;
   char *name; // first name, no spaces
   struct height {int feet; int inches;} vertical;
   int idenifier; // unique identifier
   struct person *next;
} person_t;

person_t *head=NULL;//declared header as global for linked list

//function inserts element into priority list, pointed by head
void insert_plist(unsigned short a, char *b, int c, int d, int e)
{
   //code here
   person_t *nn=(person_t*)malloc(sizeof(person_t));//allocate new element node
   nn->age=a; //assign each parameter to new node nn
   nn->name=(char*)malloc(sizeof(char)*20);//allocate char array
   nn->name=b;
   nn->vertical.feet=c;
   nn->vertical.inches=d;
   nn->idenifier=e;
   nn->next=NULL;
   if(head==NULL) //when first insertion
   {
       head=nn; //new node as header
       head->next=NULL; //next is nothing
   }
   else //when other than first node
   {
       person_t *temp=head;//temp refers header
       person_t *tprev=temp; //prev follows temp
       while(temp->age<a && temp!=NULL) //repeat until existing age is less than parameter a, traverse upto greater element is found
       {
           tprev=temp;
           temp=temp->next; //move to next
       }
      
       tprev->next=nn;//link new node nn between prev and temp
       nn->next=temp;
   }
      
}

//Function reads file
void read_person_data(FILE *fp)
{
   unsigned short a;
   char *b=(char*)malloc(sizeof(char)*2);//allocate memory
   int c,d,e; //declare local variable for reading
   fscanf(fp,"%hu%s%d%d%d",&a,b,&c,&d,&e);//read first element from file
  
   while(!feof(fp)) //repeat unitl end of file
   {
       insert_plist(a,b,c,d,e);//call function to add element
       fscanf(fp,"%hu%s%d%d%d",&a,b,&c,&d,&e);//read second element onwards from file
   }
}
//Function writes to another file
void write_person_data(FILE *fp)
{
   person_t *temp=head;//temp refers header
   while(temp!=NULL) //traverse upto null
   {
       fprintf(fp,"%hu %s %d %d %d",temp->age,temp->name,temp->vertical.feet,temp->vertical.inches,temp->idenifier);//write data of element from list to file
       temp=temp->next;//move to next element
   }
}
  

//Function main with command line args
int main(int argc,char *argv[])
{
   if(argc!=3) //when invalid no.of args
   {
       printf(" Usage: read_person_date <source file> <destination file> ");//print error message
   }
   else //when proper args are passed
   {
       FILE *infile=fopen(argv[1],"r");//open file for reading
       FILE *outfile=fopen(argv[2],"w");//open file for writing
       read_person_data(infile);//call function to read file to linked list
       write_person_data(outfile);//call function to write file
       fclose(infile);//close the file
       fclose(outfile);//close the file
       printf(" File is successfully sorted and copied!!!");
   }
  
  
   return 0;
}

Explanation / Answer

#include <stdio.h>

#include <stdlib.h> //required for malloc function

#include <string.h>

//structure definition

typedef struct person {

   unsigned short age;

   char *name; // first name, no spaces

   struct height {int feet; int inches;} vertical;

   int idenifier; // unique identifier

   struct person *next;

} person_t;

person_t *head=NULL;//declared header as global for linked list

//function inserts element into priority list, pointed by head

void insert_plist(unsigned short a, char *b, int c, int d, int e)

{

   //code here

   person_t *nn=(person_t*)malloc(sizeof(person_t));//allocate new element node

   nn->age=a; //assign each parameter to new node nn

   nn->name=(char*)malloc(sizeof(char)*20);//allocate char array

   nn->name=b;

   nn->vertical.feet=c;

   nn->vertical.inches=d;

   nn->idenifier=e;

   nn->next=NULL;

   if(head==NULL) //when first insertion

   {

       head=nn; //new node as header

       head->next=NULL; //next is nothing

   }

   else //when other than first node

   {

       person_t *temp=head;//temp refers header

       person_t *tprev=temp; //prev follows temp

       while(temp->age<a && temp!=NULL) //repeat until existing age is less than parameter a, traverse upto greater element is found

       {

           tprev=temp;

           temp=temp->next; //move to next

       }

       tprev->next=nn;//link new node nn between prev and temp

       nn->next=temp;

   }

}

//Function reads file

void read_person_data(FILE *fp)

{

   unsigned short a;

   char *b=(char*)malloc(sizeof(char)*2);//allocate memory

   int c,d,e; //declare local variable for reading

   fscanf(fp,"%hu%s%d%d%d",&a,b,&c,&d,&e);//read first element from file

   while(!feof(fp)) //repeat until end of file

   {

       insert_plist(a,b,c,d,e);//call function to add element

       fscanf(fp,"%hu%s%d%d%d",&a,b,&c,&d,&e);//read second element onwards from file

   }

   }

//Function writes to another file

void write_person_data(FILE *fp)

{

   person_t *temp=head;//temp refers header

   while(temp!=NULL) //traverse upto null

   {

       fprintf(fp,"%hu %s %d %d %d",temp->age,temp->name,temp->vertical.feet,temp->vertical.inches,temp->idenifier);//write data of element from list to file

       temp=temp->next;//move to next element

   }

}

// function to output the records which matches the age a

void search_age_person_data(unsigned short a)

{

               person_t *temp=head;//temp refers header

               while(temp!=NULL) //traverse upto null

               {

                              if(temp->age == a)

                                             printf("%hu %s %d %d %d",temp->age,temp->name,temp->vertical.feet,temp->vertical.inches,temp->idenifier);//write data of element from list to file

                              temp = temp->next;

               }

}

// function to output the records whose height lies strictly between ft1 feet inch1 inches and ft2 feet and inch2 inches.

void search_height_person_data(int ft1, int inch1, int ft2, int inch2)

{

               person_t *temp=head;//temp refers header

               while(temp!=NULL) //traverse upto null

               {

                              if(ft1 < ft2)

                              {

                                             if(temp->vertical.feet > ft1 && temp->vertical.feet < ft2)

                                                            printf("%hu %s %d %d %d",temp->age,temp->name,temp->vertical.feet,temp->vertical.inches,temp->idenifier);//write data of element from list to file

                                             else if(temp->vertical.feet == ft1 && temp->vertical.inches > inch1)

                                                            printf("%hu %s %d %d %d",temp->age,temp->name,temp->vertical.feet,temp->vertical.inches,temp->idenifier);//write data of element from list to file

                                             else if(temp->vertical.feet == ft2 && temp->vertical.inches < inch2)

                                                            printf("%hu %s %d %d %d",temp->age,temp->name,temp->vertical.feet,temp->vertical.inches,temp->idenifier);//write data of element from list to file

                              }else if(ft1 == ft2)

                              {

                                             if(temp->vertical.feet == ft1 && temp->vertical.inches > inch1 && temp->vertical.inches < inch2)

                                                            printf("%hu %s %d %d %d",temp->age,temp->name,temp->vertical.feet,temp->vertical.inches,temp->idenifier);//write data of element from list to file

                              }else{

                                             return;

                              }

                              temp = temp->next;

               }

}

// function to delete the person with age=a and idenifier=b

void delete_person_data(unsigned short a, int b)

{

               person_t *temp=head;//temp refers header

               person_t *preTemp = NULL; // preTemp refers previous to temp

               // delete the head nodes with age 'a' and idenifier 'b' and updating the new head

               while(head->age == a && head->idenifier == b)

               {

                              head = head->next;

                              free(temp);

                              temp = head;

               }

               preTemp = temp;

               temp = temp->next;

               // loop till the end of the list

               while(temp != NULL)

               {

                              // if temp has age 'a' and idenifier 'b', delete it

                              if(temp->age == a && temp->idenifier == b)

                              {

                                             person_t del = temp;

                                             preTemp->next = temp->next;

                                             temp = temp->next;

                                             free(del);

                              }else{

                                             preTemp = temp;

                                             temp = temp->next;

                              }

               }

}

//Function main with command line args

int main(int argc,char *argv[])

{

   if(argc!=3) //when invalid no.of args

   {

       printf(" Usage: read_person_date <source file> <destination file> ");//print error message

   }

   else //when proper args are passed

   {

       FILE *infile=fopen(argv[1],"r");//open file for reading

       FILE *outfile=fopen(argv[2],"w");//open file for writing

       read_person_data(infile);//call function to read file to linked list

       write_person_data(outfile);//call function to write file

       fclose(infile);//close the file

       fclose(outfile);//close the file

       printf(" File is successfully sorted and copied!!!");

   }

   return 0;

}