Hi !

This is a program which creates 3D linked list
Linked list of airlines, Each struct holds a node to a list of flights for that airline and and each airline holds a node to a list of passengers for that flight.

Here is some quick background about the program There is a function for each list which creates a node for that list and places it in the alphabetical order in that list.

a)airline* create_airline_node(airline *start1) : creates airline node
b)flight* create_flight_node(flight *start2) : creates flight node
c)passenger* create_passenger_node(passenger *start3): creates passenger node

each node has a for loop which traversers through the list and compares the name entered enteres it at the right position alphabetically.

Now here are my PROBLEMS:

1. the condition tested in the for loops in the functions given above looks like this

(count_node1 != NULL) && (strcmp(count_node1->airlinename, node->airlinename) <= 0)

If i change this condtion to :

(strcmp(count_node1->airlinename, node->airlinename) <= 0) &&
(count_node1 != NULL)

the function doesn't work properly it gives me an error if the second name entered is greater that the first ????????????

any explantion would be a great help

2. I need an idea to iplement this program in files..????

Here is the code:

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

 
struct flight;                               //Forward Declarations 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);
airline* airline_menu(airline *start1);




//-----------------------------------   FLIGHT LIST STRUCTURE AND FUNCTION DECLARATIONS ----------------------------------------

struct passenger;                              //Forward Declarations 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;
	}
}


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");
	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;
	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 = airline_menu(start1);
}