e[0]=1 e[1]=2 e[2]=3 e[3]=4 e[4]=5 e[5]=6
ne[0]=1 ne[1]=2 ne[2]=3 ne[3]=4 ne[4]=5 ne[5]=-1
Use ne to store the next subscript of e
initialization
We specify that -1 is set to an empty node
void init()
{
head = -1;
idx = 0;
}
plug
First open up new nodes
e[idx]=x;
Then let the next node of the changed node point to the current first node (head)
ne[idx]=head;
Then disconnect the connection between head and the original first node, head points to the inserted node, and finally idx++;
head = idx;
idx++;
So the code for plugging in is:
void add_to_head(int x)
{
e[idx] = x;
ne[idx] = head;
head = idx;
idx++;
}
Insert x after the element with subscript k
void add(int k, int x)
{
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx;
idx++;
}
delete
void move(int k)
{
ne[k] = ne[ne[k]];
}
topic:
AC code:
#include<iostream>
using namespace std;
const int N = 100010;
int head, e[N], ne[N], idx;
void init()
{
head = -1;
idx = 0;
}
void add_to_head(int x)
{
e[idx] = x;
ne[idx] = head;
head = idx;
idx++;
}
//在下标为k的元素后面插入x
void add(int k, int x)
{
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx;
idx++;
}
//将下标为k的点后面的点删除掉
void move(int k)
{
ne[k] = ne[ne[k]];
}
int main(void)
{
int m;
cin >> m;
init();
while (m--)
{
int k, x;
char op;
cin >> op;
if (op == 'H')
{
cin >> x;
add_to_head(x);
}
else if (op == 'D')
{
cin >> k;
if (k == 0)
{
head = ne[head];
}
move(k - 1);
}
else
{
cin >> k >> x;
add(k - 1, x);
}
}
for (int i = head; i != -1; i = ne[i])
{
cout << e[i] << ' ';
}
return 0;
}