linked list copy constructor

is my copy constructor for linked list right??

1
2
3
4
5
6
7
8
9
10
11
12
LINKED_LIST<T>::LINKED_LIST(const LINKED_LIST &aList)
{
	ListNode *p1 = head;
	ListNode *p2 = aList.head;
	for (unsigned i = 0; i < aList.size)
	{
		p1->data = p2->data;
		p1 = p1->next;
		p2 = p2->next;
	}

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <class T> 
class LINKED_LIST
{
private:

	struct ListNode
	{
		T data; 
		ListNode *next; 
	};

	unsigned int size; 
	ListNode *head; 

public:

	LINKED_LIST(); 
	LINKED_LIST(const LINKED_LIST &aList); // copy constructor
}
.....
.....



thks
you make a shallow copy of the list. When list1 destroys an element list2 will still reference it and will probably crash later. So no it's not corrrect.

You need to create a new ListNode for each element in aList
here is insert after index
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
int LINKED_LIST<T>::insert(unsigned int index, T newItem)
{
	ListNode *pNewNode = new ListNode;
	pNewNode->data = newItem;
	pNewNode->next = NULL;

	if (index > size) return 0; // fail
	else 
		if (index == 0) // addhead
		{
			if (head == NULL) 
			{
				head = pNewNode;
			}
			else
				{	
					pNewNode->next = head;
					head = pNewNode;
					size++;
				}
			
		}
		else
			{
				ListNode *p = head;
				unsigned pos = 0;
				while (pos != index - 1)
				{
					p = p->next;
					pos++;
				}

				pNewNode->next = p->next;
				p->next = pNewNode;
				size++;
			}
	return 1; // succeed
}


is it correct?

thks
it not run, i dont know why>
Your copy constructor needs to update size also. If there are other constructors, make sure they do too,

insert() needs to update size at line 13.
thks
Last edited on
Here is my code
but still not working
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <class T>
LINKED_LIST<T>::LINKED_LIST(const LINKED_LIST &aList)
{
	ListNode *p = aList.head;

	//create first node
	ListNode *q = head;
	q->data = p->data;
	q->next = NULL;
	size++;
	p = p->next;

	for (unsigned int i = 2; i <= aList.size; i++)
	{
		T newItem = p->data;
		p = p->next;
		insert(i,newItem);
		size++;
	}

}
This is a constructor. That means that upon entry, head and size contain garbage and must be initialized before you can use them.
This is a constructor. That means that head and size contain garbage on entry and must be initialized. Line 7 and 10 assume that they contain something valid.
Here is insertion

PLZ check for me

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
int LINKED_LIST<T>::insert(unsigned int index, T newItem)
{
	ListNode *pNewNode = new ListNode;
	pNewNode->data = newItem;
	pNewNode->next = NULL;

	if (index > size) return 0; // fail
	else 
		if (index == 0) // addhead
		{
			if (head == NULL) 
			{
				head = pNewNode;
				size++;
				return 1;
			}
			else
				{	
					pNewNode->next = head;
					head = pNewNode;
					size++;
					return 1;
				}
			
		}
		else
			if (index == size) //addtail
			{
				ListNode *p = head;
				unsigned int pos = 0;
				while (pos != index - 1)
				{
					p = p->next;
					pos++;
				}
				p->next = pNewNode;
				size++;
			}
			else
				{
					ListNode *p = head;
					unsigned int pos = 0;
					while (pos != index - 1)
					{
						p = p->next;
						pos++;
					}

					pNewNode->next = p->next;
					p->next = pNewNode;
					size++;
					return 1;
				}
	return 0;
}
Generally the code looks pretty good. I do have a couple of comments, though:

Line 7 is a memory leak. You have allocated pNewNode, and then, if your index is out of range, you return without deleting it.

The last element in the list has next = NULL. There is no harm reassigning the this value in pNewNode if it happens to be inserted at the end of the list. So, you can completely get rid of lines 27 - 39.

Edit: Oh yeah. Why return an int? A bool indicating success or failure might be cleaner.
Last edited on
Topic archived. No new replies allowed.