Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct flight; //Forward Declaration of Flight structure
//----------------------------------- AIRLINE LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------
struct airline // Structure for Airline node, includes strings for Airline name
{ // pointer locations to the next airline and also to the flight list for that airline
char *airlinename;
struct airline *next;
struct flight *node1;
} *start1 = NULL;
airline* create_airline_node(airline *start1); //Function Declarations
airline* enter_airline_info(airline *node);
airline* delete_airline_node(airline *start1);
void print_airline_list(airline *start1);
airline* enter_flight_menu(airline *start1);
void write_to_file(airline *start1);
airline * read_from_file();
airline* airline_menu(airline *start1);
//----------------------------------- FLIGHT LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------
struct passenger; //Forward Declaration of passenger structure
struct flight // Structure for flight node, includes strings for flightname, from, to and time with
{ // pointer locations to the next flight and also to the passenger list
char *flightname;
char *departure_location;
char *arrival_location;
char *take_off_time;
struct flight *next;
struct passenger *node2;
} *start2 = NULL;
flight* create_flight_node(flight *start2); //Function Declarations
flight* enter_flight_info(flight *node);
flight* delete_flight_node(flight *start2);
void print_flight_list(flight *start2);
flight* enter_passenger_menu(flight *start2);
flight* flight_menu(flight *start2);
//----------------------------------- PASSENGER LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------
struct passenger //Passenger Structure includes name of the passenger and the ponter to the next
{ // passenger node, structure declaration also declares start as the first node
char *name; // and declares it NULL.
struct passenger *next;
} *start3 = NULL;
passenger* create_passenger_node(passenger *start3); //Function Declarations
passenger* enter_passenger_info(passenger *node);
passenger* delete_passenger_node(passenger *start3);
passenger* passenger_menu(passenger *start3);
void print_passenger_list(passenger *node);
//----------------------------- MEMBER FUNCTIONS TO MANIPULATE THE AIRLINE LINKED LIST --------------------------------------------
airline* create_airline_node(airline *start1) // Creates a sorted list of airlines (default entry sort condition by airline name)
{ // by entering the airline name in a node at the sorted position,
// it calls the function "enter_airline_info() to ask the
if (start1 == NULL) // user for its information
{
start1 = (airline *) malloc(sizeof(airline));
start1 = enter_airline_info(start1);
start1->next = NULL;
}
else
{
airline *count_node1, *count_node2, *node;
node = (airline *) malloc(sizeof(airline));
node = enter_airline_info(node);
for (count_node1 = start1; (count_node1 != NULL) && (strcmp(count_node1->airlinename, node->airlinename) <= 0); count_node2 = count_node1, count_node1 = count_node1->next);
if(count_node1 == start1)
{
node->next = start1;
start1 = node;
}
else if (count_node1 == NULL)
{
count_node2->next = node;
node->next = NULL;
}
else
{
count_node2->next = node;
node->next = count_node1;
}
}
return start1;
}
airline* enter_airline_info(airline *node) // Function to enter the information about the airline, this function is
{ // called from the function create_airline_node(airline *start1)by itself.
printf("\n");
printf(" Welcome to IGI International Airport\n");
printf("Please enter the Airline Name\n");
printf("\n");
fflush(stdin);
node->airlinename = new char [80];
gets(node->airlinename);
node->node1 = NULL;
return node;
}
airline* delete_airline_node(airline *start) // Function to delete the name of a airline, function
{ // searches for the name and deletes if it is found.
if(start1 == NULL)
{
printf("\n");
printf("Sorry, this Airport is not operational at this moment\n");
}
else
{
char airlinename[80];
printf("\n");
printf("Please enter the name of the Airline you want to delete\n");
fflush(stdin);
gets(airlinename);
airline *count_node1, *count_node2;
airline *temp;
for(count_node1 = start1; (count_node1 != NULL) && (strcmp(count_node1->airlinename, airlinename) != 0); count_node2 = count_node1, count_node1 = count_node1->next);
if(count_node1!= NULL)
{
if (count_node1 == start1)
{
temp = start1;
start1 = start1->next;
free(temp);
}
else if(count_node1->next == NULL && count_node1 != start1)
{
temp = count_node1;
count_node2->next = NULL;
free (temp);
}
else
{
temp = count_node1;
count_node2->next = count_node1->next;
free (temp);
}
}
else
{
printf("\n");
printf("Sorry, this airline is currently not in service at this airport\n");
}
}
return start1;
}
void print_airline_list(airline *start) //Function to print all the airlines operating on the airport
{
if (start1 == NULL)
{
printf("\n");
printf("Sorry, this Airport is not operational at this moment\n");
}
else
{
printf("\n");
airline *count;
for (count = start1; count != NULL; count = count->next)
printf("%s\n", count->airlinename);
}
}
airline* enter_flight_menu(airline *start1) // Function to search for the airline entered and enter its flight list menu
{
char airlinename[80];
printf("\n");
printf("Welcome to the Flight list Menu\n");
printf("Enter the Airline name you wish to check\n");
fflush(stdin);
gets(airlinename);
airline *count;
for(count = start1; (count != NULL) && (strcmp(count->airlinename, airlinename) != 0); count = count->next);
if(count == NULL)
{
printf("\n");
printf("Sorry, this airline does not operate in this airport\n");
return start1;
}
else
{
printf("\n");
count->node1 = flight_menu(count->node1);
return start1;
}
}
void write_to_file(airline *start1)
{
FILE *fp;
airline *count1;
flight *count2;
passenger *count3;
int length = 0;
int airline_count = 0;
int passenger_count = 0;
int flight_count = 0;
fp = fopen("airline.txt","wb");
// Determine number of airlines and write to file
for( count1 = start1; count1 != NULL; count1 = count1->next) ++airline_count;
fwrite(&airline_count,sizeof(int),1,fp);
// Loop through and write airlines data to file
for( count1 = start1; count1 != NULL; count1 = count1->next)
{
// Determine length of airline name and write to file
length = strlen(count1->airlinename);
fwrite(&length,sizeof(int),1,fp);
// Write airline name to file
fwrite(count1->airlinename,length,1,fp);
// Determine number of flights for the airline and write to file
for( count2 = count1->node1; count2 != NULL; count2 = count2->next) ++flight_count;
fwrite(&flight_count,sizeof(int),1,fp);
// Loop through and write flight data for the airline to file
for( count2 = count1->node1; count2 != NULL; count2 = count2->next)
{
// Determine length of flight name and write to file
length = strlen(count2->flightname);
fwrite(&length,sizeof(int),1,fp);
// Write flight name to file
fwrite(count2->flightname,length,1,fp);
// Determine length of departure location and write to file
length = strlen(count2->departure_location);
fwrite(&length,sizeof(int),1,fp);
// Write departure location to file
fwrite(count2->departure_location,length,1,fp);
// Determine length of arrival location and write to file
length = strlen(count2->arrival_location);
fwrite(&length,sizeof(int),1,fp);
// Write departure location to file
fwrite(count2->arrival_location,length,1,fp);
// Determine length of take off time and write to file
length = strlen(count2->take_off_time);
fwrite(&length,sizeof(int),1,fp);
// Write departure location to file
fwrite(count2->take_off_time,length,1,fp);
// Determine number of passengers for this flight and write to file
for( count3 = count2->node2; count3 != NULL; count3 = count3->next) ++passenger_count;
fwrite(&passenger_count,sizeof(int),1,fp);
// Loop through and write passenger data for the flight to file
for( count3 = count2->node2; count3 != NULL; count3 = count3->next)
{
// Determine length of passenger name and write to file
length = strlen(count3->name);
fwrite(&length,sizeof(int),1,fp);
// Write passenger name to file
fwrite(count3->name,length,1,fp);
}
}
}
fclose(fp);
}
airline * read_from_file()
{
FILE *fp;
airline *start, *curr, *temp;
int length = 0;
int airline_count= 0;
int flight_count = 0;
int passenger_count = 0;
// Initialize pointers
start = curr = NULL;
// Open file for reading
fp = fopen("airline.txt","rb");
while(!feof(fp))
{
// Read in number of airlines stored in file
if( fp == NULL)
airline_count = 0;
else
fread(&airline_count,sizeof(int),1,fp);
// Loop through "airline_count" airlines
for( ; airline_count > 0; --airline_count )
{
// allocate room for a single airline structure and flight link to NULL
temp = (airline *)malloc(sizeof(airline));
temp->next = NULL;
temp->node1 = NULL;
// read airline name length
fread(&length,sizeof(int),1,fp);
//allocate sufficent memory for the airline name
temp->airlinename = (char *)malloc(length+1);
// read in airline's name and NULL terminate
fread(temp->airlinename,length,1,fp);
temp->airlinename[length] = 0;
// Add current airline to end of growing list of airlines
if( start == NULL )
{
start = curr = temp;
}
else
{
curr->next = temp;
curr = curr->next;
}
// Read in number of flights stored for the airline in file
fread(&flight_count,sizeof(int),1,fp);
// Loop through "flight_count" flight
for( ; flight_count > 0; --flight_count )
{
// allocate room for a single flight structure and set passenger link to NULL
curr->node1 = (flight *)malloc(sizeof(flight));
curr->node1->next = NULL;
curr->node1->node2 = NULL;
// read flight name length
fread(&length,sizeof(int),1,fp);
//allocate sufficent memory for the flight name
curr->node1->flightname = (char *)malloc(length+1);
// read in flight name and NULL terminate
fread(curr->node1->flightname,length,1,fp);
curr->node1->flightname[length] = 0;
// read departure location length
fread(&length,sizeof(int),1,fp);
//allocate sufficent memory for the departure location
curr->node1->departure_location = (char *)malloc(length+1);
// read in departure location and NULL terminate
fread(curr->node1->departure_location,length,1,fp);
curr->node1->departure_location[length] = 0;
// read arrival location length
fread(&length,sizeof(int),1,fp);
//allocate sufficent memory for the arrival location
curr->node1->arrival_location = (char *)malloc(length+1);
// read in arrival location and NULL terminate
fread(curr->node1->arrival_location,length,1,fp);
curr->node1->arrival_location[length] = 0;
// read take off time length
fread(&length,sizeof(int),1,fp);
//allocate sufficent memory for the take off time
curr->node1->take_off_time = (char *)malloc(length+1);
// read in take off time and NULL terminate
fread(curr->node1->take_off_time,length,1,fp);
curr->node1->take_off_time[length] = 0;
// Add current flight to end of growing list of flights
if( start->node1 == NULL )
{
start->node1 = curr->node1 = temp->node1;
}
else
{
curr->node1->next = temp->node1;
curr->node1 = curr->node1->next;
}
// Read in number of passengers stored for that file
fread(&passenger_count,sizeof(int),1,fp);
// Loop through "passenger_count" passengers for this flight
for( ; passenger_count > 0; --passenger_count )
{
// allocate room for a single passenger structure
curr->node1->node2 = (passenger *)malloc(sizeof(passenger));
curr->node1->node2->next = NULL;
// read passenger name length
fread(&length,sizeof(int),1,fp);
//allocate sufficent memory for the passenger name
curr->node1->node2->name = (char *)malloc(length+1);
// read in passenger name and NULL terminate
fread(curr->node1->node2->name,length,1,fp);
curr->node1->node2->name[length] = 0;
// Add current passenger to end of growing list of passengers for this flight
if( start->node1->node2 == NULL )
{
start->node1->node2 = curr->node1->node2 = temp->node1->node2;
}
else
{
curr->node1->node2->next = temp->node1->node2;
curr->node1->node2 = curr->node1->node2->next;
}
}
}
}
}
fclose(fp);
return start;
}
airline* airline_menu(airline *start) // Menu Function which is handles the airlinelist
{
int choice;
printf("Welcome to the Main Menu\n");
printf("Please enter your choice from the menu shown below\n");
printf("1: Add a new airline to this airport\n");
printf("2: Delete an airline from the airport\n");
printf("3: Display the Airlines List at this Airport\n");
printf("4: Enter Flight menu for an airline\n");
printf("5: Exit the program\n");
scanf ("%d", &choice);
switch(choice)
{
case 1:
{
start1 = create_airline_node(start1);
} break;
case 2:
{
start1 = delete_airline_node(start1);
} break;
case 3:
{
print_airline_list(start1);
} break;
case 4:
{
start1 = enter_flight_menu(start1);
} break;
case 5:
{
write_to_file(start1);
exit(1);
} break;
default:
{
start1 = airline_menu(start1);
} break;
}
printf("\n");
start1 = airline_menu(start1);
return start1;
}
//----------------------------- MEMBER FUNCTIONS TO MANIPULATE THE FLIGHT LINKED LIST --------------------------------------------
flight* create_flight_node(flight *start2) // Creates a sorted list of flights (default entry sort condition by flight no.)
{ // by entering the flight node at the sorted position,
printf("\n"); // it calls the function "enter_flight_info() to ask the
if (start2 == NULL) // user for its information
{
start2 = (flight *) malloc(sizeof(flight));
start2 = enter_flight_info(start2);
start2->next = NULL;
}
else
{
flight *count_node1, *count_node2, *node;
node = (flight *) malloc(sizeof(flight));
node = enter_flight_info(node);
for (count_node1 = start2; (count_node1 != NULL) && (strcmp(count_node1->flightname, node->flightname) <= 0); count_node2 = count_node1, count_node1 = count_node1->next);
if(count_node1 == start2)
{
node->next = start2;
start2 = node;
}
else if (count_node1 == NULL)
{
count_node2->next = node;
node->next = NULL;
}
else
{
count_node2->next = node;
node->next = count_node1;
}
}
return start2;
}
flight* enter_flight_info(flight *node) // Function to enter the information about the flight, this function is
{ // called from the function create_flight_node(flight *start)by itself.
printf(" Welcome to the Flight List\n");
printf("Please enter the Flight Name\n");
fflush(stdin);
node->flightname = new char [80];
gets(node->flightname);
printf("Please enter the Departure Location\n");
fflush(stdin);
node->departure_location = new char [80];
gets(node->departure_location);
printf("Please enter the Arrival Location\n");
fflush(stdin);
node->arrival_location = new char [80];
gets(node->arrival_location);
printf("Please enter the Take off time\n");
fflush(stdin);
node->take_off_time = new char [80];
gets(node->take_off_time);
node->node2 = NULL;
return node;
}
flight* delete_flight_node(flight *start2) // Function to delete the name of a flight, function
{ // searches for the name and deletes if it is found.
printf("\n");
if(start2 == NULL)
{
printf("Sorry, no available Flights today\n");
}
else
{
char flightname[80];
printf("Please enter the flight name you want to delete\n");
fflush(stdin);
gets(flightname);
flight *count_node1, *count_node2;
flight *temp;
for(count_node1 = start2; (count_node1 != NULL) && (strcmp(count_node1->flightname, flightname) != 0); count_node2 = count_node1, count_node1 = count_node1->next);
if(count_node1!= NULL)
{
if (count_node1 == start2)
{
temp = start2;
start2 = start2->next;
free(temp);
}
else if(count_node1->next == NULL && count_node1 != start2)
{
temp = count_node1;
count_node2->next = NULL;
free (temp);
}
else
{
temp = count_node1;
count_node2->next = count_node1->next;
free (temp);
}
}
else
{
printf("\n");
printf("Sorry, this name was not found in this list\n");
}
}
return start2;
}
void print_flight_list(flight *start2)
{
printf("\n");
if (start2 == NULL)
{
printf("Sorry, the Flight List is empty\n");
}
else
{
flight *count;
for (count = start2; count != NULL; count = count->next)
{
printf("%s\n", count->flightname);
printf("From %s\n", count->departure_location);
printf("To %s\n", count->arrival_location);
printf("Take off Time : %s\n", count->take_off_time);
printf("\n");
}
}
}
flight* enter_passenger_menu(flight *start2) // Function to search for the flight entered and enter its passenger list menu
{
printf("\n");
char flightname[80];
printf("Welcome to the passenger list Menu\n");
printf("Enter the Flight name you wish to check\n");
fflush(stdin);
gets(flightname);
flight *count;
for(count = start2; (count != NULL) && (strcmp(count->flightname, flightname) != 0); count = count->next);
if(count == NULL)
{
printf("Sorry, this flight is not available\n");
return start2;
}
else
{
count->node2 = passenger_menu(count->node2);
return start2;
}
}
flight* flight_menu(flight *start2) // Menu Function which is handles the flightlist for an airline
{
int choice;
printf("Welcome to the Flight menu\n");
printf("Please enter your choice from the menu shown below\n");
printf("1: Add a new Flight to this Airline\n");
printf("2: Delete a Flight from the Airline\n");
printf("3: Display the Flight List for the Airline\n");
printf("4: Enter passenger menu for a flight\n");
printf("5: Return back to the Main Menu\n");
scanf ("%d", &choice);
switch(choice)
{
case 1:
{
start2 = create_flight_node(start2);
} break;
case 2:
{
start2 = delete_flight_node(start2);
} break;
case 3:
{
print_flight_list(start2);
} break;
case 4:
{
start2 = enter_passenger_menu(start2);
} break;
case 5:
{
return start2;
} break;
default:
{
start2 = flight_menu(start2);
} break;
}
printf("\n");
start2 = flight_menu(start2);
return start2;
}
//----------------------------- MEMBER FUNCTIONS TO MANIPULATE THE PASSENGER LINKED LIST --------------------------------------------
passenger* create_passenger_node(passenger *start3) //Creates a sorted list of passengers by entering the passenger node at the
{ // sorted position, it calls the function "enter_passenger_info() to
printf("\n"); // ask the user for its information
if (start3 == NULL)
{
start3 = (passenger *) malloc(sizeof(passenger));
start3 = enter_passenger_info(start3);
start3->next = NULL;
}
else
{
passenger *count_node1, *count_node2, *node;
node = (passenger *) malloc(sizeof(passenger));
node = enter_passenger_info(node);
for (count_node1 = start3; (count_node1 != NULL) && (strcmp(count_node1->name, node->name) <= 0); count_node2 = count_node1, count_node1 = count_node1->next);
if(count_node1 == start3)
{
node->next = start3;
start3 = node;
}
else if (count_node1 == NULL)
{
count_node2->next = node;
node->next = NULL;
}
else
{
count_node2->next = node;
node->next = count_node1;
}
}
return start3;
}
passenger* enter_passenger_info(passenger *node) // Function to enter the name of the passenger, this function is called
{ // from the function create_passenger_node(passenger *start)by itself.
printf(" Welcome to the passenger list\n");
printf("Please enter your name\n");
fflush(stdin);
node->name = new char [80];
gets(node->name);
return node;
}
passenger* delete_passenger_node(passenger *start3) // Function to delete the name of a passenger in the flight, function
{ // searches for the name and deletes if it is found.
printf("\n");
if(start3 == NULL)
{
printf("\n");
printf("Sorry, the passenger list is empty\n");
}
else
{
char name[80];
printf("Please enter the name you want to delete\n");
fflush(stdin);
gets(name);
passenger *count_node1, *count_node2;
passenger *temp;
for(count_node1 = start3; (count_node1 != NULL) && (strcmp(count_node1->name, name) != 0); count_node2 = count_node1, count_node1 = count_node1->next);
if(count_node1!= NULL)
{
if (count_node1 == start3)
{
temp = start3;
start3 = start3->next;
free(temp);
}
else if(count_node1->next == NULL && count_node1 != start3)
{
temp = count_node1;
count_node2->next = NULL;
free (temp);
}
else
{
temp = count_node1;
count_node2->next = count_node1->next;
free (temp);
}
}
else
{
printf("\n");
printf("Sorry, this name was not found in this list\n");
}
}
return start3;
}
passenger* passenger_menu(passenger *start3) // Menu Function which is called from main and takes
{ // over the program list after that
printf("\n");
int choice;
printf("Welcome to the passenger menu\n");
printf("Please enter your choice from the menu shown below\n");
printf("1: Add a passenger to this flight\n");
printf("2: Delete a passenger from this flight\n");
printf("3: Display the passenger list for this flight\n");
printf("4: Return back to the Flight Menu\n");
scanf ("%d", &choice);
switch(choice)
{
case 1:
{
start3 = create_passenger_node(start3);
} break;
case 2:
{
start3 = delete_passenger_node(start3);
} break;
case 3:
{
print_passenger_list(start3);
} break;
case 4:
{
return start3;
} break;
default:
{
start3 = passenger_menu(start3);
} break;
}
start3 = passenger_menu(start3);
return start3;
}
void print_passenger_list(passenger *start3)
{
printf("\n");
if (start3 == NULL)
{
printf("Sorry, the passenger list is empty\n");
}
else
{
passenger *count;
for (count = start3; count != NULL; count = count->next)
printf("%s\n", count->name);
}
}
//--------------------------------- MAIN FUNCTION CALLING THE MENU FUNCTION ------------------------------------------------
void main()
{
start1 = read_from_file();
start1 = airline_menu(start1);
}