C语言跳表(skiplist)实现

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>
 
struct sk_link {
   struct sk_link *next, *prev;
};
 
static inline void
skip_list_init( struct sk_link *link)
{
   link->prev = link;
   link->next = link;
}
 
static inline void
__skip_list_add( struct sk_link *link, struct sk_link *prev, struct sk_link *next)
{
   link->next = next;
   link->prev = prev;
   next->prev = link;
   prev->next = link;
}
 
static inline void
__skip_list_del( struct sk_link *prev, struct sk_link *next)
{
   prev->next = next;
   next->prev = prev;
}
 
static inline void
skip_list_add( struct sk_link *link, struct sk_link *prev)
{
   __skip_list_add(link, prev, prev->next);
}
 
static inline void
skip_list_del( struct sk_link *link)
{
   __skip_list_del(link->prev, link->next);
   skip_list_init(link);
}
 
static inline int
skip_list_empty( struct sk_link *link)
{
   return link->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 */
 
struct man {
   int level;
   int dog_num;
   struct sk_link collar[MAX_LEVEL];
};
 
struct dog {
   int price;
   struct sk_link link[];
};
 
// Wow!
struct dog *
dog_birth( int level, int price)
{
   int i;
   struct dog *dog;
 
   dog = ( struct dog *) malloc ( sizeof (*dog) + level * sizeof ( struct sk_link));
   if (dog == NULL)
     return NULL;
 
   dog->price = price;
   for (i = 0; i < level; i++)
     skip_list_init(&dog->link[i]);
 
   return dog;
}
 
// Wow! Wow! Wow! Wowwwwwwwwwwwwwww...
void
dog_kill( struct dog *dog)
{
   free (dog);
}
 
// Come on, baby!
struct man *
man_birth()
{
   int i;
   struct man *man;
 
   man = ( struct man *) malloc ( sizeof (*man));
   if (man == NULL)
     return NULL;
 
   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]);
 
   return man;
}
 
// Please don't, Bro! please!
void
man_kill( struct man *man)
{
   struct sk_link *pos, *n;
   struct dog *dog;
 
   pos = man->collar[0].next;
   skip_list_foreach_safe(pos, n, &man->collar[0]) {
     dog = skip_list_entry(pos, struct dog, link[0]);
     dog_kill(dog);
   }
   
   free (man);
}
 
static int
random_level( void )
{
   const double SKIPLIST_P = 0.25;
   int level = 1;
   while ((random() & 0xffff) < 0xffff * SKIPLIST_P)
     level++;
   return level > MAX_LEVEL ? MAX_LEVEL : level;
}
 
/* $_$ */
struct dog *
find( struct man *man, int price)
{
   int i;
   struct sk_link *pos, *end;
 
   i = man->level - 1;
   pos = &man->collar[i];
   end = &man->collar[i];
 
   for (; i >= 0; i--) {
     struct dog *dog;
     pos = pos->next;
     skip_list_foreach(pos, end) {
       dog = skip_list_entry(pos, struct dog, link[i]);
       if (dog->price == price) {
         return dog;
       } else if (dog->price > price) {
         end = &dog->link[i];
         break ;
       }
     }
     pos = end->prev;
     pos--;
     end--;
   }
 
   return NULL;
}
 
/* O^_^O */
void
adopt( struct man *man, int price)
{
   int i, level;
   struct sk_link *pos, *end;
   struct dog *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--) {
     struct dog *d;
     pos = pos->next;
     skip_list_foreach(pos, end) {
       d = skip_list_entry(pos, struct dog, 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( struct man *man, struct dog *dog, int level)
{
   int i;
   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( struct man *man, int price)
{
   int i;
   struct sk_link *pos, *n, *end;
 
   i = man->level - 1;
   pos = &man->collar[i];
   end = &man->collar[i];
 
   for (; i >= 0; i--) {
     struct dog *dog;
     pos = pos->next;
     skip_list_foreach_safe(pos, n, end) {
       dog = skip_list_entry(pos, struct dog, 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( struct man *man)
{
   struct sk_link *pos, *n;
   struct dog *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, struct dog, link[0]);
     printf ( "price:0x%08x\n" , dog->price);
   }
}
 
int main( void )
{
#define NUM 1024 * 1024
 
   struct man *man;
   struct dog *dog;
   int i;
   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);
 
   return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值