Sorting in Linked List

Can anyone please help me that how do I do sorting in a linked list? I have to use Bubble Sort to done my assignment.
This is the sorting part of the code.

void sorting ()
{
ListNode * temphead = head;
ListNode * tempnode = NULL;
int counter = 0;
while (temphead)
{
temphead = temphead->next;
counter++;
}
for (int i=0; i<counter; i++)
{
for (int j=0; j<counter-i; j++)
{
if (temphead->roll > temphead->next->roll)
{
tempnode = temphead;
temphead = temphead->next;
temphead->next = tempnode;
}
}
}
}
the others probably want to see the full code, and please use [code] [/code] tag
It might be easier (conceptually) to swap the node data rather than the nodes themselves.
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
struct ListNode
{
	string name;
	int roll;
	ListNode * next;
};
ListNode * head = NULL;
void append (string, int);
void insertname (string, int);
void insertroll (string, int);
void deletenode (int);
void deletenode (string);
void deleteall ();
void printing ();
void sorting ();
void exitprog ();
void main ()
{
	int select, roll, num;
	string name;
	while (1)
	{
		cout<<"Press 1 for appending the node"<<endl;
		cout<<"Press 2 for inserting the node"<<endl;
		cout<<"Press 3 for deleting the node"<<endl;
		cout<<"Press 4 for deleting the list"<<endl;
		cout<<"Press 5 for printing the list"<<endl;
		cout<<"Press 6 for sorting the list"<<endl;
		cout<<"Press 7 to exit the program"<<endl;
		cin>>select;
		switch (select)
		{
		case 1:
			cout<<"Please enter the name of the student: ";
			cin.ignore (10000,'\n');
			getline (cin, name);
			cout<<"Please enter the roll number of the student: ";
			cin>>roll;
			append (name, roll);
			break;
		case 2:
			cout<<"Press 1 to sort on the basis of name or 2 to sort on the basis of roll number: ";
			cin>>num;
			cout<<"Please enter the name of the student: ";
			cin.ignore (10000,'\n');
			getline (cin, name);
			cout<<"Please enter the roll number of the student: ";
			cin>>roll;
			if (num == 1)
			{
				insertname (name, roll);
			}
			else if (num == 2)
			{
				insertroll (name, roll);
			}
			break;
		case 3:
			cout<<"Press 1 to delete the node on the basis of name or 2 to delete on the basis of roll number: ";
			cin>>num;
			if (num == 1)
			{
				string value;
				cout<<"Please enter the name: ";
				cin.ignore (10000,'\n');
				getline (cin, value);
				deletenode (value);
			}
			else if (num == 2)
			{
				int value;
				cout<<"Please enter the roll number: ";
				cin>>value;
				deletenode (value);
			}
			else
			{
				cout<<"You have entered an invalid input...!"<<endl;
			}
			break;
		case 4:
			deleteall ();
			break;
		case 5:
			printing ();
			break;
		case 6:
			sorting ();
			break;
		case 7:
			exitprog ();
		default:
			cout<<"You have entered an invalid input...!"<<endl;
			break;
		}
	}
}
void append (string name, int roll)
{
	ListNode * temphead = head;
	ListNode * temp = new ListNode;
	temp->name = name;
	temp->roll = roll;
	temp->next = NULL;
	if (head == NULL)
	{
		head = temp;	
	}
	else
	{
		while (temphead->next)
		{
			temphead = temphead->next;
		}
		temphead->next = temp;
	}
}
void insertname (string name, int roll)
{
	ListNode * temp = new ListNode;
	temp->next = NULL;
	temp->name = name;
	temp->roll = roll;
	if (head == NULL)
	{
		head = temp;
	}
	else
	{
		ListNode * temphead = head;
		ListNode * previous = NULL;
		while (temphead != NULL && name > temphead->name)
		{
			previous = temphead;
			temphead = temphead->next;
		}
		if (previous == NULL)
		{
			head = temp;
			temp->next = temphead;
		}
		else if (temphead == NULL)
		{
			previous->next = temp;
		}
		else
		{
			previous->next = temp;
			temp->next = temphead;
		}
	}
}
void insertroll (string name, int roll)
{
	ListNode * temp = new ListNode;
	temp->next = NULL;
	temp->name = name;
	temp->roll = roll;
	if (head == NULL)
	{
		head = temp;
	}
	else
	{
		ListNode * temphead = head;
		ListNode * previous = NULL;
		while (temphead != NULL && roll > temphead->roll)
		{
			previous = temphead;
			temphead = temphead->next;
		}
		if (previous == NULL)
		{
			head = temp;
			temp->next = temphead;
		}
		else if (temphead == NULL)
		{
			previous->next = temp;
		}
		else
		{
			previous->next = temp;
			temp->next = temphead;
		}
	}
}
void deletenode (int roll)
{
	ListNode * temphead = head;
	ListNode * previous = NULL;
	ListNode * tempnext = NULL;
	while (temphead->roll != roll && temphead != NULL)
	{
		previous = temphead;
		temphead = temphead->next;
	}
	if (temphead == NULL)
	{
		cout<<"Sorry no data found!!!"<<endl;
	}
	else if (previous == NULL)
	{
		tempnext = temphead->next;
		head = tempnext;
		delete temphead;
	}
	else
	{
		tempnext = temphead->next;
		previous->next = tempnext;
		delete temphead;
	}
}
void deletenode (string name)
{
	ListNode * temphead = head;
	ListNode * previous = NULL;
	ListNode * tempnext = NULL;
	while (temphead->name != name && temphead != NULL)
	{
		previous = temphead;
		temphead = temphead->next;
	}
	if (temphead == NULL)
	{
		cout<<"Sorry no data found!!!"<<endl;
	}
	else if (previous == NULL)
	{
		tempnext = temphead->next;
		head = tempnext;
		delete temphead;
	}
	else
	{
		tempnext = temphead->next;
		previous->next = tempnext;
		delete temphead;
	}
}
void deleteall()
{
	ListNode * temphead = head, * temp;
	while (temphead)
	{
		temp = temphead->next;
		delete temphead;
		temphead = temp;
	}
	head = NULL;
}
void printing ()
{
	if (head == NULL)
	{
		cout<<"There is nothing to print."<<endl;
		return;
	}
	ListNode * temphead = head;
	while (temphead)
	{
		cout<<temphead->name<<' '<<temphead->roll<<endl;
		temphead = temphead->next;
	}
}
void sorting ()
{
	ListNode * temphead = head;
//	ListNode * tempnode = NULL;
	int temproll;
	string tempname;
	int counter = 0;
	while (temphead)
	{
		temphead = temphead->next;
		counter++;
	}
	for (int i=0; i<counter; i++)
	{
		for (int j=0; j<counter-i; j++)
		{
			if (temphead->roll > temphead->next->roll)
			{
//				tempnode = temphead;
//				temphead = temphead->next;
//				temphead->next = tempnode;
				temproll = temphead->roll;
				temphead->roll = temphead->next->roll;
				temphead->next->roll = temproll;

				tempname = temphead->name;
				temphead->name = temphead->next->name;
				temphead->next->name = tempname;
			}
		}
	}
}
void exitprog ()
{
	exit (0);
}
naraku9333 i have tried to sort through values but its still not working. I have problem just in sorting. Otherwise whole code is running smoothly perfect.
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
void sorting ()
{
	ListNode * temphead = head;
	//ListNode * tempnode = NULL;
	int temproll;
	string tempname;
	int counter = 0;
	while (temphead)
	{
		temphead = temphead->next;
		counter++;
	}
	temphead = head;
	
	for (int j=0; j<counter; j++)
	{
		while (temphead->next)  //iterate through list until next is null
		{
			if (temphead->roll > temphead->next->roll)
			{
				/*tempnode = temphead;
				temphead = temphead->next;
				temphead->next = tempnode;*/
				temproll = temphead->roll;
				temphead->roll = temphead->next->roll;
				temphead->next->roll = temproll;

				tempname = temphead->name;
				temphead->name = temphead->next->name;
				temphead->next->name = tempname;
				temphead = temphead->next;//increment node
			}
			else 
				temphead = temphead->next;//increment node
		}	
		temphead = head;//reset temphead
	}
}

Thank you very much. I shall always be thankful to you.
This is the switch case part.
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

		case 6:
			cout<<"Press 1 to sort in ascending order or 2 to sort in descending order: ";
			cin>>num;
			if (num == 1)
			{
				cout<<"Press 1 to sort on the basis of name or 2 to sort on the basis of roll number: ";
				cin>>num2;
				if (num2 == 1)
				{
					asc_name_sorting ();
				}
				else if (num2 == 2)
				{
					asc_roll_sorting ();
				}
				else
				{
					cout<<"You have entered an invalid input...!"<<endl;
				}
			}
			else if (num == 2)
			{
				cout<<"Press 1 to sort on the basis of name or 2 to sort on the basis of roll number: ";
				cin>>num2;
				if (num2 == 1)
				{
					des_name_sorting ();
				}
				else if (num2 == 2)
				{
					des_roll_sorting ();
				}
				else
				{
					cout<<"You have entered an invalid input...!"<<endl;
				}
			}
			else
			{
				cout<<"You have entered an invalid input...!"<<endl;
			}
			asc_roll_sorting ();
			break;

This is the implementations functions.
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
void asc_name_sorting ()
{
	ListNode * temphead = head;
	int temproll;
	string tempname;
	int counter = 0;
	while (temphead)
	{
		temphead = temphead->next;
		counter++;
	}
	temphead = head;
	
	for (int j=0; j<counter; j++)
	{
		while (temphead->next)
		{
			if (temphead->name > temphead->next->name)
			{
				temproll = temphead->roll;
				temphead->roll = temphead->next->roll;
				temphead->next->roll = temproll;

				tempname = temphead->name;
				temphead->name = temphead->next->name;
				temphead->next->name = tempname;
			}
			temphead = temphead->next;
		}	
		temphead = head;
	}
}
void asc_roll_sorting ()
{
	ListNode * temphead = head;
	int temproll;
	string tempname;
	int counter = 0;
	while (temphead)
	{
		temphead = temphead->next;
		counter++;
	}
	temphead = head;
	
	for (int j=0; j<counter; j++)
	{
		while (temphead->next)
		{
			if (temphead->roll > temphead->next->roll)
			{
				temproll = temphead->roll;
				temphead->roll = temphead->next->roll;
				temphead->next->roll = temproll;

				tempname = temphead->name;
				temphead->name = temphead->next->name;
				temphead->next->name = tempname;
			}
			temphead = temphead->next;
		}	
		temphead = head;
	}
}
void des_name_sorting ()
{
	ListNode * temphead = head;
	int temproll;
	string tempname;
	int counter = 0;
	while (temphead)
	{
		temphead = temphead->next;
		counter++;
	}
	temphead = head;
	
	for (int j=0; j<counter; j++)
	{
		while (temphead->next)
		{
			if (temphead->name < temphead->next->name)
			{
				temproll = temphead->roll;
				temphead->roll = temphead->next->roll;
				temphead->next->roll = temproll;

				tempname = temphead->name;
				temphead->name = temphead->next->name;
				temphead->next->name = tempname;
			}
			temphead = temphead->next;
		}	
		temphead = head;
	}
}
void des_roll_sorting ()
{
	ListNode * temphead = head;
	int temproll;
	string tempname;
	int counter = 0;
	while (temphead)
	{
		temphead = temphead->next;
		counter++;
	}
	temphead = head;
	
	for (int j=0; j<counter; j++)
	{
		while (temphead->next)
		{
			if (temphead->roll < temphead->next->roll)
			{
				temproll = temphead->roll;
				temphead->roll = temphead->next->roll;
				temphead->next->roll = temproll;

				tempname = temphead->name;
				temphead->name = temphead->next->name;
				temphead->next->name = tempname;
			}
			temphead = temphead->next;
		}	
		temphead = head;
	}
}

Now the problem is that the sorting is not going perfect. It is just sorting in ascending order of roll numbers every time, and I have to do sorting in ascending order as well as in descending order. And on the basis of both name and roll number.
Oops sorry it was an extra line in the end of case part. Thanks :)
Topic archived. No new replies allowed.