【模板】链表
posted on 2021-02-25 18:23:15 | under 学术 | source
数组
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
template<int N> struct linked_list{
struct node{int num,ch[2];node(){ch[0]=ch[1]=-1;}};
node a[N+10];
linked_list(){a[0].ch[0]=a[0].ch[1]=0;}
void insert(int pos,int x,int k){
a[x].ch[k]=a[pos].ch[k];
a[x].ch[!k]=pos;
a[a[pos].ch[k]].ch[!k]=x;
a[pos].ch[k]=x;
}
void remove(int pos){
if(a[pos].ch[0]==-1) return ;
a[a[pos].ch[0]].ch[1]=a[pos].ch[1];
a[a[pos].ch[1]].ch[0]=a[pos].ch[0];
a[pos].ch[0]=a[pos].ch[1]=-1;
}
void output(){
for(int i=a[0].ch[1];i;i=a[i].ch[1]){
printf("%d%c",i," \n"[a[i].ch[1]==0]);
}
}
};
int n,m;
linked_list<100010> l;
int main(){
l.insert(0,1,1);
scanf("%d",&n);
for(int i=2;i<=n;i++){int pos,k;
scanf("%d%d",&pos,&k);
l.insert(pos,i,k);
}
scanf("%d",&m);
for(int i=1;i<=m;i++){int x;
scanf("%d",&x);
l.remove(x);
}
l.output();
return 0;
}
更神秘的数组
P1160 AC代码
#include <cstdio>
using namespace std;
class List{
private:
struct node{
int l,r;
node():l(-1),r(-1){}
} *a;
public:
List(int size=1e5){
a=new node[size+10];
a[0].r=1,a[1].l=0;
}
~List(){
delete []a;
}
void addl(int x,int pos){
a[x].r=pos;
if(~a[pos].l) a[a[pos].l].r=x;
a[x].l=a[pos].l;
a[pos].l=x;
}
void addr(int x,int pos){
a[x].l=pos;
if(~a[pos].r) a[a[pos].r].l=x;
a[x].r=a[pos].r;
a[pos].r=x;
}
void del(int x){
if(~a[x].l) a[a[x].l].r=a[x].r;
if(~a[x].r) a[a[x].r].l=a[x].l;
a[x].l=a[x].r=-1;
}
void run(){
for(int i=a[0].r;~i;i=a[i].r){
printf("%d ",i);
}
}
};
List a;
int n,m;
int main(){
scanf("%d",&n);
for(int i=2;i<=n;i++){int pos,op;
scanf("%d%d",&pos,&op);
if(op==0) a.addl(i,pos);
else a.addr(i,pos);
}
scanf("%d",&m);
for(int i=1;i<=m;i++){int x;
scanf("%d",&x);
a.del(x);
}
a.run();
return 0;
}
过不了模板(查找太慢),但功能多
#include <cstdio>
using namespace std;
template<typename T=int>
class Linked_list{
private:
struct node{
T num;
node *l,*r;
node(const T &num,node *l=NULL,node *r=NULL):num(num),l(l),r(r){}
node():l(NULL),r(NULL){}
void del(){if(r!=NULL) r->del(),delete r;}
} head;
public:
~Linked_list(){
head.del();
}
node* find(const T &num){
for(node *i=head.r;i!=NULL;i=i->r){
if(i->num==num) return i;
}
return NULL;
}
int findidx(const T &num){
int now=1;
for(node *i=head.r;i!=NULL;i=i->r,now++){
if(i->num==num) return now;
}
return 0;
}
void addempty(const T &num){
node *tmp=new node(num,&head,NULL);
head.r=tmp;
}
void addl(const T &num,const T &posnum){
node *pos=find(posnum);
node *tmp=new node(num,pos->l,pos);
if(pos->l!=NULL) pos->l->r=tmp;
pos->l=tmp;
}
void addr(const T &num,const T &posnum){
node *pos=find(posnum);
node *tmp=new node(num,pos,pos->r);
if(pos->r!=NULL) pos->r->l=tmp;
pos->r=tmp;
}
void del(const T &num){
node *tmp=find(num);
if(tmp==NULL) return ;
if(tmp->l!=NULL) tmp->l->r=tmp->r;
if(tmp->r!=NULL) tmp->r->l=tmp->l;
delete tmp;
}
template<typename function>
void run(function opt){
for(node *i=head.r;i!=NULL;i=i->r){
opt(i->num);
}
}
};
int n,m;
Linked_list<> a;
void opt(int x){printf("%d ",x);}
int main(){
a.addempty(1);
scanf("%d",&n);
for(int i=2;i<=n;i++){int pos,op;
scanf("%d%d",&pos,&op);
if(op==0) a.addl(i,pos);
else a.addr(i,pos);
}
scanf("%d",&m);
for(int i=1;i<=m;i++){int x;
scanf("%d",&x);
a.del(x);
}
a.run(opt);
return 0;
}
/*
in:
10
1 0
1 1
1 1
1 1
4 1
2 1
2 0
7 1
7 1
5
6
3
4
7
6
out:
8 2 10 9 1 5
*/
`map`优化查询
#include <cstdio>
#include <map>
using namespace std;
template<typename T=int>
class Linked_list{
private:
struct node{
T num;
node *l,*r;
node(const T &num,node *l=NULL,node *r=NULL):num(num),l(l),r(r){}
node():l(NULL),r(NULL){}
void del(){if(r!=NULL) r->del(),delete r;}
} head;
map<T,node*> nodes;
public:
~Linked_list(){head.del();}
node* find(const T &num){
return nodes[num];
}
void addempty(const T &num){
node *tmp=new node(num,&head,NULL);
head.r=tmp;
nodes[num]=tmp;
}
void addl(const T &num,const T &posnum){
node *pos=find(posnum);
node *tmp=new node(num,pos->l,pos);
if(pos->l!=NULL) pos->l->r=tmp;
pos->l=tmp;
nodes[num]=tmp;
}
void addr(const T &num,const T &posnum){
node *pos=find(posnum);
node *tmp=new node(num,pos,pos->r);
if(pos->r!=NULL) pos->r->l=tmp;
pos->r=tmp;
nodes[num]=tmp;
}
void del(const T &num){
node *tmp=find(num);
if(tmp==NULL) return ;
if(tmp->l!=NULL) tmp->l->r=tmp->r;
if(tmp->r!=NULL) tmp->r->l=tmp->l;
nodes.erase(nodes.find(num));
delete tmp;
}
template<typename function>
void run(function opt){
for(node *i=head.r;i!=NULL;i=i->r){
opt(i->num);
}
}
};
int n,m;
Linked_list<> a;
void opt(int x){printf("%d ",x);}
int main(){
//freopen("test.in","r",stdin);
a.addempty(1);
scanf("%d",&n);
for(int i=2;i<=n;i++){int pos,op;
scanf("%d%d",&pos,&op);
if(op==0) a.addl(i,pos);
else a.addr(i,pos);
}
scanf("%d",&m);
for(int i=1;i<=m;i++){int x;
scanf("%d",&x);
a.del(x);
}
a.run(opt);
return 0;
}
/*
in:
10
1 0
1 1
1 1
1 1
4 1
2 1
2 0
7 1
7 1
5
6
3
4
7
6
out:
8 2 10 9 1 5
*/
本文来自博客园,作者:caijianhong,转载请注明原文链接:https://www.cnblogs.com/caijianhong/p/template-linked-list.html