数据结构——双向链表(数组模拟)
#include<iostream> using namespace std; const int N = 100010; int val[N],le[N],ri[N]; int idx; int n,m; //初始化 ri[0] 和 le[1]分别为双链表的左右端点 void init(){ ri[0] = 1, le[1] = 0; idx = 2; } //在节点k的右侧插入一个数 void add(int k,int x){ val[idx] = x; ri[idx] = ri[k]; le[idx] = k; le[ri[k]] = idx; ri[k] = idx; idx++; } //删除节点k void del(int k){ le[ ri[k] ] = le[k]; ri[ le[k] ] = ri[k]; } int main(){ cin>>n; string ch; init(); int k,x; while(n--){ cin>>ch; if(ch == "L"){ cin>>x; add(0,x); } if(ch == "R"){ cin>>x; add(le[1],x); } if(ch == "D"){ cin>>x; del(x+1); } if(ch == "IL"){ cin>>k>>x; add(le[k+1],x); } if(ch == "IR"){ cin>>k>>x; add(k+1,x); } } for(int i = ri[0] ; i != 1 ; i = ri[i]){ cout<<val[i]<<" "; } cout<<endl; return 0; }
相关推荐
koushr 2020-11-12
范范 2020-10-28
qiangde 2020-09-13
范范 2020-07-30
mingyunxiaohai 2020-07-19
zhangxiafll 2020-11-13
kikaylee 2020-10-31
MILemon 2020-10-22
hugebawu 2020-10-12
LauraRan 2020-09-28
shenwenjie 2020-09-24
omyrobin 2020-09-23
guangcheng 2020-09-22
hanyujianke 2020-08-18
晨曦之星 2020-08-14
xiesheng 2020-08-06
KAIrving 2020-08-02
xiesheng 2020-08-02