AcWing 826. 单链表
原题链接
简单
作者:
SillyBoy
,
2021-02-07 09:53:46
,
所有人可见
,
阅读 288
C++ 代码
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int head,ne[N],e[N],idx;
//head表示头节点是第几个插入的
//ne[i]表示第i个插入的数的下一个数是第几个插入的
//e[i]表示第i个插入的数的值
//idx++当前要插入的数应该是第几个插入的
void add_to_head(int x)
{
e[idx] = x,ne[idx] = head,head = idx++;
}
void add_to_afterK(int k,int x)
{
e[idx] = x,ne[idx] = ne[k],ne[k] = idx++;
}
void delete_k(int k)
{
ne[k] = ne[ne[k]];
}
int main()
{
int n;
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
head = 0,idx = 1;
while(n--)
{
char op;
int k,x;
cin >> op;
if(op == 'H'){
cin >> x;
add_to_head(x);
}
else if(op == 'I')
{
cin >> k >> x;
add_to_afterK(k,x);
}
else{
cin >> k;
if(!k) head = ne[head];
else delete_k(k);
}
}
for(int i = head; i!= 0; i = ne[i])
cout << e[i] << " ";
cout << endl;
return 0;
}