自己创建一个h头指针,不存东西,h->next是第一个元素,这样做方便获取链表和插入第一个元素,指针数组按次序存储对应节点的指针
#include<cstdio>
#include<vector>
using namespace std;
struct List{
int val;
List *ne;
List(int x = 0, List* next = nullptr): val(x), ne(next){}
};
int main(){
int m;
List* h = new List;
scanf("%d", &m);
vector<List*> inptr(m + 1);
int idx = 0;
char c;
getchar();
while(m--){
c = getchar();
int k, x;
if(c == 'H'){
scanf("%d", &x);
inptr[idx] = new List(x, h->ne);
h->ne = inptr[idx++];
}
else if(c == 'D'){
scanf("%d", &k);
if(k == 0)
h->ne = h->ne->ne;
else if(inptr[k - 1]->ne)
inptr[k - 1]->ne = inptr[k - 1]->ne->ne;
}
else if(c == 'I'){
scanf("%d%d", &k, &x);
inptr[idx] = new List(x, nullptr);
inptr[idx]->ne = inptr[k - 1]->ne;
inptr[k - 1]->ne = inptr[idx++];
}
getchar();
}
List *p = h->ne;
while(p){
printf("%d ", p->val);
p = p->ne;
}
return 0;
}