Linked List Template Class

Hi everyone I am having a bit of an issue implementing a template linked list class with a merge member function.

The merge should take another linked list object(it should already be in order) and merge it with the base object(also in order) to produce a single linked list.

I initially made the class without templates and was able to make the merge function correctly. Unfortunately I am very poor with the template keyword and botched my program up.

Here is the relevant code if you think its necessary I can upload all of it.

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
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using std::cout;
using std::endl;
template <class T>
class Node
{
public:
	T value;
	Node<T> *next;
	Node(T num)
	{
		value = num;
		next = nullptr;
	}
};

template <class T>
class Linkedlist
{
private:
public:
	Node<T> *head;            // List head pointer
	Linkedlist()
	{
		head = nullptr;
	}
	//signatures
	~Linkedlist();
	T back();
	void display() const;
	void push_back(T);
	bool empty();
	void push_front(T);
	T front();
	void erase(T);
	void insert(T);
	void pop_back();
	void pop_front();
	int size();
	void reverse();
	Node<T>* merge(Linkedlist<T>);
};

template <class T>
Node<T>* Linkedlist<T>::merge(Linkedlist<T> b) //merge sort
{
	Node<T> *current;
	current = nullptr;
	Node<T> *temp1;
	Node<T> *temp2;
	temp1 = this->head;
	temp2 = b.head;
	if (temp1 == nullptr)
	{
		this->head = temp2;
		return this->head;
	}
	else if (temp2 == nullptr)
	{
		return this->head;
	}
	if (temp1->value <= temp2->value)
	{
		current = temp1;
		this->head = temp1->next;
		current->next = merge(b);
	}
	else
	{
		current = temp2;
		b.head = temp2->next;
		current->next = merge(b);
	}
	this->head = current;
	return this->head;
}
#endif 


I realize this code looks sloppy but this is what I ended up with after numerous failures. Any assistance or advice is very appreciated. Thanks.
Topic archived. No new replies allowed.