AcWing 827. 双链表
原题链接
简单
作者:
Yoshiki..
,
2024-04-12 22:31:19
,
所有人可见
,
阅读 2
STL list 解法
#include <iostream>
#include <cstring>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
list <int> l;
vector<list<int>::iterator> pos(1);
bool erz[100010];
void solve(){
string op;cin>>op;
if(op=="L"){
int x;cin>>x;
l.push_front(x);
pos.push_back(l.begin());
}else if(op=="R"){
int x;cin>>x;
l.push_back(x);
pos.push_back(prev(l.end()));
}else if(op=="IL"){
int k,x;cin>>k>>x;
pos.push_back(l.insert(pos[k],x));
}else if(op=="IR"){
int k,x;cin>>k>>x;
pos.push_back(l.insert(next(pos[k]),x));
}else if(op=="D"){
int k;cin>>k;
if(!erz[k]) l.erase(pos[k]);
}
}
int main(){
ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
int _;cin>>_;
while(_--)solve();
for(auto &x:l){
cout<<x<<" \n"[&x==&l.back()];
}
return 0;
}