In a FIFO (first-in first-out), also called FCFS (first-come first-served), list
ID: 3919996 • Letter: I
Question
In a FIFO (first-in first-out), also called FCFS (first-come first-served), list elements are inserted not based on a key but when they were presented/encountered. For example, in the example of Problem 1 with three data elements in the file, ordering is the same as in the data file. This means that a new element is inserted at the end of the FIFO list. Write a function
void insert_flist(unsigned short a, char *b, int c, int d, int e);
that inserts new elements at the end of the list. Test that your code works correctly by compiling and linking with the other functions in libmyplist.so (except insert_plist()) which do not need to change.
HERE IS THE CODE: JUST ADD THE FUNCTION FROM ABOVE
#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;
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 insert new elements at the end of the list
void insert_flist(unsigned short a, char *b, int c, int d, int e)
{
person_t *ptr; //a temporary pointer which will be used to traverse the list
person_t *new1=(person_t*)malloc(sizeof(person_t));//allocate new element node
new1->age=a; //assign each parameter to new node nn
new1->name=(char*)malloc(sizeof(char)*20);//allocate char array
new1->name=b;
new1->vertical.feet=c;
new1->vertical.inches=d;
new1->idenifier=e;
new1->next=NULL; /* as the new node will be the last node so the next field of the new node will contain NULL */
ptr=head; // the temporary pointer points to the header node
if(ptr==NULL) //when the list is empty such that first insertion
{
head=new1; //new node as header
}
else // when other than the first node insertion
{
while(ptr->next != NULL)//repeat until the temporary pointer ptr reaches the last node of the existing list
{
ptr=ptr->next; //move to the next node
}
ptr->next=new1; // link new node new1 as the last node
}
}
//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)
{
char flag; // temporary flag character variable to know the type of insertion
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
{
printf("Do you want the new node as a Last node ? Y/N"); // ask what type of insertion will happen
scanf("%c",&flag); // assign the answer to the flag variable
if(flag =='Y') //if user wants insertion as a last node
{
insert_flist(a,b,c,d,e);//call function to add element as a last node
}
else // otherwise insertion will be as a priority node
{
insert_plist(a,b,c,d,e);//call function to add element as priority node
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.