C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
//e[i] i号结点的值 element
//ne[i] i号结点的next的指针值 next element
//head 头结点的下标
//idx 当前用到了哪个点(相当于j)
int e[N], ne[N], head, idx;
//初始化
void init()
{
head = -1;//-1表示为空
idx = 1;//表示从1开始计数(首元节点为0)
}
//将x插入到头结点之前(x变成头结点)
//(也可以理解为插入到首元结点0之后)
void add_head(int x)
{
e[idx] = x; ne[idx] = head;
head = idx;
idx++;
}
//将x插入到下标为k的结点之后
void add_k(int k, int x)
{
e[idx] = x; ne[idx] = ne[k];
ne[k] = idx;
idx++;
}
//删除下标为k的后一结点
void remove(int k)
{
if (k)ne[k] = ne[ne[k]];
else head = ne[head];
}
//remove(0)表示删除头结点(首元结点0的后一结点)
int main()
{
init();
int m; cin >> m;
while (m--)
{
int x, k;
char ch; cin >> ch;
if (ch == 'H')
{
cin >> x;
add_head(x);
}
else if (ch == 'D')
{
cin >> k;
remove(k);
}
else
{
cin >> k >> x;
add_k(k, x);
}
}
//遍历
for (int i = head; i != -1; i = ne[i])printf("%d ", e[i]);
}
求关注