![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Feb 2005
Posts: 91
| Linked List of Linked list of linked list : problem with a condtion 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);
}
|
| gemini_shooter is offline | |
| | #2 | |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
| Quote:
In the second case, you jump right in and test the values contained in a node with what you are attempting to add/check without knowing if the node exists; only after do you check for the existance of the node (and by this time its too late). This is bad. Do you see the potential problem? It really helps to know if a node exists before you attempt to access its values.
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | |
| hk_mp5kpdw is offline | |
| | #3 |
| Registered User Join Date: Feb 2005
Posts: 91
| Hmmmmmm.......... interesting I understand what you are trying to say, I guess I never noticed this side of it , However it would also never enter that block of else if the list was empty, since there is an if-else around it and that if holds the key for the empty list. Hence if it enter's that block a node always exists so there is always a value to check for that node. I am a little slow at this stuff ..... Any idea on how to implement this using files . It would be a great help !!!! |
| gemini_shooter is offline | |
| | #4 | ||
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
| Quote:
I don't know what surrounds your code that might affect that, but what I said could still apply depending on how your code looked, as an example: Code: if( count_node1 != NULL ) // Start off by making sure we have a node
{
while( (count_node1 != NULL) && (strcmp(...,...) <= 0 ) )
count_node1 = count_node1->next;
// Do stuff
}
Quote:
With data members like char* in your nodes, saving/loading to/from files becomes a bit more of a challenge. When saving this type of data, you basically have to write out the length of the data that is to follow first and then you write out the data (characters) itself. This is so when you read the data back in you can read in this length value and dynamically allocate an appropriate size memory chunk followed by reading in the characters themselves.
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | ||
| hk_mp5kpdw is offline | |
| | #5 |
| Registered User Join Date: Feb 2005
Posts: 91
| Thanks A Lot !!!! Your a genius hk_mp5kpdw !!!!!! I finally got it and ur absolutely right even if the node exists once it goes to the end it looks for the member of a NULL and then DISASTOR STRIKES !!!!! About the files a few questions : 1. Does this file structure sound right to you : one file to store the names of all the airlines operating on the airport. one file for each airline to store the file names and data one file for each flight contaning list of passengers for that flight 2. Is there a way i can copy the entire struct like write and read in C++, I hope there is 3. once I close the program and start it again before even reading member like airline name how would i even know its length ????? now Iam lost again |
| gemini_shooter is offline | |
| | #6 | |||
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
| Quote:
Code: [number of airlines]
[first airline's name]
[number of flights for first airline]
[first airline's, first flight's information]
[first airline's, first flight's number of passengers]
[first airline's, first flight's, first passengers information]
[first airline's, first flight's, next passengers information]
[etc...]
[first airline's, second flight's information]
[first airline's, second flight's number of passengers]
[first airline's, second flight's, first passenger's information]
[first airline's, second flight's, next passenger's information]
[etc...]
[etc...]
[second airline's name]
[etc...]
Quote:
As an example, using the rough format described above, here is how I would start this (just showing the writing of the airline data, not any flight or passenger data): Code: FILE* fp;
struct airline *temp;
int count = 0;
// Open file for writing
fp = fopen("airline.txt","wb"); // Note binary mode
// Determine number of airlines and write to file
for( temp = start1; temp != NULL; temp = temp->next ) ++count;
fwrite(&count,sizeof(int),1,fp);
// Loop through and write airlines data to file
for( temp = start1; temp != NULL; temp = temp->next )
{
// Determine length of airline name and write to file
int length = strlen(temp->name);
fwrite(&length,sizeof(int),1,fp);
// Write airline name to file
fwrite(temp->name,length,1,fp);
// Write airline flight information to file
determine and write number of flights for current airline to file
for( loop through flights for current airline )
{
// Write passenger data to file
determine and write number of passengers for current flight to file
for( loop through passengers in current flight )
{
}
}
}
fclose(fp);
Quote:
As a rough example of the read process: Code: FILE* fp;
struct airline *start, *curr, *temp;
int count;
// Initialize pointers
start = curr = NULL;
// Open file for reading
fp = fopen("airline.txt","rb");
// Read in number of airlines stored in file
if( fp == NULL )
count = 0;
else
fread(&count,sizeof(int),1,fp);
// Loop through "count" airlines
for( ; count > 0; --count )
{
int length;
// read airline name length
fread(&length,sizeof(int),1,fp);
// allocate room for a single airline structure
temp = malloc(sizeof(struct airline));
temp->next = NULL;
temp->name = malloc(length+1);
// read in airline's name and NULL terminate
fread(temp->name,length,1,fp);
temp->name[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 flight information for current airline
read number of flights
for( loop through number of flights just read in)
{
// Read in passenger information
read number of passengers
for( loop through number of passengers for current flight)
{
}
}
}
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | |||
| hk_mp5kpdw is offline | |
| | #7 |
| Registered User Join Date: Feb 2005
Posts: 91
| You are the man hk_mp5kpdw , Thanks for the tip or I should say you solved the whole problem for me .. Thanks again man !!! |
| gemini_shooter is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linked List Not Saving Value as Int | bar338 | C Programming | 4 | 05-04-2009 07:53 PM |
| Need help sorting a linked list. Beginner | scarlet00014 | C Programming | 1 | 09-27-2008 06:16 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C Programming | 3 | 03-04-2005 02:46 PM |
| singly linked list | clarinetster | C Programming | 2 | 08-26-2001 10:21 PM |