Original Post
Hi, I've managed to get a good Working Link list for my program that Adds Players Names and Numbers of Kills or Frags. But my sorting seems to be a little backwards. It sorts by Kills correctly. but in reverse order.. meaning the Highest is at the bottom instead at the top where i would like to have it. I was wondering if anyone might be able to tell me how i can reverse this to get the Highest kills or frags at the top of the list instead of the bottom. I'm sorta pulling my hair out at this now.. any help would be apprecieated..
[edited by - tonic151 on April 11, 2002 2:16:43 PM]
/*--------------------------------------------------------------------------------*/
// Link List for Sorting Data
struct ListItem
{
char name[30];
long frags;
struct ListItem *next;
};
struct ListItem *MyList = 0 ; // This is the list, initially as empty
void AddPlayer2(PLAYERNAME * player) // Add a new word to the list MyList
{
struct ListItem *Item, *next;
Item = (struct ListItem *) malloc(sizeof(struct ListItem));
if ( Item==0 ) { printf ("Malloc Failed \n"); return ; }
strcpy(Item->name,player->name); // Copy Player's Name
Item->frags = player->frags; // Copy Player's Frags
Item->next = 0; // Set that next Item is nothing
if ( MyList == 0 ) // If List is Empty, make this the first item
{ MyList = Item; return ; }
// This is where Sorting takes place!
// Check if the new item comes before the first item in old list
if((player->frags - MyList->frags) < 0 ) {
Item->next = MyList;
MyList = Item;
return ;
}
// Check to see if an item is inserted before the next item
for ( next = MyList ; next->next != 0 ; next = next->next ) {
if ((player->frags - next->next->frags) < 0 ) {
// Insert Item before the next Item.
Item->next = next->next;
next->next = Item ;
return;
}
}
// There are no more Items ! Add to end
next->next = Item;
}