Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 53 lines (44 sloc) 0.924 kb
0db71e0 add mergesort() for linked lists
René Scharfe authored
1 #include "cache.h"
2 #include "mergesort.h"
3
4 struct line {
5 char *text;
6 struct line *next;
7 };
8
9 static void *get_next(const void *a)
10 {
11 return ((const struct line *)a)->next;
12 }
13
14 static void set_next(void *a, void *b)
15 {
16 ((struct line *)a)->next = b;
17 }
18
19 static int compare_strings(const void *a, const void *b)
20 {
21 const struct line *x = a, *y = b;
22 return strcmp(x->text, y->text);
23 }
24
84d32bf sparse: Fix mingw_main() argument number/type errors
Ramsay Jones authored
25 int main(int argc, char **argv)
0db71e0 add mergesort() for linked lists
René Scharfe authored
26 {
27 struct line *line, *p = NULL, *lines = NULL;
28 struct strbuf sb = STRBUF_INIT;
29
30 for (;;) {
31 if (strbuf_getwholeline(&sb, stdin, '\n'))
32 break;
33 line = xmalloc(sizeof(struct line));
34 line->text = strbuf_detach(&sb, NULL);
35 if (p) {
36 line->next = p->next;
37 p->next = line;
38 } else {
39 line->next = NULL;
40 lines = line;
41 }
42 p = line;
43 }
44
7365c95 @gitster mergesort: rename it to llist_mergesort()
gitster authored
45 lines = llist_mergesort(lines, get_next, set_next, compare_strings);
0db71e0 add mergesort() for linked lists
René Scharfe authored
46
47 while (lines) {
48 printf("%s", lines->text);
49 lines = lines->next;
50 }
51 return 0;
52 }
Something went wrong with that request. Please try again.