AcWing 826. 单链表
原题链接
简单
作者:
鑫霜
,
2022-02-27 18:39:49
,
所有人可见
,
阅读 124
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int m, k, x, idx, head;
int e[N], ne[N];
void init()
{
head = -1;
idx = 0;
}
void insert_h(int x)
{
e[idx] = x;
ne[idx] = head;
head = idx ++;
}
void Delete(int k)
{
ne[k] = ne[ne[k]];
}
void insert(int k, int x)
{
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx ++;
}
int main()
{
init();
scanf("%d", &m);
while (m --)
{
char c;
scanf("%s", &c);
if(c == 'H')
{
scanf("%d", &x);
insert_h(x);
}
else if(c == 'D')
{
scanf("%d", &k);
if(k == 0) head = ne[head];
Delete(k - 1);
}
else
{
scanf("%d %d", &k, &x);
insert(k - 1, x);
}
}
for(int i = head; i != -1; i = ne[i]) cout << e[i] << ' ';
return 0;
}