Skip to content

Commit fc92f14

Browse files
committed
更新了跳表
1 parent c2883e3 commit fc92f14

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

cpp/数据结构/ShipList.cpp

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <cstdlib>
2+
#include <cmath>
3+
#include <vector>
4+
#include "ShipList.h"
5+
6+
using std::rand;
7+
using std::ceil;
8+
using std::log;
9+
using std::vector;
10+
11+
int ShipList::randLevel()
12+
{
13+
int level = 1;
14+
for (;rand() < p && maxLevel; level++);
15+
return level;
16+
}
17+
18+
ShipList::ShipList(double _p, int _maxN)
19+
{
20+
p = _p;
21+
maxN = _maxN;
22+
maxLevel = static_cast<int>(ceil(log(_maxN) / log(1/p)));
23+
forward = vector<Node*>(maxLevel+1, nullptr); // 使用拷贝构造进行初始化
24+
25+
}
26+
27+
int
28+
ShipList::Search(int _key)
29+
{
30+
31+
// 出循环的时候已经到达最后一层
32+
vector<Node*>& p = forward;
33+
for (int i = maxLevel; i > 0; --i) {
34+
if (!p[i]) continue;
35+
if (p[i]->key < _key)
36+
p = p[i]->forward;
37+
else if (p[i]->key == _key)
38+
return p[i]->key;
39+
}
40+
while (1) {
41+
if (p[1]->key == _key) {
42+
return p[1]->key;
43+
} else if (p[1]->key < _key) {
44+
return -1;
45+
}
46+
p = p[1]->forward;
47+
}
48+
return -1;
49+
}
50+
51+
void
52+
ShipList::Insert(int _key)
53+
{
54+
vector<Node*> update(maxLevel, nullptr); // 用于更新
55+
vector<Node*>& p = forward;
56+
for (int i = maxLevel; i > 0; --i) {
57+
if (!p[i]) continue;
58+
if (p[i]->key < _key) {
59+
update[i] = p[i];
60+
p = p[i]->forward;
61+
}
62+
else if (p[i]->key == _key)
63+
return ; // 因为出现相等了
64+
}
65+
66+
// 循环找插入节点
67+
while (1) {
68+
if (p[1]->key < _key) {
69+
update[1] = p[1];
70+
p = p[1]->forward;
71+
} else if (p[1]->key == _key) {
72+
return; // 直接返回
73+
} else break;
74+
}
75+
76+
/**
77+
* 创建节点:
78+
* 1. 随机选randlevel
79+
* 2. new节点
80+
* 3. 更新具体链表信息
81+
*/
82+
int level = randLevel();
83+
Node* newNode = new Node(level, _key);
84+
p = newNode->forward;
85+
for (int i = 1; i <= level; ++i) {
86+
p[i] = update[i]->forward[i];
87+
update[i]->forward[i] = newNode;
88+
}
89+
}
90+
91+
void
92+
ShipList::Delete(int _key)
93+
{
94+
if (Search(_key) < 0) return;
95+
vector<Node*> update(maxLevel, nullptr); // 用于更新
96+
vector<Node*>& p = forward;
97+
for (int i = maxLevel; i > 0; --i) {
98+
if (!p[i]) continue;
99+
if (p[i]->key < _key) {
100+
update[i] = p[i];
101+
p = p[i]->forward;
102+
}
103+
else if (p[i]->key == _key)
104+
return ; // 因为出现相等了
105+
}
106+
107+
// 循环找插入节点
108+
while (1) {
109+
if (p[1]->key < _key) {
110+
update[1] = p[1];
111+
p = p[1]->forward;
112+
} else
113+
break; // =等于跳出循环
114+
}
115+
116+
// 找到的离开该最近的节点
117+
// p是forward的引用
118+
int level = update[1]->forward.size();
119+
Node* delPtr = update[1];
120+
for (int i = 1; i <= level; ++i) {
121+
update[i]->forward[i] = p[i];
122+
}
123+
delete delPtr;
124+
}

cpp/数据结构/ShipList.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <vector>
2+
using std::vector;
3+
4+
5+
struct Node
6+
{
7+
int k; // 表示它的阶数
8+
int key; // key value
9+
vector<Node*> forward;
10+
Node()=default;
11+
Node(int _k, int _key): k(_k), key(_key) {forward.push_back(nullptr);};
12+
};
13+
14+
class ShipList
15+
{
16+
public:
17+
ShipList()=default;
18+
ShipList(double _p, int _maxN);
19+
int Search(int _key); // 这类做简化,只返回key,而不是指针和具体的value
20+
void Insert(int _key);
21+
void Delete(int _key);
22+
private:
23+
int randLevel();
24+
double p;
25+
int maxLevel;
26+
int maxN;
27+
vector<Node*> forward;
28+
Node* _search(Node* node, int _key);
29+
};

0 commit comments

Comments
 (0)