forked from wenjun1055/c
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34c2878
commit 03a61fb
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
struct Heap_ | ||
{ | ||
int size; | ||
int (*compare)(const void *key1, const void *key2); | ||
void (*destory)(void *data); | ||
void **tree; | ||
}; | ||
typedef struct Heap_ Heap; | ||
|
||
void heap_init(Heap *heap, int (*compare)(const void *key1, const void *key2), void (*destory)(void *data)); | ||
void heap_destory(Heap *heap); | ||
int heap_insert(Heap *heap, const void *data); | ||
int heap_extract(Heap *heap, void **data); | ||
|
||
#define HEAP_SIZE(heap) ((heap)->size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
int issort(void *data, int size, int esize, int (*compare)(const void *key1, const void *key2)) | ||
{ | ||
char *a = data; | ||
void *key; | ||
int i,j; | ||
|
||
if (NULL == (key = (char *)malloc(esize))) { | ||
return -1; | ||
} | ||
|
||
for (j = 1; j < size; j++) { | ||
memcpy(key, &a[j * esize], esize); | ||
i = j - 1; | ||
|
||
while (i >=0 && compare(&a[i * esize], key) > 0) { | ||
memcpy(&a[(i + 1) * esize], &a[i * esize], esize); | ||
i--; | ||
} | ||
|
||
memcpy(&a[(i + 1) * esize], key, esize); | ||
} | ||
|
||
free(key); | ||
|
||
return 0; | ||
} | ||
|
||
int main() | ||
{ | ||
char *str = "zyxkjhaop"; | ||
|
||
printf("original:str = %s", str); | ||
|
||
issort(str, 9, sizeof(char), strcmp); | ||
|
||
printf("changed:str = %s", str); | ||
|
||
return 0; | ||
} |