单链表完整代码
#include<iostream>
using namespace std;
const int M = 1e5 + 10;
int m, k, x, idx, head;
int e[M], ne[M];
void init()
{
ne[0] = -1, idx = 1;
}
void remove(int k)
{
ne[k] = ne[ne[k]];
}
void add(int k, int x)
{
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx++;
}
int main()
{
init();
cin >> m;
while (m--)
{
char op;
cin >> op;
if (op == 'H')
{
cin >> x;
add(0, x);
}
else if (op == 'D')
{
cin >> k;
if (!k) head = ne[head];
remove(k);
}
else
{
cin >> k >> x;
add(k, x);
}
}
for (int i = ne[0]; i != -1; i = ne[i]) cout << e[i] << ' ';
cout << endl;
return 0;
}