-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskiplist.cpp
More file actions
232 lines (212 loc) · 5.01 KB
/
Copy pathskiplist.cpp
File metadata and controls
232 lines (212 loc) · 5.01 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
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
/**
* 跳表是一个随机化的数据,期待是log(n)的时间的复杂度
* 性能近似红黑树,平衡树等数据结构
* 编码简单,插入可以在跳表的不同部分并发进行
* 不用全局的数据结构重新平衡(例如红黑树)
*
* 跳表是实现在有序链表上的快速查找 删除等操作
* 简单实现,假设key是唯一的
* */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_LEVEL 32
typedef int KeyType;
typedef int ValueType;
// 节点的类型
struct Node
{
KeyType key;
ValueType value; // 表示数据资料
Node **next; // 指向后继节点的next数组
Node() { next = NULL; }
Node(KeyType k, ValueType v, int level) : key(k), value(v)
{
next = (Node **)malloc(sizeof(Node *) * level);
}
};
// 跳表
struct SkipList
{
int level; // 跳表的层数
Node *head;
};
/**
* 创建一个跳表的节点
* */
Node *createNode(int level, KeyType key, ValueType value)
{
Node *p = new Node(key, value, level);
if (!p)
return NULL;
return p;
}
// 创建整个链表的首部
SkipList *createCapital()
{
SkipList *capital = (SkipList *)malloc(sizeof(SkipList));
if (!capital)
return NULL;
Node *head = new Node(0, 0, MAX_LEVEL);
if (!head)
{
free(capital);
return NULL;
}
capital->level = 0;
capital->head = head;
for (int i = 0; i < MAX_LEVEL; i++)
{
head->next[i] = NULL;
}
return capital;
}
int randomInt()
{
int level = 1;
while (rand() % 2)
{
level++;
}
return level > MAX_LEVEL ? MAX_LEVEL : level;
}
Node *find(SkipList *capital, KeyType key)
{
Node *pCurrent = capital->head;
for (int i = capital->level - 1; i >= 0; --i)
{
while (pCurrent->next[i] && pCurrent->next[i]->key < key)
{
pCurrent = pCurrent->next[i];
}
}
if (pCurrent->next[0] && pCurrent->next[0]->key == key)
{
return pCurrent->next[0];
}
return NULL;
}
bool insert(SkipList *&capital, KeyType key, ValueType value)
{
Node *updates[MAX_LEVEL]; // updates[i]保存第i层需要更新的节点
// 首先找到应该放在的位置
Node *pCurrent = capital->head; // 从头节点开始(note: capital 是头结点的前一个节点)
for (int i = capital->level - 1; i >= 0; i--)
{ // 从最高层开始遍历
while (pCurrent->next[i] && pCurrent->next[i]->key < key)
{
pCurrent = pCurrent->next[i];
}
updates[i] = pCurrent; // 记录每一层需要更新的点,因为待插入的节点,从这里进入下一层
}
if (pCurrent->next[0] && pCurrent->next[0]->key == key)
{
pCurrent->next[0]->value = value;
return true;
}
// 随机一个层次
int level = randomInt();
if (level > capital->level)
{
for (int i = capital->level; i < level; ++i)
{
updates[i] = capital->head;
}
capital->level = level;
}
// 创建节点 并插入
Node *tmp = new Node(key, value, level);
if (!tmp)
return false;
for (int i = level - 1; i >= 0; --i)
{
tmp->next[i] = updates[i]->next[i];
updates[i]->next[i] = tmp;
}
return true;
}
bool erase(SkipList *capital, KeyType key)
{
Node *updates[MAX_LEVEL];
Node *pCurrent = capital->head;
for (int i = capital->level - 1; i >= 0; --i)
{
while (pCurrent->next[i] && pCurrent->next[i]->key < key)
{
pCurrent = pCurrent->next[i];
}
updates[i] = pCurrent;
}
if (!pCurrent->next[0] || pCurrent->next[0]->key != key)
{
return false; // not found.
}
for (int i = capital->level - 1; i >= 0; --i)
{
if (updates[i]->next[i] == pCurrent->next[i])
{
updates[i]->next[i] = pCurrent->next[i]->next[i];
if (capital->head->next[i] == NULL)
capital->level--;
}
}
return true;
}
void printSkipList(SkipList *capital)
{
// display skip list
printf("\n\ntotal level: %d\n", capital->level);
Node *pcur = capital->head;
for (int i = capital->level - 1; i >= 0; --i)
{
pcur = capital->head;
while (pcur)
{
printf("%d---->", pcur->key);
pcur = pcur->next[i];
}
printf("NULL\n");
}
}
int main()
{
srand(time(0));
// test insert function.
SkipList *capital = createCapital();
for (int i = 1; i <= 10; i++)
{
printf("insert [%d] status: %d\n", i, insert(capital, i, i + 100));
}
printSkipList(capital);
// test. list item.
// Node *tmp = capital->head;
// while(tmp) {
// printf("%d ", tmp->key);
// tmp = tmp->next[0];
// }
// test find function.
clock_t startTime, endTime;
SkipList *capital_2 = createCapital();
for (int i = 1; i <= 1000000; i++)
{
insert(capital_2, i, i + 100);
}
startTime = clock();
printf("%d\n", find(capital_2, 805529)->value);
endTime = clock();
printf("Total Time : %.2fs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
// test erase function.
SkipList *capital_3 = createCapital();
for (int i = 1; i <= 1000000; i++)
{
insert(capital_3, i, i + 100);
}
int key = 67;
Node* result = find(capital_3, key);
printf("\nfind %d, status : %s\n", key, result ? "found" : "not found");
bool status = erase(capital_3, key);
printf("\ndelete %d is %s\n", key, status?"success":"fail");
result = find(capital_3, key);
printf("\nfind %d, status : %s\n", key, result ? "found" : "not found");
return 0;
}