Using Template & linked list to build radix sort

Hello,

Here below is the code snippet where i am not able to build radix for Most Significant Digit (MSD) and also i do not know how to use this code in template?
friend chain<T> bin(chain<T> *a,int x,int y);

and it shows the error message like:
radix.cpp:39:45: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

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
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

template <class T>
class chain;
template <class T>
class node{
	private:T data;
			node<T> *next;
			
	friend class chain<T>;
	friend chain<T> bin(chain<T> *a,int x,int y);
};

template <class T>
class chain{
	private:node<T> *head;
	public: chain(){
				head = NULL;
			}
			
			node<T> *create_node(int x){
				node<T> *newnode = new node<T>();
				newnode->data = x;
				newnode->next = NULL;
				return newnode;
			}
			
			void display();
			void add(int x);
			int max();
			void divide(chain &x1,chain &x2);
			void merge(chain y);
			friend chain<T> bin(chain a,int x,int y);
};

template <class T>
void chain<T>::display(){
	if(head){
		node<T> *ptr = head;
		while(ptr){
			cout<<ptr->data<<" ";
			ptr = ptr->next;
		}
	}
}

template <class T>
void chain<T>::add(int x){
	node<T> *newnode = create_node(x);
	if(head == NULL){
		head = newnode;
	}
	else{
		node<T> *ptr = head;
		while(ptr->next){
			ptr = ptr->next;
		}
		ptr->next = newnode;
	}
}

template <class T>
void chain<T>::divide(chain &a1,chain &a2){//no changes
	if(head){
		node<T> *ptr=head;
		while(ptr)
		{
		if(ptr->data < 0)
			a1.add(ptr->data);
		else
			a2.add(ptr->data);
		ptr=ptr->next;
		}
	}
}

template <class T>
int chain<T>::max(){//no changes
	int m=0;
if(head)
{
	node<T> *ptr=head;
	while(ptr)
	{
		if(abs(m)<abs(ptr->data))
			m=ptr->data;
		ptr=ptr->next;
	}
}
return m;
}

template <class T>
void chain<T>::merge(chain<T> b){// no changes
	node<T> *ptr,*ptr1;
	ptr=b.head;
	ptr1=ptr->next;
	while(ptr)
	{
		ptr->next=head;
		head=ptr;
		ptr=ptr1;
		ptr1=ptr1->next;
	}
}

int digit(int m)//no changes
{
	int l=0;
	m=abs(m);
	while(m>0)
	{
		m = m/10;
		l++;
	}
	return l;
}

template <class T>
chain<T> bin(chain<T> a,int x,int y){//done changes
	
	chain<T> k[10];
	int d;
	int rad=1;
	for(int i=1;i<y;i++)
		rad *= x;
	node<T> *ptr=a.head;
	while(ptr)
	{
		int r = abs(ptr->data);
		d=(r / rad)%x;
		k[d].add(ptr->data);
		ptr=ptr->next;
	}
	chain<T> b;
	d=0;
	node<T> *nn=NULL;
	for(d=0;d<=10;)
	{
		if(nn==NULL)
			nn=k[d++].head;
		else
		{
			b.add(nn->data);
			nn=nn->next;
		}
	}
	return b;
}

template <class T>
chain<T> radix(chain<T> &a){// no changes
	chain<T> a1,a2;
 a.divide(a1,a2);
 int d1,d2;
 d1=digit(a1.max());
 d2=digit(a2.max());
 cout<<d2;
 cout<<"\n\nInitial N List is ";
 a1.display();
 cout<<"\n        P List is ";
 a2.display();
 int i=1;
 while(i<=d2 || i<=d1)
 {
	cout<<"\n\nStep "<<i<<" ";
	if(i<=d2)
		a2=bin(a2,10,i);
	if(i<=d1)
		a1=bin(a1,10,i);
	cout<<" N List is ";
	a1.display();
	cout<<"\n        P List is ";
	a2.display();
	i++;
	//getch();
 }
 a2.merge(a1);
 return a2;
}

int main(int argc, char **argv)// no changes
{
	ifstream fin("output");
 int x;
 chain<int> a;
 fin>>x;
 do
 {
	a.add(x);
 }while(fin>>x);
 cout<<"\nList -";
 a.display();
 //getch();
 a=radix(a);
 cout<<"\n\nSorted List is ";
 a.display();
 //getch();
 return 0;
}

I believe the syntax is something like:
chain<T> bin<>(chain<T> a,int x,int y) { /***/ }
@LowestOne This is absolutely wrong.
Line 15:
friend chain<T> bin(chain<T> *a,int x,int y);
Your compiler at this point does not know the size of chain<T>, so you cannot return a unknown-sized object.
Anyways you can return a pointer to it because pointers are fixed-size.
Topic archived. No new replies allowed.