I have this linked list class that stores int variables. Each int variable is placed in it's own node. I want to put the nodes in order, greatest to least. I thought of an idea where you have a loop that goes through each node and another loop inside of that that goes through each node also. The first loop chooses what node is being "scored". The node's score determines where it is moved to in the linked list. The second loop "scores" the node the first loop chose by comparing it's int's value against the node that second loop selected's int. If the first node's int is greater than the other one, it gets a point. After the second loop is done executing, you calculate what element number the node that was being scored gets by taking the opposite of the score and adding the length of the list to that. I hope I explained it well. Anyway, I'm having a lot of trouble with that idea. I have no idea why I get unhandled exception errors when I try it, so I'm asking for your help! If you know of a better way to sort stuff from greatest to least, I beg you to tell me. Before I show you my code, I'm going to tell a little about some of the functions my class uses. The important function that my sorting function (sortgtl) uses is the moveto function. It's arguments are the element numbers of two nodes. The nodes corresponding to these numbers switch places in the linked list. I haven't used the elementdel function as I thought I was going to, so I guess I'll stop explaining and show you the code:
Code:
class intarr
{
protected:
class node
{
public:
int data;
node * next;
node * prev;
node & operator=(int);
friend bool operator>(node & a, node & b) { return a.data > b.data; }
friend bool operator<(node & a, node & b) { return a.data < b.data; }
friend bool operator>=(node & a, node & b) { return a.data >= b.data; }
friend bool operator<=(node & a, node & b) { return a.data <= b.data; }
};
node * head;
node * current;
public:
unsigned int len;
intarr() : head(0), current(head) {}
intarr(int *, unsigned int);
node & operator[](unsigned int);
void elementdel(unsigned int);
void moveto(unsigned int, unsigned int);
void sortgtl();
};
intarr::node & intarr::node::operator=(int n)
{
this->data = n;
return *this;
}
intarr::intarr(int * d, unsigned int l)
{
head = new node;
current = head;
for(unsigned int a = 0; a < l; a++)
{
current->data = d[a];
current->next = new node;
current->next->prev = current;
current = current->next;
}
len = l;
}
intarr::node & intarr::operator[](unsigned int i)
{
current = head;
for (unsigned int a = 0; a < i; a++)
current = current->next;
return *current;
}
void intarr::elementdel(unsigned int e)
{
node * deadman = &(operator[](e));
if(deadman != head)
{
deadman->prev->next = deadman->next;
deadman->next->prev = deadman->prev;
delete deadman;
}
}
void intarr::moveto(unsigned int m, unsigned int l)
{
node * mover = &(operator[](m));
node * location = &(operator[](l));
node * temp = new node;
*temp = *mover;
mover->next = location->next;
mover->prev = location->prev;
location->next = temp->next;
location->prev = temp->prev;
delete temp;
}
void intarr::sortgtl()
{
for (unsigned int a = 0; a < len; a++)
{
unsigned int points = 0;
for (unsigned int b = 0; b < len; b++)
{
if (operator[](a) > operator[](b))
points++;
}
moveto(a, -points + len);
}
}