Help converting a method using pointer to use copy constructor instead

Imagine the method below is fully functional. However professor is saying it is not acceptable to use a pointer. I could use a copy constructor instead to accomplish it. Can someone tell me steps to approach a copy constructor needed to allow me to remove the pointer reference from this SortedList SortedList::merge method please?

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
SortedList* SortedList::merge(const SortedList& other) const
{
	//List A
	node* a = head;
	//Second list B (other)
	node* b = other.head;
	//Created third doubly linked list
	node* x;
	//Create a variable to manipulate nodes
	node* prev;
	//Set the previous node as nullptr to initialize it
	prev = nullptr;
	//Create an object
	SortedList* sort = new SortedList;
	//Error handling
	if (sort == nullptr)
		return nullptr;
	
	//Traverse in lists a while it is not null
	while (a != nullptr && b != nullptr)
	{
		//Now compare first element of list a against ordered, first element of list b
		if (a->num < b->num)
		{
			//Create list x
			x = new node;
			//Error handling
			if (x == nullptr)
				return sort;

			//Assign nullptr to x->next (that is the end of list scenario)
			x->next = nullptr;
			//Error handling. If previous node was not null...
			if (prev != nullptr)		
				//... then assign pointer x to prev->next 
				prev->next = x;
			//if the head is the only element, assign sort.head = x
			if (sort->head == nullptr)
				sort->head = x;
		    //
			x->prev = prev;
			//Assign data value of a to final list x, since a value is less than b value.
			x->num = a->num;
			//Traverse to next hop on list a
			a = a->next;
		}
		//Else means, vaue of element in list a is greater than value in element list b
		else
		{   
			//Initialize node x
			x = new node;
			if (x == nullptr)
				return sort;
		
			x->next = nullptr;
			//Error handling, checking if previous node is not null
			if (prev != nullptr)
				//Then assign prev->next to pointer to point to x element
				prev->next = x;
			//Error handling checking if object sort is null
			if (sort->head == nullptr)
				//In that case,  value of x is now the head of the sorted list.
				sort->head = x;
			x->prev = prev;
			//Consider value of b to be the final value in order in list x
			x->num = b->num;
			//Traverse to next hop in the list b
			b = b->next;

		}
		prev = x;
	}
	while (b != nullptr) {
		//if that is the end of a list
		x = new node;
		if (x == nullptr)
			return sort;
		x->next = nullptr;
		if (prev != nullptr)
			prev->next = x;
		if (sort->head == nullptr)
			sort->head = x;
		x->prev = prev;

		x->num = b->num;
		b = b->next;
		prev = x;
		}

	while (a != nullptr) {
		x = new node;
		if (x == nullptr)
			return sort;
		x->next = nullptr;
		if (prev != nullptr)
			prev->next = x;
		if (sort->head == nullptr)
			sort->head = x;
		x->prev = prev;
		x->num = a->num;
		a = a->next;
		prev = x;
	}
	//Get the length of the two lists given
	sort->count = getLength() + other.getLength();
	//Return the final sorted list
	return sort;
}
//End**** 
Last edited on
http://www.cplusplus.com/articles/jEywvCM9/
Please edit your post for readability.
Done salem c.
appreciated if anyone can help.
Topic archived. No new replies allowed.