Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How to use this SORTING function

Posted on 2000-05-16
Medium Priority
191 Views
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]

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-
0
Question by:pure032398
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 3
• 2
• +1

LVL 1

Expert Comment

ID: 2815801
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

LVL 1

Author Comment

ID: 2815908
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

Expert Comment

ID: 2815998
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

LVL 1

Author Comment

ID: 2816179
Adjusted points from 20 to 50
0

LVL 1

Author Comment

ID: 2816180
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

LVL 1

Expert Comment

ID: 2817508
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

LVL 16

Expert Comment

ID: 2817552
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

LVL 1

Author Comment

ID: 2819480
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.

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

and it works,

BUT

is this correct though?

-PK-

0

LVL 1

Author Comment

ID: 2819506
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.

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

and it works,

BUT

is this correct though?

-PK-

0

LVL 16

Accepted Solution

ID: 2819537
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

LVL 1

Author Comment

ID: 2819944
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

LVL 16

Expert Comment

ID: 2821618
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soā¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infā¦
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month9 days, 5 hours left to enroll