ZOJ Problem Set - 4016
ZOJ Problem Set - 4016
链表操作,数组实现,其中记得要保存链表的头
参考代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+100;
int shu[maxn];
int point[maxn];
int pre[maxn];
int First[maxn];
int sz;
void Insert(int x,int n)
{
shu[sz] = n;
if(point[x] == 0)
First[x] = sz;
pre[sz] = point[x];
point[x] = sz;
sz++;
}
void Pop(int x)
{
if(point[x] == 0)
{
printf("EMPTY\n");
return ;
}
int t = point[x];
printf("%d\n",shu[t]);
point[x] = pre[t];
}
void Move(int t,int s)
{
if(point[t] == 0)
return ;
if(First[s] == 0)
First[s] = First[t];
pre[First[t]] = point[s];
point[s] = point[t];
point[t] = 0;
First[t] = 0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d %d",&n,&m);
for(int i = 1; i <= n; ++i)
point[i] = 0,First[i] = 0;
sz = 1;
while(m--)
{
int op;
int a,b;
scanf("%d",&op);
if(op == 1)
{
scanf("%d %d",&a,&b);
Insert(a,b);
}
else if(op == 2)
{
scanf("%d",&b);
Pop(b);
}
else
{
scanf("%d %d",&a,&b);
Move(b,a);
}
}
}
return 0;
}
通过STL的list 也可以实现,有兴趣的可以百度一下