#include <stdio.h> #include <stdlib.h> struct mytype { struct mytype *next; int key; }; struct mytype *mysort; int cmpfunc (const void * a, const void * b) { struct mytype* aa = (struct mytype*)a; struct mytype* bb = (struct mytype*)b; return (aa->key - bb->key); } int main() { mysort = (struct mytype*)malloc(sizeof(struct mytype*)*10); int i; for( i = 0; i < 10; i++) { mysort[i].key = 10-i; } printf("before sorting\n"); for( i = 0; i < 10; i++) printf("%d ", mysort[i].key); printf("\n"); qsort(mysort, 10, sizeof(struct mytype), cmpfunc); printf("after sorting\n"); for( i = 0; i < 10; i++) printf("%d ", mysort[i].key); printf("\n"); return 0; }
2014年5月9日 星期五
C qsort() using struct
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言