链表(数组模拟)
12.链表
1)(单链表)
#include<stdio.h>
const int nill=-1;
const int head=0;
struct my{
int next;
int zhi;
};
struct mylist{
my a[100000];
int n;
int getpos(int i){
int td=head;
for (int j=1;j<=i;j++)
td=a[td].next;
return td;
}
void insert(int pos,int val){
int p=getpos(pos-1);
a[++n].zhi=val;
a[n].next=a[p].next;
a[p].next=n;
}
void det(int pos){
int p=getpos(pos-1);
a[p].next=a[a[p].next].next;
}
}tu;
int main(){
int m;
scanf("%d",&tu.n);
for (int i=1;i<=tu.n;i++)
{
scanf("%d",&tu.a[i].zhi);
tu.a[i-1].next=i;
}
tu.a[tu.n].next=nill;
scanf("%d",&m);
int help;
int v,sum;
for (int i=1;i<=m;i++){
scanf("%d%d",&help,&v);
if(help==2) {
scanf("%d",&sum);
tu.insert(v,sum);
}
if(help==3){
tu.det(v);
}
if(help==1){
printf("%d\n",tu.a[tu.getpos(v)].zhi);
}
}
return 0;
}
2)双向链表
const int nill=-1;
const int head=0;
struct my{
int next;
int zhi;
int prve;
};
struct mylist{
my a[100000];
int n;
int getpos(int i){
int td=head;
for (int j=1;j<=i;j++)
td=a[td].next;
return td;
}
void insert(int pos,int val){
int p=getpos(pos-1);
a[++n].zhi=val;
a[n].next=a[p].next;
a[p].next=n;
a[a[n].next].prve=n;
a[n].prve=p;
}
void det(int pos){
int p=getpos(pos-1);
a[p].next=a[a[p].next].next;
a[a[p].next].prve=p;
}
}tu;