selection sorting pointers

Hello. I had written a program where 10 numbers are infiled and placed on a linked list. They are then sorted using selection sort. I sorted the values not the pointers. Now I have to write the exact same program but I need to sort the pointers not the values. I have never sorted pointers before so I was wondering if someone could give me a few tips and get me going in the right direction. Here is the first program I wrote that sorted the values...

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
  #include <iostream>
#include  <fstream>
using namespace std; 

struct ListNode 
{ 
	double value; 
	ListNode *next;
	ListNode(double value1, ListNode *next1 = NULL)
	{
		value = value1; 
		next = next1;
	}
}; 

int main() 
{ 
	ifstream inFile; 
	inFile.open("grades2.txt"); 
	ListNode *head= NULL; 
	double value; 
	for (int x=0; x<10; x++)
	{ 
		inFile>>value; 
		head = new ListNode(value,head);
	} 
	inFile.close();

	//code for selection sort
	ListNode *h = head, *current, *j;
	for(current = h; current!=NULL && current->next != NULL; current = current->next)
	{
		ListNode *min;	//node to contain node with minimum value
		min = current;
		for(j = current->next; j != NULL ; j = j->next)
		{
			if(j->value < min->value)	
			min = j;	//assign reference of min node
		}

		if(min != current)
		{
			//swap with the current node
			double temp;
			temp = min->value;
			min->value = current->value;
			current->value = temp;
		}
	}
	head = h;
	
	ListNode *ptr = head;
	cout<< "The sorted list is: " <<endl;
	for (int x=0; x<10; x++)
	{
		cout<< ptr->value <<endl;
		ptr=ptr -> next;
	
	} 
	
	return 0; 
}
Last edited on
What do you mean by "sort pointers"?

Is it that you don't want to swap values between nodes, but wan't to rearrange the order of nodes in the list instead? If so, then you should have methods that insert and remove node for a list. Furthermore, you have only one list now; one head. You could/should have temporary list(s) too.
Topic archived. No new replies allowed.