#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5 + 10;
int head, e[N], ne[N], idx;
int m;
void init()
{
head = -1;
idx = 0;
}
void remove(int k){
ne[k] = ne[ne[k]];
}
void add_top(int x){
e[idx] = x;
ne[idx] = head;
head = idx ++ ;
}
void insert(int k, int x){
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx ++ ;
}
int main()
{
cin>>m;
init();
memset(ne, -1, sizeof(ne));
while(m -- ){
char op;
cin>>op;
if(op == 'H'){
int x;
cin>>x;
add_top(x);
}
else if(op == 'D'){
int k;
cin>>k;
if(k == 0) head = ne[head];
else remove(k - 1);
}
else{
int k, x;
cin>>k>>x;
insert(k - 1, x);
}
}
for(int i = head; i != -1; i = ne[i]){
cout<<e[i]<<" ";
}
return 0;
}
AcWing 826 单链表 题解 (单链表模板)
于 2021-08-26 18:52:05 首次发布
本文详细解析AcWing平台上的第826题,通过实例深入讲解单链表的基本操作和解题思路,提供了一份完整的单链表模板,帮助读者巩固链表算法知识。
摘要由CSDN通过智能技术生成