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");
        
        
        
        
    }

Recommended Answers

All 3 Replies

First: Memory that is allocated must be deleted. If you allocate memory
like f/temp/temp2 and then hit a return you have 100% guaranteed to have a memory leak.

Second: you need to post your declaration of flightnode and other
classes your are using.

Third: Your "end else if" comments just clutter stuff up. The code tells you this. you dump that BUT keep the informational comments. e.g.
"add somehere in the middle".

Fourth you would be better off reallizing that you could do this with less if/else and have two pointer, that bracket the entry position. Then set if not null but use regardless to set.

Linked list A-B-C-D
New node=N
Insert between C and D. 
T1=C 
T2=D
if (T1) T1->setNext(N)
if (T2) T2->setPrev(N)

Thank you for the reply. I've fixed the problem by adding a tail pointer but I'm concerned about the memory leaks so thanks for that. I assume its why my implementation doesn't work on the redhat linux that I'm required to run it on but works fine on my PC.

1) How should I deallocate the memory, by using delete or setting them to null?

2) Well I've fixed my problem.

3) The comments like end ifelse are strictly for me to know where in each loop/control statement I'm in. I delete them when I finish my program. When I remove or add something they are extremely helpful for me.

4) I wanted to use a singly linked list.

Good, you are making progress then.

Quick comment: point 4.

A singularly linked list is set up the same way as a double linked list EXCEPT that my step 7 is skipped.

Comment on 1: Consider this:

// single allocation
double* Ptr=new double;
delete Ptr;
// multiple allocations:
int* IPtr=new int[10];
delete [] IPtr;

Use the new/delete in pairs. AND be VERY careful that if you have a set of if/else were you have multiple return points, you delete the memory regardless of the path out of the function.

Later you will start using things like boost::shared_ptr and the problem will go away. [to be replaced with much less-frequent but more difficult to find deletion before lifetime over problems] but this is a lot of code in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.