Link to home
Start Free TrialLog in
Avatar of pure032398
pure032398

asked on

How to use this SORTING function

heya all.

After reading some <PAQ>'s, i found this function from a site. I do not know how to use it.

Can this function quickly be explainined -> not WHAT it does, but the params it requires please.

The function is a linked list sorting algorithm, taken from www.snippets.org
[snippet == LL_QSORT.C]

this is the function header.

Heya all.


void *sortl(void *list, void *(*getnext)(void *),
            void (*setnext)(void *, void *),
            int (*compare)(void *, void *));


-NOW-
if my l-list is
struct student_data *student_list;

how do i sort the contents of student list?

Thank you kindly.

-PK-
Avatar of mournblade
mournblade

you need to pass the student_list as the first parameter and you need to provide three functions ptrs for the other parameters. the first seems to be a function that will get the next item in the list; the second one would be a function that would do permutations on two items based on the result of the third function; this third function would be one that compare two items and decide if they must be swapped or not. you have to write these fcns and pass their addresses to this sortl() fcn.
Avatar of pure032398

ASKER

If I have to write those 3 functions, then why bother having a snippet available for quick use when u still have to write your own code?

surly this seems - silly?!

Or have they already done that code somewhere else?

all i was after was a simple linked list sorting function.... =(

-PK-
Mournblade explains it right. The reason you need to write these functions' code is that the author of the sorting function has no way to know what your linked kist contains or how you built it. Only you know how to compare two elements of your list, how to get to the next element in the list and how to swap elements in that list. No one else can know this because it depends on your code.
These functions should be fairly simple to write however. Since you use them yourself probably  already in your code. All you have to do is to make them behave like the prototype requires. The getnext function will probably be something like:
void *getnext(void *elem)
{
  return (void *)(((struct student_data *)elem)->next);
}
assuming your structure contains some pointer to the next element in the list.
Adjusted points from 20 to 50
GetNext is simple (now that i've seen the answer)

This is my structure. Anyone wanna answer the last 2

extern struct player_logged_in_data *player_list;


and in another file, the struct is declared..


struct player_logged_in_data {
  long idnum;  /* unique*/
  char player_name[MAX_NAME_LENGTH];
  struct player_logged_in_data *next;
};

-PK-
compare() should be something like this:

int compare(void *elem, void *next)
{
  struct player_logged_in_data * first;
  struct player_logged_in_data * second;

  first = (struct player_logged_in_data *)elem;
  second = (struct player_logged_in_data *) next;

  if (first->idnum > second->idnum)
    return 0;
  else
    return 1;
}

but i can't be sure without knowing how sortl() works. you'll have to look at the code inside or its api. same for setNext(). but they are REALLY simple functions to code as you can see.
compare would be something like:

int playercompare(void *e1,void *e2)
{   long id1,id2;

    id1=((struct player_logged_in_data *)e1)->idnum;
    id2=((struct player_logged_in_data *)e2)->idnum;
    if(id1<id2)return(-1);
    if(id1==id2)return(0);
    return(1);
}

and setnext would be something like:

void setnextplayer(void *p1,void *p2)
{   struct player_logged_in_data *pdp;

    pdp=((struct player_logged_in_data *)p1)->next;
    ((struct player_logged_in_data *)p1)->next=p2;
    ((struct player_logged_in_data *)p2)->next=pdp;
    return;
}
setnext crashes.

(offending line)
((struct player_logged_in_data *)p2)->next=pdp;

It works fine when theirs one element in the list, but 2, it then crashes there.

it says p2 is 0x00000000 just before it goes to that line.

I assume it's trying to make p2's NEXT pointer point to pdp, but there IS not content / structure.

therefore i added a

if (p2)
   ((struct player_logged_in_data *)p2)->next=pdp;

and it works,

BUT

is this correct though?

-PK-

setnext crashes.

(offending line)
((struct player_logged_in_data *)p2)->next=pdp;

It works fine when theirs one element in the list, but 2, it then crashes there.

it says p2 is 0x00000000 just before it goes to that line.

I assume it's trying to make p2's NEXT pointer point to pdp, but there IS not content / structure.

therefore i added a

if (p2)
   ((struct player_logged_in_data *)p2)->next=pdp;

and it works,

BUT

is this correct though?

-PK-

ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sweet.

that worked.

any chance you could tell me how to player_compare on the player_name, not on the ID?

so names are then alphabetically listed, not ID listed.

danke =)

-PK-
That's even easier; you can use strcmp, it would be something like:

int playercompare(void *e1,void *e2)
{   char *nm1,*nm2;

    nm1=((struct player_logged_in_data *)e1)->player_name;
    nm2=((struct player_logged_in_data *)e2)->player_name;
    return(strcmp(nm1,nm2));
}