• Read in a class roster input file given as a command line argument to the prog
ID: 3765861 • Letter: #
Question
• Read in a class roster input file given as a command line argument to the program and save it into a queue data structure by using a linked list o The input file will be supplied and have a format as follows First_Name Last_Name ID Age Names will be no longer than 20 characters long o The program must read from the file safely o The program must gracefully exit if the input file command line parameter is not given • The structs needed for the program are: struct student_record { int student_id_; int student_age_; char first_name_[21]; char last_name_[21]; }; /** Should be defined in StudentRecord.h **/ struct student_record_node { struct student_record* record_; struct student_record_node* next_; struct student_record_node* prev_; }; /** Should be defined in StudentRecordNode.h **/ • Write a function to read in the class roster input file and create the queue o It will have the prototype: void parseFile(char* filename, struct student_record_node** head) Should be defined in your .C file • Write a function that will print the contents of a struct student_record_node o It will have the prototype: void printNode(struct student_record_node* node) Should be defined in StudentRecordNode.h • Write a function that will write the contents of the queue linked list to a file o It will have the prototype void streamNodeList(char * fileName, struct student_record_node* head) Should be defined in StudentRecordNode.h • Write a function that will allocate and initialize a struct student_record_node o It will have the prototype: Struct student_record_node* student_record_allocate() Should be defined in StudentRecordNode.h • Write a function that will free the memory occupied by a struct student_record_node o It will have the prototype: Void student_record_node_deallocate(struct student_record_node* node) Should be defined in StudentRecordNode.h • Write a function that will sort the linked list by using a function pointer to a comparator function o It will have the prototype: void sort(struct student_record_node** recordsHead, int (*compare_fcn)(struct student_record_node*, struct student_record_node*)) Should be defined in StudentRecordNode.h • Write a function that will compare to nodes in the list and determine if the first node’s age is greater than the second node’s age o It will have the prototype: int ageComparator(struct student_record_node* node1, struct student_record_node* node2) Should be defined in StudentRecordNode.h Should return 1 or 0 accordingly • Write a function that will compare to nodes in the list and determine if the first node’s id is greater than the second node’s id o It will have the prototype: int idComparator(struct student_record_node* node1, struct student_record_node* node2) Should be defined in StudentRecordNode.h Should return 1 or 0 accordingly • Write a function that the sorting functions will use for swapping the POSITIONS of nodes in a linked list(this is important. We are sorting by swapping changing position of nodes) o void swap(struct student_record_node** node1, struct student_record_node** node2) o Should be defined in StudentRecordNode.h • Write a function that will free the memory occupied by the Queue o It will have the prototype: void freeNodeList(struct student_record_node* head) Should be defined in StudentRecordNode.h • Write a function that will append a node to the Queue o It will have the prototype: void appendNode(struct student_record_node* head, struct student_record_node* newNode) Should be defined in StudentRecordNode.h • Write a function that will print the Queue o It will have the prototype: void printNodeList(struct student_record_node* head) Should be defined in StudentRecordNode.h
Explanation / Answer
Below is the code for the question. Sample input and output is also shown. Please don't forget to rate the answer if it helped. Thank you very much.
StudentRecord.h
#ifndef StudentRecord_h
#define StudentRecord_h
struct student_record
{
int student_id_;
int student_age_;
char first_name_[21];
char last_name_[21];
};
#endif /* StudentRecord_h */
StudentRecordNode.h
#ifndef StudentRecordNode_h
#define StudentRecordNode_h
#include "StudentRecord.h"
struct student_record_node
{
struct student_record* record_;
struct student_record_node* next_;
struct student_record_node* prev_;
};
void parseFile(char *filename, struct student_record_node** head);
void printNode(struct student_record_node* node);
void streamNodeList(char * fileName, struct student_record_node* head);
struct student_record_node* student_record_allocate();
void student_record_node_deallocate(struct student_record_node* node);
void sort(struct student_record_node** recordsHead, int (*compare_fcn)(struct student_record_node*, struct student_record_node*));
int ageComparator(struct student_record_node* node1, struct student_record_node* node2) ;
int idComparator(struct student_record_node* node1, struct student_record_node* node2);
void swap(struct student_record_node** node1, struct student_record_node** node2);
void freeNodeList(struct student_record_node* head);
void appendNode(struct student_record_node* head, struct student_record_node* newNode);
void printNodeList(struct student_record_node* head);
#endif /* StudentRecordNode_h */
StudentRecordNode.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "StudentRecordNode.h"
void parseFile(char *filename, struct student_record_node** head)
{
FILE *fp = fopen(filename, "r");
struct student_record_node* node;
struct student_record* rec;
char fname[21], lname[21];
int id, age;
if(fp == NULL)
{
printf(" *** ERROR reading file %s. ", filename);
*head = NULL;
return;
}
fscanf(fp, "%s %s %d %d", fname, lname, &id, &age);
while(!feof(fp))
{
node = student_record_allocate();
rec = node->record_;
strcpy(rec->first_name_, fname);
strcpy(rec->last_name_, lname);
rec->student_id_ = id;
rec->student_age_ = age;
if(*head == NULL)
*head = node;
else
appendNode(*head, node);
fscanf(fp, "%s %s %d %d", fname, lname, &id, &age);
}
fclose(fp);
}
void printNode(struct student_record_node* node)
{
struct student_record* rec;
if(node == NULL || node->record_ == NULL)
return;
rec = node->record_;
printf(" %20s %20s %5d %5d ", rec->first_name_, rec->last_name_, rec->student_id_, rec->student_age_);
}
void streamNodeList(char * fileName, struct student_record_node* head)
{
FILE *fp = fopen(fileName, "w");
struct student_record_node* node;
struct student_record* rec;
if(head != NULL && fp != NULL)
{
node = head;
while(node != NULL)
{
rec = node->record_;
fprintf(fp, "%20s %20s %5d %5d ", rec->first_name_, rec->last_name_, rec->student_id_, rec->student_age_);
node = node->next_;
}
}
fclose(fp);
}
struct student_record_node* student_record_allocate()
{
struct student_record_node* node = (struct student_record_node *)malloc(sizeof(struct student_record_node));
node->record_ = (struct student_record*) malloc(sizeof(struct student_record));
node->prev_ = NULL;
node->next_ = NULL;
return node;
}
void student_record_node_deallocate(struct student_record_node* node)
{
if(node == NULL)
return;
free(node->record_);
free(node);
}
void sort(struct student_record_node** recordsHead, int (*compare_fcn)(struct student_record_node*, struct student_record_node*))
{
if(*recordsHead == NULL || compare_fcn == NULL)
return;
for(struct student_record_node *node1 = *recordsHead; node1 != NULL; node1 = node1->next_)
{
for(struct student_record_node *node2 = node1->next_; node2 != NULL; node2 = node2->next_)
{
if(compare_fcn(node1, node2) > 0)
swap(&node1, &node2);
}
}
}
int ageComparator(struct student_record_node* node1, struct student_record_node* node2)
{
return node1->record_->student_age_ - node2->record_->student_age_;
}
int idComparator(struct student_record_node* node1, struct student_record_node* node2)
{
return node1->record_->student_id_ - node2->record_->student_id_;
}
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
char temp[21];
int id, age;
//swap the first names
strcpy(temp, (*node1)->record_->first_name_);
strcpy( (*node1)->record_->first_name_, (*node2)->record_->first_name_);
strcpy( (*node2)->record_->first_name_, temp);
//swap the last names
strcpy(temp, (*node1)->record_->last_name_);
strcpy( (*node1)->record_->last_name_, (*node2)->record_->last_name_);
strcpy( (*node2)->record_->last_name_, temp);
//swap id
id =(*node1)->record_->student_id_;
(*node1)->record_->student_id_ = (*node2)->record_->student_id_;
(*node2)->record_->student_id_ = id;
//swap age
age =(*node1)->record_->student_age_;
(*node1)->record_->student_age_ = (*node2)->record_->student_age_;
(*node2)->record_->student_age_ = age;
}
void freeNodeList(struct student_record_node* head)
{
struct student_record_node* p = head;
if(head == NULL)
return;
while(p->next_ != NULL)
{
student_record_node_deallocate(p->prev_);
p = p->next_;
}
student_record_node_deallocate(p);
}
void appendNode(struct student_record_node* head, struct student_record_node* newNode)
{
struct student_record_node* p = head;
if(head == NULL || newNode == NULL)
return;
while(p->next_ != NULL)
p = p->next_;
p->next_ = newNode;
newNode->prev_ = p;
}
void printNodeList(struct student_record_node* head)
{
struct student_record_node* p = head;
while(p != NULL)
{
printNode(p);
p = p->next_;
}
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "StudentRecordNode.h"
int main(int argc, const char * argv[]) {
struct student_record_node *head = NULL;
char filename[256];//="students.txt";
printf(" Enter filename: ");
fgets(filename, 255, stdin);
//remove at the end
if(strlen(filename) > 0)
filename[strlen(filename)-1] = '';
parseFile(filename, &head);
printf("The following records are loaded from file ");
printNodeList(head);
printf("The records sorted on ID ");
sort(&head, idComparator);
printNodeList(head);
printf("The records sorted on age ");
sort(&head, ageComparator);
printNodeList(head);
freeNodeList(head);
}
input file students.txt
John S 555 19
Alice C 111 20
Bob K 888 21
Peter M 333 17
Henry T 222 20
output
Enter filename: students.txt
The following records are loaded from file
John S 555 19
Alice C 111 20
Bob K 888 21
Peter M 333 17
Henry T 222 20
The records sorted on ID
Alice C 111 20
Henry T 222 20
Peter M 333 17
John S 555 19
Bob K 888 21
The records sorted on age
Peter M 333 17
John S 555 19
Alice C 111 20
Henry T 222 20
Bob K 888 21
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.