Question: Hello, I need help completing this code as per the instructions below. I am trying to use a singly linked list. I am trying to
Hello, I need help completing this code as per the instructions below. I am trying to use a singly linked list. I am trying to use a single linked list. I have provided some test cases. Thank you. Anything that can be done to complete some of the empty functions would be immensely helpful. Thank you as I really need any help I can get with this.
Essentially trying to do what I put in this comment here.
/*Put each line of the text file into the airpdata structure (already done by parseline function) then put it
into a linked list. In the sort functions, you sort the linked list either alphabetically or by latitude
then you print it. In the sexag2decimal function you just apply the formula from the pdf to the longitude/
latitude to turn them from sexagesimal degrees to decimal degrees.
*/
#include
#include
#include
typedef struct airPdata{?char *LocID; //Airports Short Name, ie MCO char *fieldName; //Airport Name?char *city; //Associated City?float longitude; //Longitude?float latitude; //Latitude
} airPdata;
typedef struct linkedList{
char data;
struct linkedList *next;
}linkedList;
void parseLine(char *line, airPdata *apd);
void printData(int length, airPdata *apd);
void deleteStruct(airPdata *apd);
float sexag2decimal(char *degreeString);
void sortByLocID(lListAirPdata *airports);
void sortByLatitude(lListAirPdata *airports);
int checkLocID(tocheck);
#define BUFFER_SIZE 500
int main (int argc, char *argv[]){
// Declare input buffer and other parameters
FILE *fid;
char buffer[BUFFER_SIZE];
int count = 0;
// Check for command line input and open input file.
if(argc==3){
fid = fopen(argv[1], "r");
if(fid==NULL){
printf("hw3Sort ERROR: File 'bogusFilename' not found. ", argv[1]);
return 2;
}
if(tolower(argv[2]) != "a" || tolower(argv[2]) != "n"){
printf("hw3Sort ERROR: valid sort parameters are a or n.");
}
}
else{
printf("Incorect number of input parameters. Please specify the name of the input file and sort parameter only. ");
return 1;
}
// Determine length of the file.
while(fgets(buffer, BUFFER_SIZE, fid) != NULL){
count++;
}
rewind(fid);
// Declare a struct array and allocate memory.
airPdata *data;
data = malloc(sizeof(airPdata)*count);
if(data==NULL){
printf("Memory allocation for airPdata array failed. Aborting. ");
return 2;
}
// Read and parse each line of the inputt file.
for(int i = 0; i fgets(buffer, BUFFER_SIZE, fid); // fgets() includes the New Line delimiter in the output string. // i.e. "This is my string. \0" // We will truncate the string to drop the ' ' if it is there. // Note: There will be no ' ' if the line is longer than the buffer. if(buffer[strlen(buffer) - 1] == ' ') buffer[strlen(buffer)-1] = '\0'; parseLine(buffer, data+i); } // close the input file. fclose(fid); // Output the data to stdout. printData(count, data); // Free the memory used for fields of the structs. for(int i=0; i deleteStruct(data+i); } // Free the memory for the struct array. free(data); return 0; } void parseLine(char *line, airPdata *apd){ int i=0, j=0, commas=0; while(commas while(*(line+i)!=','){ i++; } // strncpy does not append a '\0' to the end of the copied sub-string, so we will // replace the comma with '\0'. *(line+i) = '\0'; switch (commas){ case 0: //Grab the first "field" - Site Number apd->siteNumber = malloc(sizeof(char)*(i-j+1)); if(apd->siteNumber==NULL){ printf("malloc failed to initialize airPdata.siteNumber. "); exit(-1); } strncpy(apd->siteNumber, line+j, i-j+1); break; case 1: //Grab the second "field" - Location ID apd->LocID = malloc(sizeof(char)*(i-j+1)); if(apd->LocID==NULL){ printf("malloc failed to initialize airPdata.LocID. "); exit(-1); } strncpy(apd->LocID, line+j, i-j+1); break; case 2: //Grab the third "field" - Field Name apd->fieldName = malloc(sizeof(char)*(i-j+1)); if(apd->fieldName==NULL){ printf("malloc failed to initialize airPdata.fieldName. "); exit(-1); } strncpy(apd->fieldName, line+j, i-j+1); break; case 3: //Grab the fourth "field" - City apd->city = malloc(sizeof(char)*(i-j+1)); if(apd->city==NULL){ printf("malloc failed to initialize airPdata.city. "); exit(-1); } strncpy(apd->city, line+j, i-j+1); break; case 4: //Grab the fifth "field" - State apd->state = malloc(sizeof(char)*(i-j+1)); if(apd->state==NULL){ printf("malloc failed to initialize airPdata.state. "); exit(-1); } strncpy(apd->state, line+j, i-j+1); break; case 8: //Grab the ninth "field" - Latitude (sexagesimal string) apd->latitude = malloc(sizeof(char)*(i-j+1)); if(apd->latitude==NULL){ printf("malloc failed to initialize airPdata.latitude. "); exit(-1); } strncpy(apd->latitude, line+j, i-j+1); break; case 9: //Grab the tenth "field" - Longitude (sexagesimal string) apd->longitude = malloc(sizeof(char)*(i-j+1)); if(apd->longitude==NULL){ printf("malloc failed to initialize airPdata.longitude. "); exit(-1); } strncpy(apd->longitude, line+j, i-j+1); break; case 14: //Grab the fifteenth "field" - Control Tower (Y or N) apd->controlTower=*(line+j); break; case 16: //Grab the seventeenth "field" - Control Tower on North/South runway (Y or N) apd->controlTower=*(line+j); break; } j=++i; commas++; } } int checkLocID(tocheck){ for(int i = 0; i if(!isalpha(tocheck[i]){ return 0; } } return 1; } float sexag2decimal(char *degreeString){ if(degreeString == NULL){ return 0.0; } print latitudaly; } void sortByLocID(lListAirPdata *airports){ if(argv[2] == 'a' || argv[2]=='n') print alphabetically; } void sortByLatitude(lListAirPdata *airports); { } void printData(int length, airPdata *data){ printf("%-12s %-11s %-42s %-34s %-3s %-15s %-16s Tower ", "FAA Site", "Short Name", "Airport Name", "City", "ST", "Latitude", "Longitude"); printf("%-12s %-11s %-42s %-34s %-3s %-15s %-16s ===== ", "========", "==========", "============", "====", "==", "========", "========="); for(int i=0; i if((strlen((data+i)->LocID) == 3 || strlen((data+i)->LocID) == 4) && checkLocID((data+i)->LocID) == 1){ if(argv[2] == 'n'){ call function to print by location; } if(argv[2] == 'a'){ sortByLocID(data); } } printf("%-12s %-11s %-42s %-34s %-3s %-15s %-16s %c ",(data+i)->siteNumber,(data+i)->LocID,(data+i)->fieldName, (data+i)->city,(data+i)->state,(data+i)->latitude,(data+i)->longitude,(data+i)->controlTower); } } void deleteStruct(airPdata *apd){ free(apd->longitude); free(apd->latitude); free(apd->state); free(apd->city); free(apd->fieldName); free(apd->LocID); free(apd->siteNumber); } 





Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
