C Program Given a list of N numbers, a priority list (or queue) comprised of the
ID: 3918067 • 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 64 Bob 6 11 555555. The last element, after the file has been read in and processed, is 22 Mira 6 3 7171. 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(). When two or more records have the same age, insert the one read later from the file at the end of the those read earlier.
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.
Explanation / Answer
here is your program : ---------->>>>>>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct person {
unsigned short age;
char *name; // first name, no spaces
struct height {int feet; int inches;} vertical;
int identifier; // unique identifier
struct person *next;
} person_t;
person_t *head = NULL;
void insert_plist(unsigned short a, char *b, int c, int d, int e);
void read_person_data(FILE *fp){
if(fp == NULL){
printf("File Not Opened");
exit(0);
}
unsigned short age;
char *name = (char *)malloc(sizeof(char)*20);
int feet,inch,ident;
while(!feof(fp)){
fscanf(fp,"%d",&age);
fscanf(fp,"%s%d%d%d",name,&feet,&inch,&ident);
if(feof(fp)){
break;
}
insert_plist(age,name,feet,inch,ident);
}
}
void insert_plist(unsigned short a,char *b, int c, int d, int e){
if(head == NULL){
head = (person_t *)malloc(sizeof(person_t));
head->age = a;
head->name = (char *)malloc(sizeof(char)*40);
strcpy(head->name,b);
head->vertical.feet = c;
head->vertical.inches = d;
head->identifier = e;
head->next = NULL;
return;
}
person_t *new_data = (person_t *)malloc(sizeof(person_t));
new_data->age = a;
new_data->name = (char *)malloc(sizeof(char)*40);
strcpy(new_data->name,b);
new_data->vertical.feet = c;
new_data->vertical.inches = d;
new_data->identifier = e;
new_data->next = NULL;
person_t *cur = head;
person_t *prev = NULL;
while(cur != NULL){
if(cur->age > new_data->age){
if(prev == NULL){
new_data->next = head;
head = new_data;
return;
}
prev->next = new_data;
new_data->next = cur;
return;
}
prev = cur;
cur = cur->next;
}
if(prev != NULL){
prev->next = new_data;
}
}
void write_person_data(FILE *fp){
person_t *cur = head;
while(cur != NULL){
fprintf(fp,"%d %s %d %d %d ",cur->age,cur->name,cur->vertical.feet,cur->vertical.inches,cur->identifier);
cur = cur->next;
}
}
void search_age_person_data(unsigned short a){
person_t *cur = head;
while(cur != NULL){
if(a == cur->age){
printf("%d %s %d %d %d ",cur->age,cur->name,cur->vertical.feet,cur->vertical.inches,cur->identifier);
}
cur = cur->next;
}
}
void search_height_person_data(int ft1, int inch1, int ft2, int inch2){
person_t *cur = head;
int f,i;
while(cur != NULL){
f = cur->vertical.feet;
i = cur->vertical.inches;
if(f > ft1 && f < ft2){
printf("%d %s %d %d %d ",cur->age,cur->name,cur->vertical.feet,cur->vertical.inches,cur->identifier);
}else if(f == ft1 && i > inch1 && f < ft2){
printf("%d %s %d %d %d ",cur->age,cur->name,cur->vertical.feet,cur->vertical.inches,cur->identifier);
}else if(f > ft1 && f == ft2 && i < inch2){
printf("%d %s %d %d %d ",cur->age,cur->name,cur->vertical.feet,cur->vertical.inches,cur->identifier);
}else if(f == ft1 && i > inch1 && f == ft2 && i < inch2){
printf("%d %s %d %d %d ",cur->age,cur->name,cur->vertical.feet,cur->vertical.inches,cur->identifier);
}
cur = cur->next;
}
}
void delete_person_data(unsigned short a, int b){
person_t *cur = head;
person_t *prev = NULL;
while(cur != NULL){
if(a == cur->age && b == cur->identifier){
if(prev == NULL){
head = head->next;
free(cur);
return;
}
prev->next = cur->next;
free(cur);
return;
}
prev = cur;
cur = cur->next;
}
}
void menu(){
printf(" 1- Search By age ");
printf(" 2- See person between heights ");
printf(" 3- delete a person ");
printf(" 4- Exit");
printf(" Choose : ");
}
int main(int argc,char *argv[]){
if(argc < 3){
printf("Too few command line argumnet !!! ");
return -1;
}
FILE *fin;
FILE *fout;
fin = fopen(argv[1],"r");
fout = fopen(argv[2],"w");
if(!fin || !fout){
printf("File Opening Error !!! ");
return -1;
}
read_person_data(fin);
char choice = '0';
while(choice != '4'){
menu();
scanf("%c*%c",&choice);
switch(choice){
case '1':
{
unsigned short int age = 0;
printf(" Enter a age : ");
scanf("%d%*c",&age);
search_age_person_data(age);
break;
}
case '2':
{
int ft1,ft2,inch1,inch2;
printf(" Enter two feet inch pair to search : ");
scanf("%d%d%d%d%*c",&ft1,&inch1,&ft2,&inch2);
search_height_person_data(ft1,inch1,ft2,inch2);
break;
}
case '3':
{
unsigned short int age = 0;
int ident;
printf(" Enter Age : ");
scanf("%d%*c",&age);
printf(" Enter Identifier : ");
scanf("%d%*c",&ident);
delete_person_data(age,ident);
break;
}
case '4':
break;
default:
printf(" Enter Wrong Choice !!! ");
}
}
write_person_data(fout);
fclose(fin);
fclose(fout);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.