-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_skiplist.c
More file actions
76 lines (62 loc) · 1.69 KB
/
test_skiplist.c
File metadata and controls
76 lines (62 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Description:
* History: yang@haipo.me, 2017/03/21, create
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "ut_sds.h"
# include "ut_skiplist.h"
void *node_dup(void *obj)
{
return sdsnewlen(obj, sdslen(obj));
}
void node_free(void *obj)
{
sdsfree(obj);
}
int node_compare(const void *obj, const void *key)
{
return strcmp(obj, key);
}
int main(int argc, char *argv[])
{
skiplist_type type;
type.dup = node_dup;
type.free = node_free;
type.compare = node_compare;
skiplist_t *list = skiplist_create(&type);
for (int i = 0; i < 1000; ++i) {
int num = random() % 26;
sds value = sdsempty();
value = sdscatprintf(value, "%c", 'a' + num);
if (skiplist_insert(list, value)) {
printf("insert: %s\n", value);
}
sdsfree(value);
}
printf("list len: %ld\n", skiplist_len(list));
printf("level: %d\n", list->level);
for (int i = list->level - 1; i >= 0; --i) {
printf("level %d: ", i);
skiplist_node *node = list->header->forward[i];
while (node) {
printf("%s -> ", (char *)node->value);
node = node->forward[i];
}
printf("\n");
}
sds value = sdsnew("k");
skiplist_delete(list, skiplist_find(list, value));
sdsfree(value);
printf("list len: %ld\n", skiplist_len(list));
printf("level: %d\n", list->level);
skiplist_iter *iter = skiplist_get_iterator(list);
skiplist_node *node;
while ((node = skiplist_next(iter)) != NULL) {
printf("%s\n", (char *)node->value);
skiplist_delete(list, node);
}
printf("list len: %ld\n", skiplist_len(list));
return 0;
}