数据结构模拟
链表
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int head, e[N], ne[N], idx;
void init(){
head = -1;
idx = 0;
}
void insert_head(int x){ // 插入到头
e[idx] = x, ne[idx] = head, head = idx ++;
}
void insert(int k, int x){ // 插入到第 k 个插入的数
e[idx] = x, ne[idx] = ne[k], ne[k] = idx ++;
}
void remove(int k){
ne[k] = ne[ne[k]];
}
int main(){
int n; cin >> n;
init();
while (n -- ){
char a; cin >> a;
if (a == 'H'){
int x; cin >> x;
insert_head(x);
} else if (a == 'D'){
int k; cin >> k;
if (!k) head = ne[head];
else remove(k - 1);
} else {
int k, x; cin >> k >> x;
insert(k - 1, x);
}
}
for (int i = head; i != -1; i = ne[i]){
cout << e[i] << ' ';
}
cout << endl;
}栈
单调栈

队列
单调队列
并查集
堆
哈希表
最后更新于