Solved

How to use this SORTING function

Posted on 2000-05-16
12
185 Views
Last Modified: 2010-04-02
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-
0
Comment
Question by:pure032398
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 1

Expert Comment

by:mournblade
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

by:pure032398
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

by:mbogaerts
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:pure032398
ID: 2816179
Adjusted points from 20 to 50
0
 
LVL 1

Author Comment

by:pure032398
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

by:mournblade
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

by:imladris
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

by:pure032398
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.

therefore i added a

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

and it works,

BUT

is this correct though?

-PK-

0
 
LVL 1

Author Comment

by:pure032398
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.

therefore i added a

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

and it works,

BUT

is this correct though?

-PK-

0
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
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

by:pure032398
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

by:imladris
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adjust Mfcapp 29 177
Inorder binary search tree 5 178
c++ getting the first 10 characters of a char* string 11 104
How to install SVN Command Line Client? 5 99
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question