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

Create a new version of your Project 5 program, the one that reads the acars.bin

ID: 3713237 • Letter: C

Question

Create a new version of your Project 5 program, the one that reads the acars.bin file, so that instead of storing the structs in an array, you dynamically allocate each struct, and add them to a linked list that you create.

So the logic of your program looks like: create two pointers to your struct. One is called head, and the other tail. open the acars.bin file malloc the first struct read the file into that struct make head point to that struct make tail point to that struct while not end-of-file on the acars file: malloc a new struct read the next struct from acars.bin into the structure you malloc'ed.

Add that struct to your linked list, using the next_ptr member of the struct set tail to point to the element you just added repeat the loop Starting at head, traverse the list, printing the elements of each structure.

Your struct should now look like this:

typedef struct acars_struct {

char flight[7] ;

char dest[5] ;

char origin[5] ;

int dateTimeStamp ;

struct acars_struct* next ;

} AcarsStruct ;

This is exactly like your definition for the struct in Project 5, except that a pointer named next has been added. This pointer is used to "string together" successively-allocated structs in the linked list. The head pointer points to the first element of the list, and the tail pointer will point to the last element of the list.

Explanation / Answer

#include #include #include int main() { typedef struct acars_struct{ char flight[7] ; char dest[5] ; char origin[5] ; int dateTimeStamp ; struct acars_struct* next ; } AcarsStruct ; FILE* inFile = NULL; inFile = fopen("acars.bin", "rb"); if(inFile == NULL){ return -1; } AcarsStruct* head; AcarsStruct* tail; AcarsStruct* temp; temp = (AcarsStruct*) malloc(sizeof(AcarsStruct)); fread(temp, sizeof(AcarsStruct) - sizeof(AcarsStruct*), 1, inFile); temp->next = NULL; head = temp; tail = head; time_t t = head->dateTimeStamp; printf("%s, %s, %s, %s ", head->flight, head->origin, head->dest, ctime(&t)); while(!feof(inFile)){ temp = (AcarsStruct*) malloc(sizeof(AcarsStruct)); fread(temp, sizeof(AcarsStruct) - sizeof(AcarsStruct*) , 1, inFile); temp->next = NULL; tail->next = temp; tail = temp; time_t t = tail->dateTimeStamp; printf("%s, %s, %s, %s ", tail->flight, tail->origin, tail->dest, ctime(&t)); } return 0; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote