I am working on a doubly linked list assignment for my class and I am getting a segmentation fault on my test for the copy constructor. I don't know if the problem lies within my copyList method or my copy constructor logic. Any assistance be great.
I recommend you think twice about simple copying of head and tail pointers to copy one list to another. This is called shallow copy and works okay in simple programs but it can quickly cause problems if you aren't aware of what is happening. In particular, under this scenario if either list is deleted then the other list is useless because there is now no longer any place in memory to point to.
I recommend doing hard copy within the copy constructor, copy function. That way each list has it's own memory, even if it contains the same information.
Don't try to copy an empty list. If the list to copy is empty, then return from the function/constructor.
Mock up copy constructor. Code untested. I'm comfortable with the logic and the comments. Adjustable to your needs as desired.
//use deep copy protocol to copy rhs into a new List using a copy constructor.
//type Node is used to create type List.
//List is doubly linked
//List always adds to the end
List(const List & rhs)
if(rhs.count == 0) //rhs is empty, so assign default values to new List member variables
count = 0;
firstNode = NULL;
lastNode = NULL;
//declare a Node to run rhs, start with first node in rhs.
Node * current = rhs.firstNode;
//assign default values to member variables of new List
count = 0;
firstNode = lastNode = NULL;
//use a loop to copy data from rhs into new List one Node at a time
while(current != NULL)
//create a new Node to enter into the new List
Node newNode = new Node;
newNode->prev = NULL;
newNode->next = NULL;
newNode->data = current->data;
//add new Node to new List
if(count == 0) //new list is empty
firstNode = lastNode = newNode;
else //add new Node to end of new List
lastNode->next = newNode;
newNode->prev = lastNode;
lastNode = lastNode->next;
//increment count and get next node in rhs to copy into new List
current = current->next;
If code compiles but doesn't provide the response you expect then run time debugging is what's called for. Either learn to use a debugger----many IDEs come with one built in or you can download one if you don't have an IDE that has it's own----OR-----toss in well placed output statements, program flags and program pauses to out put results of desired variables on the fly and that you are where you think you are with each step of the function/loop; and be sure to remove the debugging code before deploying the final project. In this case I'd first put code in to display the lists being compared. If you have the correct lists then I'd display the values of p1->data and p2->data each time through the while loop to be sure you're getting the right values compared.
For some reason I am not seeing it. Don't I want to check that both pointers are NULL? That way I enter the while loop when something is in the list. When in the loop I check if the data in p1 is LESS than the data in p2 and since thats what I was testing for that would return true? Or if p1 is greater than p2 i return false? Then advance the list?
Ah I just don't know what I'm missing logically I keep looking at it and I just can't seem to put my finger on it.