标签:head turn lin insert ext 模板 linked cpp 节点
一个只供删除的双向链表,为了简单不再引入head节点,而且也不进行next的套娃操作。空间使用略微多了一些,但是无伤大雅。
struct LinkedList {
static const int MAXN = 100000;
int n, prev[MAXN + 5], next[MAXN + 5];
void Init(int _n) {
n = _n;
for(int i = 1; i <= n; ++i) {
prev[i] = i - 1;
next[i] = i + 1;
}
prev[1] = -1;
next[n] = -1;
}
void Remove(int x) {
prev[next[x]] = prev[x];
next[prev[x]] = next[x];
}
};
正常的链表是单向链表,删除操作是删除当前节点的下一个节点,没有必要。全部写双向的。b事多还可以写个垃圾回收。
struct LinkedList {
static const int MAXN = 100000;
int n, prev[MAXN + 5], next[MAXN + 5];
static const int head = 0, tail = MAXN + 1;
void Init() {
n = 0;
prev[head] = -1;
next[head] = tail;
prev[tail] = head;
next[tail] = -1;
}
int Insert(int x) {
++n;
prev[next[x]] = n;
next[n] = next[x];
prev[n] = x;
next[x] = n;
return n;
}
void Remove(int x) {
prev[next[x]] = prev[x];
next[prev[x]] = next[x];
}
};
并查集实现的伪链表。
struct PrevLinkedList {
int n;
bool vis[100005];
int prev[100005];
void Init(int _n) {
n = _n;
for(int i = 1; i <= n; ++i) {
vis[i] = 0;
prev[i] = i - 1;
}
}
int Prev(int x) {
int r = prev[x];
while(vis[r])
r = prev[r];
int t;
while(prev[x] != r) {
t = prev[x];
prev[x] = r;
x = t;
}
return r;
}
void Remove(int x) {
vis[x] = 1;
}
};
标签:head turn lin insert ext 模板 linked cpp 节点
原文地址:https://www.cnblogs.com/KisekiPurin2019/p/11925448.html