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

C program Given a list of N numbers, a priority list (or queue) comprised of the

ID: 3917589 • Letter: C

Question

C program

Given a list of N numbers, a priority list (or queue) comprised of these numbers puts the highest number at the front of the list. If two or more numbers are equal then ties are broken by a fixed scheme. The second highest number comes next, etc. Hence the numbers are being sorted in decreasing (more precisely, non-increasing) order. We can also reverse the sorting order so that a lower value means "higher" priority. In many applications, the numbers acting as priorities are part of a more general data structure, in which case these numbers are referred to as a key. For example, each element of the data structure may represent information about a person such as given by the struct

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;

where the field/member age acts as a key. A data file "person_data.dat" may contain

31 Alice 5 9 111111
64 Bob 6 11 555555
22 Mira 6 3 7171

which are read in and stored as a linked list whose elements are sorted in non-increasing order of age. That is, a priority list where age acts as the key. The first element of the list is pointed to by person_t *head which in the example would be 22 Mira 6 3 7171. The last element, after the file has been read in and processed, is 64 Bob 6 11 555555. The next field of the last element should be NULL to indicate the end of the list.

Write a program that reads data from an input file, whose name is given as a command-line argument, the values of data of type person_t and stores them into a priority queue pointed to by head. Declare person_t * head as global and call

void read_person_data(FILE *fp);

to read the data and organize them as a priority list. The file is opened in main(). read_person_data(), after reading the next 5-tuple age, name, feet, inches, identifier calls

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

which inserts the new element into the priority list pointed to by head. insert_plist() traverses the existing priority list until it finds the position where the element (based on its key) should be inserted. insert_plist() calls malloc() to allocate memory for the new element of type person_t. It calls malloc() to allocate space for the name member. main() calls

void write_person_data(FILE *fp);

to output the content of the priority list pointed to by head into a file pointed to by its argument fp. The name of the output file is provided as the second command-line argument of main() which is opened in main(). Store all functions in their own .c file by the same name (e.g., read_person_data.c).

Explanation / Answer

Hi, I completed all required functions in c language. I added comments to most of lines for better explanation. PLEASE POST YOUR YOUR VALUABLE REVIEW THUMSUP!!!

//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;
}