I
incredible
how to sort link list of string
how to sort link list of string
incredible said:how to sort link list of string
how to sort link list of string
Eric said:incredible wrote On 11/20/06 12:05,:
how to sort link list of string
/* Usage instructions: Declare a `struct node' type containing a
* `struct node*' field named `next' as its forward link, along with
* whatever other elements are desired. Define a precedes() function
* that takes two `const struct node*' pointers and returns nonzero
* if the first struct must precede the second in the sorted output,
* or zero if the first follows the second or if they compare equal.
* Build a linked list of `struct node' instances, with the `next'
* field of the last node equal to NULL. Call the listsort() function,
* passing a pointer to the first node as the argument. listsort()
* will rearrange the list's links to sort the list in accordance with
* the precedes() function, and will return a pointer to the first
* `struct node' of the sorted list.
*
* Note: This code uses an OO style.
*/
#include <limits.h>
static struct node*O(struct node*o,struct node*O){struct node*Oo,**oO;
oO=&Oo;for(;{if(precedes(O,o)){*oO=O;oO=&O->next;if(0==(O=O->next)){
*oO=o;break;}}else{*oO=o;oO=&o->next;if(!(o=o->next)){*oO=O;break;}}}\
return Oo;}struct node*listsort(struct node*oO){struct node*o,*o0[siz\
eof(void*)*CHAR_BIT-sizeof(char)],*Oo;int oo,O0;o0[O0=0]=0;while((o=o\
O)){if((Oo=o->next)==0)break;oO=Oo->next;if(precedes(Oo,o)){Oo->next=o
;o->next=0;o=Oo;}else{Oo->next=0;}for(oo=0;(Oo=o0[oo]){o0[oo]=0;o=O(
Oo,o);if(++oo>O0){O0=oo;break;}}o0[oo]=o;}for(oo=0;oo<=O0;++oo){if((o=
o0[oo]))oO=oO?O(o,oO);}return oO;}
Eric said:incredible wrote On 11/20/06 12:05,:
how to sort link list of string
/* Usage instructions: Declare a `struct node' type containing a
* `struct node*' field named `next' as its forward link, along with
* whatever other elements are desired. Define a precedes() function
* that takes two `const struct node*' pointers and returns nonzero
* if the first struct must precede the second in the sorted output,
* or zero if the first follows the second or if they compare equal.
* Build a linked list of `struct node' instances, with the `next'
* field of the last node equal to NULL. Call the listsort() function,
* passing a pointer to the first node as the argument. listsort()
* will rearrange the list's links to sort the list in accordance with
* the precedes() function, and will return a pointer to the first
* `struct node' of the sorted list.
*
* Note: This code uses an OO style.
*/
#include <limits.h>
static struct node*O(struct node*o,struct node*O){struct node*Oo,**oO;
oO=&Oo;for(;{if(precedes(O,o)){*oO=O;oO=&O->next;if(0==(O=O->next)){
*oO=o;break;}}else{*oO=o;oO=&o->next;if(!(o=o->next)){*oO=O;break;}}}\
return Oo;}struct node*listsort(struct node*oO){struct node*o,*o0[siz\
eof(void*)*CHAR_BIT-sizeof(char)],*Oo;int oo,O0;o0[O0=0]=0;while((o=o\
O)){if((Oo=o->next)==0)break;oO=Oo->next;if(precedes(Oo,o)){Oo->next=o
;o->next=0;o=Oo;}else{Oo->next=0;}for(oo=0;(Oo=o0[oo]){o0[oo]=0;o=O(
Oo,o);if(++oo>O0){O0=oo;break;}}o0[oo]=o;}for(oo=0;oo<=O0;++oo){if((o=
o0[oo]))oO=oO?O(o,oO);}return oO;}
[1] c:\c\junk>cc junk.c
junk.c: In function `O':
junk.c:3: warning: implicit declaration of function `precedes'
junk.c:3: dereferencing pointer to incomplete type
junk.c:3: dereferencing pointer to incomplete type
junk.c:4: dereferencing pointer to incomplete type
junk.c:4: dereferencing pointer to incomplete type
junk.c: In function `listsort':
junk.c:4: dereferencing pointer to incomplete type
junk.c:4: dereferencing pointer to incomplete type
junk.c:4: dereferencing pointer to incomplete type
junk.c:8: dereferencing pointer to incomplete type
junk.c:8: dereferencing pointer to incomplete type
incredible said:how to sort link list of string
incredible said:how to sort link list of string
pete said:list_type *list_sort(list_type *head,
int (*compar)(const list_type *, const list_type *));
head = list_sort(head, lencomp);
int lencomp(const void *a, const void *b)
{
const size_t a_len = strlen(((const list_type *)a) -> data);
const size_t b_len = strlen(((const list_type *)b) -> data);
return b_len > a_len ? -1 : a_len != b_len;
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.