Singly Linked List Sorting

this is where i sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sortNodes(StudentRecordDataNode * ptr) {
	StudentRecordData * temp = new StudentRecordData();
	StudentRecordDataNode * curr;
	for(bool didSwap = true; didSwap; ) {
		didSwap = false;
		for(curr = ptr; curr->next != NULL; curr = curr->next) {
			if(curr->getValue()->getAge() > curr->next->getValue()->getAge()) {
				temp = curr->getValue();
				curr->getValue() = curr->next->getValue();
				curr->next->getValue() = temp;
				didSwap = true;
			} 
		}
	}
}



this is the node class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef STUDENTRECORDDATANODE_H
#define STUDENTRECORDDATANODE_H
#include "StudentRecordData.h"

class StudentRecordDataNode
{
	StudentRecordData *Value;
	friend class StudentSingleLinkedList;
public:
	StudentRecordDataNode *next;
	StudentRecordDataNode();
	StudentRecordDataNode(StudentRecordData *val)
	{
		Value = val;
		next = 0;
	}
	StudentRecordData *getValue()
	{
		return Value;
	}
};
#endif 


and the student record data class:

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
#ifndef STUDENTRECORDDATE_H
#define STUDENTRECORDDATE_H

#include "Course.h"

#define MAX_COURSES      18
#define LEN_FN           10
#define LEN_LN           10

class StudentRecordData
{
   double Marks[MAX_COURSES];
   unsigned int Age;
   unsigned int ID;
   unsigned int CourseCount;
   char FirstName[LEN_FN];
   char LastName[LEN_FN];
   Course  Courses[MAX_COURSES];
public:
	StudentRecordData();
	StudentRecordData(double m[18], unsigned int age, unsigned int id, unsigned int cc, char fn[10], char ln[10], Course c[18]);
	unsigned int getID() {return ID;}
	void display();
	unsigned int getAge() {return Age;}
	StudentRecordData & operator =(StudentRecordData &o)
	   {
		   Age = o.Age;
		   CourseCount = o.CourseCount;
		   ID = o.ID;
		   for(int i=0;i<18;i++)
			   Marks[i] = o.Marks[i];
		   strcpy(LastName,o.LastName);
		   strcpy(FirstName,o.FirstName);
		   return *this;
	   }
};

#endif 



Line 9 and 10 of the sort gives me the error error C2106: '=' : left operand must be l-value
Last edited on
Your function returns a temporary variable to which you cannot assign anything. I'm pretty sure you intended
1
2
*curr->getValue() = *curr->next->getValue();
*curr->next->getValue() = *temp;

This assigns not to the returned pointer, but to the thing that it points to.
i fixed it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sortNodes(StudentRecordDataNode * ptr) {
	StudentRecordData * temp=0;
	StudentRecordDataNode * curr;
	for(bool didSwap = true; didSwap; ) {
		didSwap = false;
		for(curr = ptr; curr->next != NULL; curr = curr->next) {
			if(curr->getValue()->getAge() > curr->next->getValue()->getAge()) {
				temp = curr->getValue();
				*curr->getValue() = *curr->next->getValue();
				*curr->next->getValue() = *temp;
				didSwap = true;
			} 
		}
	}
}



but its duplicating a couple of node. The youngest (who is 19) is being displayed 8 times. So the sort inserted that node 8 times. What could cause this?
My bad.. What you need is not *'s but a set method (or public access to value).
my getAge() returns the Age though
There is nothing wrong with your getters. The problem is that since line 8 assigns pointers and line 9 assigns data, the old value of curr->getValue() is lost. You could make all assignments copy data (then temp would have to be an object and not a pointer), but that's a lot of unwanted copying. If you write a setter (or make value public) you can do this with just pointer assignment.
Topic archived. No new replies allowed.