Created
February 2, 2011 17:18
-
-
Save zdxerr/808020 to your computer and use it in GitHub Desktop.
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
all: sorted_list.exe | |
sorted_list.exe: sorted_list.c sorted_list.h | |
gcc sorted_list.c -o sorted_list.exe | |
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
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include "sorted_list.h" | |
SortedList *sorted_list_new(void *base, size_t num, size_t size, | |
int (*compare)(const void *, const void *)) | |
{ | |
SortedList *sl; | |
Element *e; | |
unsigned int e_num; | |
sl = malloc(sizeof(SortedList)); | |
e = malloc(num * sizeof(Element)); | |
qsort(base, num, size, compare); | |
e[0].prev = NULL; | |
e[0].data = base; | |
for(e_num = 1;e_num < num;e_num++) | |
{ | |
e[e_num - 1].next = &e[e_num]; | |
e[e_num].prev = &e[e_num -1]; | |
e[e_num].data = base + e_num * size; | |
} | |
sl->head = &e[0]; | |
sl->tail = &e[num - 1]; | |
sl->length = num; | |
sl->size = size; | |
return sl; | |
} | |
void sorted_list_merge(SortedList *a, SortedList *b) | |
{ | |
SortedList *temp; | |
Element *pos_a, *pos_b; | |
if(a->length > b->length) | |
{ | |
temp = a; | |
a = b; | |
b = temp; | |
} | |
if(a->length == 0) | |
{ | |
a = b; | |
} | |
else | |
{ | |
pos_a = a->head; | |
pos_b = b->tail; | |
while(pos_a != NULL && pos_b != NULL) | |
{ | |
} | |
} | |
} | |
void *sorted_list_to_array(SortedList *sl) | |
{ | |
void *array; | |
Element *e; | |
unsigned int e_num; | |
array = malloc(sl->length * sl->size); | |
e = sl->head; | |
for(e_num = 0;e_num<sl->length;e_num++) | |
{ | |
memcpy(array + e_num * sl->size, e->data, sl->size); | |
e = e->next; | |
} | |
return array; | |
} | |
int cmp(double *a, double *b) | |
{ | |
if(*a > *b) return 1; | |
if(*a == *b) return 0; | |
return -1; | |
} | |
int main(int argc, char **argv) | |
{ | |
int arg, pos; | |
double **list; | |
size_t *length; | |
size_t n; | |
if(argc < 2) | |
{ | |
return 0; | |
} | |
list = NULL; | |
length = NULL; | |
n = 0; | |
for(arg = 1;arg < argc;arg++) | |
{ | |
printf("%d: %s\n", arg, argv[arg]); | |
if(argv[arg] > 0) | |
{ | |
n++; | |
length = realloc(length, n * sizeof(size_t)); | |
length[n - 1] = atoi(argv[arg]); | |
list = realloc(list, n * sizeof(double *)); | |
list[n - 1] = malloc(length[n - 1] * sizeof(double)); | |
for(pos = 0;pos < length[n - 1];pos ++) | |
{ | |
list[n - 1][pos] = atof(argv[arg + pos + 1]); | |
} | |
arg += length[n - 1]; | |
} | |
SortedList *sl = sorted_list_new(list[n-1], length[n-1], sizeof(double), (int (*)(const void *, const void *))cmp); | |
double *arr = (double *)sorted_list_to_array(sl); | |
int i; | |
for(i=0;i < sl->length;i++) printf("%.3f ", arr[i]); | |
printf("\n"); | |
} | |
return 0; | |
} |
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
typedef struct _Element Element; | |
typedef struct _SortedList SortedList; | |
struct _SortedList | |
{ | |
Element *head; | |
Element *tail; | |
unsigned int length; | |
size_t size; | |
int (*compare)(const void *, const void *); | |
}; | |
struct _Element | |
{ | |
Element *next; | |
Element *prev; | |
void *data; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment