This is a method I used for adding a object, flightnode, into a linked list. Basically it takes in the value in and puts it in order based on the int flightno in the flightnode object. It works fine until a value needs to go at the end of the list (assuming the list isn't empty at time of insertion) and gives me a seg fault error. I'm pretty sure its a subtle problem.
Below is the driver I used to test the program. Somewhat related this program acts completely different in another complier, any idea why?
void linkedlist::AddFlight(int flightno){
flightnode *f = new flightnode(flightno);
flightnode *temp = new flightnode(); //iterates through the list starting from 1st node
flightnode *temp2 = new flightnode();//keeps track for the temp's previous value
if( length == 0 ){ //if list is empty, then new node is first node
first_node_f = f;
length++;
return;
}//end if statement
else{
temp = first_node_f;
while( temp->next != NULL ){
if(f->flight_number == temp->flight_number ){
cout << "Flight number in use" << endl;
return;
}//end if statement
else if( f->flight_number < temp->flight_number ){//add somewhere in the middle of the list
if( temp != first_node_f){//if not first node, put between two nodes
f->next = temp;
temp2->next = f;
length++;
return;
}//end if
else{//if firstnode then put in front
f->next = temp;
first_node_f = f;
length++;
return;
}//end else
}//end else if
else{//iterate to next node for checking
temp2 = temp;
if( temp->next != NULL ){
temp = temp->next;
}
else
break;
}
}//end while statement
cout << "Working 2" << endl;
temp = f;//put element at end of the list\
cout << "Working 3" << endl;
temp2->next = f;
cout << "Working 4" << endl;
length++;
return;
}//end else statement
#include <iostream>
#include "LinkedList.cpp"
using namespace std;
int main(){
linkedlist *airline = new linkedlist();
flightnode *fnode2 = new flightnode(44);
cout << airline->length << endl;
//airline->AddFlight(34);
//cout << airline->first_node_f->flight_number << " " << airline->length << " AddFlight(34)" << endl;
airline->AddFlight(53);
cout << airline->first_node_f->flight_number << " " << airline->length << " AddFlight(53)" << endl;
airline->AddFlight(17);
cout << airline->first_node_f->flight_number << " " << airline->length << " AddFlight(17)" << endl;
airline->AddFlight(17);
cout << airline->first_node_f->flight_number << " " << airline->length << " AddFlight(17)" << endl;
airline->AddFlight(14);
cout << airline->first_node_f->flight_number << " " << airline->length << " AddFlight(14)" << endl;
airline->AddFlight(21);
cout << airline->first_node_f->flight_number << " " << airline->length << " AddFlight(21)" << endl;
airline->AddFlight(18);
cout << airline->first_node_f->flight_number << " " << airline->length << " AddFlight(18)" << endl;
cout << airline->first_node_f->flight_number << endl;
cout << airline->first_node_f->next->flight_number << endl;
cout << airline->first_node_f->next->next->flight_number << endl;
cout << airline->first_node_f->next->next->next->flight_number << endl;
system("pause");
}