I am trying to implement a copy constructor for a double linked list..
The program hangs at the copying part at list2 = list
Can anyone please help me..
#include <iostream>
usingnamespace std;
struct Node
{
int info;
Node *next;
Node *back;
};
class DoubleLinkedList
{
protected:
Node *first;
Node *last;
int count;
public:
DoubleLinkedList();
void initializeList() ;
DoubleLinkedList(const DoubleLinkedList& otherList) ;
void copyList(const DoubleLinkedList& otherList) ;
bool search(constint& searchItem) const;
void insert(constint& insertItem) ;
void deleteNode(constint& deleteItem) ;
void insertbeg(int newItem);
const DoubleLinkedList& operator=(const DoubleLinkedList&) ;
// void copyList(const DoubleLinkedList& otherList) ;
void print() ;
void reversePrint() const;
void insertend(int newItem);
void deleteNode(int deleteItem);
void buildListForward();
int front() const;
~DoubleLinkedList();
void traverse_and_print();
void destroyList();
};
const DoubleLinkedList& DoubleLinkedList:: operator=(const DoubleLinkedList& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList) ;
}//end else
return *this;
}
void DoubleLinkedList :: copyList(const DoubleLinkedList& otherList)
{
Node *newNode = new Node; //pointer to create a node
Node *current; //pointer to traverse the list
if (otherList.first == NULL) //otherList is empty
{
first = NULL;
last = NULL;
count = 0;
}else
{
current = otherList.first; //current points to the list to be copied
count = otherList.count;//copy the first node
//first = new Node; //create the node
first->info = current->info; //copy the info
first->next = current->next; //set the link field of the node to NULL
first->back = current->back;
last = first; //make last point to the first node
current = current->next; //make current point to the next node
//copy the remaining list
while (current != NULL)
{
newNode = new Node; //create a node
newNode->info = current->info; //copy the info
newNode->next = current->next;
newNode->back = current->back;
last->next = newNode;
last = newNode;
current = current->next;
}
}
}
DoubleLinkedList::DoubleLinkedList()
{
first = last = NULL;
count = 0;
}
void DoubleLinkedList ::destroyList()
{
Node *temp; //pointer to delete the node
while (first != NULL)
{
temp = first;
first = first->next;
delete temp;
}
last = NULL;
count = 0;
}
void DoubleLinkedList:: initializeList()
{
destroyList();
}
void DoubleLinkedList:: print()
{
Node * current; //pointer to traverse the list
current = first; //set current to point to the first node
while (current != NULL)
{
cout << current->info << " "; //output info
current = current->next;
}//end while
}//end print
void DoubleLinkedList:: insert(constint& insertItem)
{
Node * current; //pointer to traverse the list
Node * trailCurrent; //pointer just before current
Node * newNode = new Node; //pointer to create a node
bool found;
newNode->info = insertItem; //store the new item in the node
newNode->next = NULL;
newNode->back = NULL;
if (first == NULL) //if list is empty, newNode is the only node
{
first = newNode;
last = newNode;
count++;
}else
{
found = false;
current = first;
while (current != NULL && !found) //search the list
{
if (current->info >= insertItem)
found = true;
else
{
trailCurrent = current;
current = current->next;
}
}
if (current == first) //insert newNode before first
{
first->back = newNode;
newNode->next = first;
first = newNode;
count++;
}else
{
//insert newNode between trailCurrent and current
if (current != NULL)
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
newNode->next = current;
current->back = newNode;
}else
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
last = newNode;
}count++;
}
}
}
DoubleLinkedList::~DoubleLinkedList()
{
destroyList();
}
int main()
{
DoubleLinkedList list1,list2;
int num;
cout << " Enter the numbers to form your list" << endl;
while (num != -999)
{
cin >> num ;
list1.insert(num);
}
cout << "Print the list now" << endl;
list1.print();
cout << endl;
cout << " Copy the list to list2" << endl;
list2 = list1;
cout << "Print the list2 now" << endl;
list2.print();
return 0;
}
You aren't using a copy constructor - instead you are using operator=, which calls the copyList function, but it's commented out of the class definition.
If you are going to implement a copy ctr the code would be in:
1 2 3
DoubleLinkedList::DoubleLinkedList(const DoubleLinkedList& otherList) {
// copy ctr code here
}
By far the easiest way to diagnose runtime problems, is to use a debugger.
I haven't looked at the details of the copyList function.
1. what happens to the node created on line 54? (esp. if you're copying from an empty list!)
2. what happens if there are existing nodes in the list being copied to?
By far the easiest way to diagnose runtime problems, is to use a debugger.
It can also help to add diagnostic o/p. At it's simplest this would just be couts (pref protected by #ifdef so they can be switched in and out easily.)
And to write automatic test function (which don't need user interaction.) If you modified the print method to take a ostream& it could be used to write to an ostringstream as well as cout so you could even check the list contents easily.
Andy
PS Try to declare variables in as tight a scope as poss. Even C (as of C99) doesn't need variables declared at the head of a function! It can make things easier to follow in some cases.