C语言跳表(skiplist)实现

来源:互联网 发布:微星在淘宝 编辑:程序博客网 时间:2024/05/17 09:10

C语言跳表(skiplist)实现

http://www.oschina.net/code/snippet_1444806_33693
分享到: 
收藏+3
踩顶0
跳表(skiplist)是一个非常优秀的数据结构,实现简单,插入、删除、查找的复杂度均为O(logN)。LevelDB的核心数据结构是用跳表实现的,redis的sorted set数据结构也是有跳表实现的。代码在这里:http://flyingsnail.blog.51cto.com/5341669/1020034

由于涉及直接指针操作,代码产生很多分支,学习者很难记忆。我的做法是使用内核的list_head组件,搭建起跳表基本操作。list_head组件成功之处在于,将链表成员分离,一是使用dummy head,二是对指针的构造和析构,从而避免繁琐的指针操作和分支判断,可降低程序员心智负担,提高开发效率,堪称C语言里的链表STL。
标签: <无>

源码与演示:源码出处

代码片段(1)[全屏查看所有代码]

1. [文件] skip_list.c ~ 6KB     下载(30)     跳至 [1] [全屏预览]

?
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**********************
* Licence: MIT
* Author: Leo Ma
* Date: 2014-03-01
*********************/
 
#include <stdio.h>
#include <stdlib.h>
 
structsk_link {
  structsk_link *next, *prev;
};
 
staticinline void
skip_list_init(structsk_link *link)
{
  link->prev = link;
  link->next = link;
}
 
staticinline void
__skip_list_add(structsk_link *link, structsk_link *prev, structsk_link *next)
{
  link->next = next;
  link->prev = prev;
  next->prev = link;
  prev->next = link;
}
 
staticinline void
__skip_list_del(structsk_link *prev, structsk_link *next)
{
  prev->next = next;
  next->prev = prev;
}
 
staticinline void
skip_list_add(structsk_link *link, structsk_link *prev)
{
  __skip_list_add(link, prev, prev->next);
}
 
staticinline void
skip_list_del(structsk_link *link)
{
  __skip_list_del(link->prev, link->next);
  skip_list_init(link);
}
 
staticinline int
skip_list_empty(structsk_link *link)
{
  returnlink->next == link;
}
 
#define skip_list_entry(ptr, type, member) \
  ((type *)((char*)(ptr) - (unsigned long)(&((type *)0)->member)))
 
#define skip_list_foreach(pos, end) \
  for(; pos != end; pos = pos->next)
 
#define skip_list_foreach_safe(pos, n, end) \
  for(n = pos->next; pos != end; pos = n, n = pos->next)
 
#define MAX_LEVEL 32  /* Should be enough for 2^32 elements */
 
structman {
  intlevel;
  intdog_num;
  structsk_link collar[MAX_LEVEL];
};
 
structdog {
  intprice;
  structsk_link link[];
};
 
// Wow!
structdog *
dog_birth(intlevel, intprice)
{
  inti;
  structdog *dog;
 
  dog = (structdog *)malloc(sizeof(*dog) + level * sizeof(structsk_link));
  if(dog == NULL)
    returnNULL;
 
  dog->price = price;
  for(i = 0; i < level; i++)
    skip_list_init(&dog->link[i]);
 
  returndog;
}
 
// Wow! Wow! Wow! Wowwwwwwwwwwwwwww...
void
dog_kill(structdog *dog)
{
  free(dog);
}
 
// Come on, baby!
structman *
man_birth()
{
  inti;
  structman *man;
 
  man = (structman *)malloc(sizeof(*man));
  if(man == NULL)
    returnNULL;
 
  man->level = 1;
  man->dog_num = 0;
  for(i = 0; i < sizeof(man->collar) / sizeof(man->collar[0]); i++)
    skip_list_init(&man->collar[i]);
 
  returnman;
}
 
// Please don't, Bro! please!
void
man_kill(structman *man)
{
  structsk_link *pos, *n;
  structdog *dog;
 
  pos = man->collar[0].next;
  skip_list_foreach_safe(pos, n, &man->collar[0]) {
    dog = skip_list_entry(pos, structdog, link[0]);
    dog_kill(dog);
  }
   
  free(man);
}
 
staticint
random_level(void)
{
  constdouble SKIPLIST_P = 0.25;
  intlevel = 1;
  while((random() & 0xffff) < 0xffff * SKIPLIST_P)
    level++;
  returnlevel > MAX_LEVEL ? MAX_LEVEL : level;
}
 
/* $_$ */
structdog *
find(structman *man, intprice)
{
  inti;
  structsk_link *pos, *end;
 
  i = man->level - 1;
  pos = &man->collar[i];
  end = &man->collar[i];
 
  for(; i >= 0; i--) {
    structdog *dog;
    pos = pos->next;
    skip_list_foreach(pos, end) {
      dog = skip_list_entry(pos, structdog, link[i]);
      if(dog->price == price) {
        returndog;
      }elseif (dog->price > price) {
        end = &dog->link[i];
        break;
      }
    }
    pos = end->prev;
    pos--;
    end--;
  }
 
  returnNULL;
}
 
/* O^_^O */
void
adopt(structman *man, intprice)
{
  inti, level;
  structsk_link *pos, *end;
  structdog *dog;
 
  level = random_level();
  if(level > man->level) {
    man->level = level;
  }
 
  dog = dog_birth(level, price);
  if(dog == NULL) {
    return;
  }
 
  i = man->level - 1;
  pos = &man->collar[i];
  end = &man->collar[i];
 
  for(; i >= 0; i--) {
    structdog *d;
    pos = pos->next;
    skip_list_foreach(pos, end) {
      d = skip_list_entry(pos, structdog, link[i]);
      if(d->price >= price) {
        end = &d->link[i];
        break;
      }
    }
    pos = end->prev;
    if(i < level) {
      __skip_list_add(&dog->link[i], pos, end);
    }
    pos--;
    end--;
  }
 
  man->dog_num++;
}
 
/* T_T */
void
__abandon(structman *man, structdog *dog, intlevel)
{
  inti;
  for(i = 0; i < level; i++) {
    skip_list_del(&dog->link[i]);
    if(skip_list_empty(&man->collar[i])) {
      man->level--;
    }
  }
 
  dog_kill(dog);
  man->dog_num--;
}
 
/* T_T */
void
abandon(structman *man, intprice)
{
  inti;
  structsk_link *pos, *n, *end;
 
  i = man->level - 1;
  pos = &man->collar[i];
  end = &man->collar[i];
 
  for(; i >= 0; i--) {
    structdog *dog;
    pos = pos->next;
    skip_list_foreach_safe(pos, n, end) {
      dog = skip_list_entry(pos, structdog, link[i]);
      if(dog->price == price) {
        // Here's no break statement because we allow dogs with same price.
        __abandon(man, dog, i + 1);
      }
    }
    pos = end->prev;
    pos--;
    end--;
  }
}
 
void
print(structman *man)
{
  structsk_link *pos, *n;
  structdog *dog;
 
  printf("\nTotal %d dogs: \n", man->dog_num);
  pos = man->collar[0].next;
  skip_list_foreach_safe(pos, n, &man->collar[0]) {
    dog = skip_list_entry(pos, structdog, link[0]);
    printf("price:0x%08x\n", dog->price);
  }
}
 
intmain(void)
{
#define NUM 1024 * 1024
 
  structman *man;
  structdog *dog;
  inti;
  int*price;
 
    price = (int*)malloc(NUM * sizeof(int));
    if(price == NULL)
        exit(-1);
 
  // Hello man!
  man = man_birth();
  if(man == NULL)
    exit(-1);
 
  printf("Test start!\n");
  printf("Start to adopt %d dogs...\n", NUM);
 
  // Insert test.
  for(i = 0; i < NUM; i++) {
    price[i] = (int)rand();
    adopt(man, price[i]);
  }
 
  //print(man);
  printf("Adoption finished. Now play with each dog...\n");
 
  // Search test.
  for(i = 0; i < NUM; i++) {
    dog = find(man, price[i]);
    if(dog != NULL) {
      //printf("dog price:0x%08x\n", dog->price);
    }else{
      printf("Not found:0x%08x\n", price[i]);
    }
  }
 
  printf("Play finished. Now abandon some dogs...\n");
 
  // Delete test.
  for(i = 0; i < NUM; i += 3) {
    abandon(man, price[i]);
  }
 
  //print(man);
  printf("Abandon Finished.\nEnd of Test\n");
 
  // Goodbye man!
  man_kill(man);
 
  return0; 
}
0 0
原创粉丝点击