Copy constructor in double LinkedList

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..

Many Thanks
Jessi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
using namespace 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(const int& searchItem) const;
       void insert(const int& insertItem) ;
       void deleteNode(const int& 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(const int& 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;
}
Last edited on
Hi,

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.


After a quick look at copyList...

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.
Last edited on
Topic archived. No new replies allowed.