AcWing 826. 单链表
原题链接
简单
作者:
Value
,
2020-05-25 15:07:34
,
所有人可见
,
阅读 562
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1E5 + 10;
int e[N], ne[N];
int head, idx;
void add_to_head(int x){
e[idx] = x;
ne[idx] = head;
head = idx;
idx ++ ;
}
void remove(int k){
ne[k] = ne[ne[k]];
}
void insert(int k, int x){
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx;
idx ++ ;
}
int main(){
int n; scanf("%d", &n);
head = -1, idx = 0;
while(n -- ){
char op[5]; scanf("%s", &op);
if(op[0] == 'H'){
int x; scanf("%d", &x);
add_to_head(x);
}else if(op[0] == 'D'){
int k; scanf("%d", &k);
if(k == 0) head = ne[head];
else remove(k - 1);
}else{
int k, x; scanf("%d%d", &k, &x);
insert(k - 1, x);
}
}
for(int i = head; i != -1; i = ne[i]) cout << e[i] << ' ';
return 0;
}