• C

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-
LVL 1
pure032398Asked:
Who is Participating?
 
imladrisCommented:
Hmmm. On further reflection I would counsel removing the line altogether. It seemed to me when I was reading through LL_QSORT.C that setnext was intended to insert the element p2 in the linked list after the element p1. However, in retrospect, both the source code, and the fact that p2 can sometimes be NULL, suggest that setnext is merely adding p2 (and whatever else may be trailing after it) to p1 (which is assumed to be at the end of a list).
0
 
mournbladeCommented:
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.
0
 
pure032398Author Commented:
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-
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
mbogaertsCommented:
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.
0
 
pure032398Author Commented:
Adjusted points from 20 to 50
0
 
pure032398Author Commented:
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-
0
 
mournbladeCommented:
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.
0
 
imladrisCommented:
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;
}
0
 
pure032398Author Commented:
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-

0
 
pure032398Author Commented:
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-

0
 
pure032398Author Commented:
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-
0
 
imladrisCommented:
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));
}

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.