AcWing 826. 单链表
原题链接
简单
作者:
戈小小兀
,
2025-01-17 16:11:49
,
所有人可见
,
阅读 1
#include<iostream>
using namespace std;
const int N = 1e5+10;
int head, e[N], ne[N], idx;
void add_to_head(int x){
e[idx] = x;
ne[idx] = head;
head = idx;
idx++;
}
void add_to_k(int k, int x)
{
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx;
idx++;
}
void remove(int x)
{
ne[x] = ne[ne[x]];
}
int main(){
head = -1;
idx = 0;
int m;
cin>>m;
char c;
while(m--)
{
cin>>c;
if(c=='H'){
int x;
cin>>x;
add_to_head(x);
}
else if(c=='D'){
int k;
cin>>k;
if(k==0) head = ne[head];
remove(k-1);
}
else{
int k,x;
cin>>k>>x;
add_to_k(k-1,x);
}
}
for(int i = head;i!=-1;i = ne[i])
cout<<e[i]<<" ";
}