下面是小弟编得,你可以看看,不过我不是用的子函数
#define MAX 7
struct number
{int data;
int cur;
};
void init(struct number *list)
{int i=0;
for(;i<MAX-1;i++) list[i].cur=i+1;
/*inite list*/
list[MAX-1].cur=0;
/*end inite*/
}/*end init*/
int lmalloc(struct number *list)
/*get node from the list*/
{int i;
i=list[0].cur;
if(list[0].cur) list[0].cur=list[i].cur;
return(i);
}/*end lmalloc*/
int length(struct number *list)
{int len=0,n=list[1].cur;
while(n)
{n=list[n].cur;
len++;
}
return(len);
}/*end length*/
void print(struct number *list)
{int i=1;
printf("the list is :\n");
printf("
label
data
cur\n");
for(i=list[i].cur;list[i].cur!=0;i=list[i].cur)
printf("
%-4d
%-4d
%-4d\n",i,list[i].data,list[i].cur);
printf("
%-4d
%-4d
%-4d\n",i,list[i].data,list[i].cur);
for(i=0;list[i].cur!=0;i=list[i].cur)
printf("
%-4d
%-4d\n",i,list[i].cur);
if(list[0].cur!=0)
printf("
%-4d
0\n",i);
}/*end print*/
main()
{struct number space[MAX];
int r,s,t,n,m,i,j,b,k,p;
init(space);
s=lmalloc(space);
r=s;
loop:
{printf("input the amount of list A(between 1 and %d):",MAX-2);
scanf("%d",&n);
}
if(n<1||n>MAX-2)
{printf("input error!\n");
goto loop;
}
printf("input the elems of list A:");
for(j=1;j<=n;j++)
{i=lmalloc(space);
scanf("%d",&space[i].data);
space[r].cur=i;
r=i;
}
space[r].cur=0;
printf("the length of list A is %d\n",length(space));
print(space);
label:
{printf("input the amount of list B:");
scanf("%d",&m);
}
if(m<=0)
{label0:
printf("do you want to continue(1) or exit(0)?");
scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\n");
goto label0;
}
}/*end if*/
for(j=1;j<=m;j++)
{printf("input one elem of list B:");
scanf("%d",&b);
p=s;
k=space[s].cur;
while(k!=space[r].cur&&space[k].data!=b)
{p=k;
k=space[k].cur;
}
if(k==space[r].cur)
/*insert*/
{if(length(space)==MAX-2)
/*judge whether the list is full or
not*/
{printf("the list is full!\nyou can not insert any elem before you
delete!\n");
printf("the length of the list is %d\n",length(space));
print(space);
label1: printf("do you want to delete(1) or exit(0)?");
scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\n");
goto label1;
}
}/*end judge*/
else
/*insert b at the end of list A*/
{i=lmalloc(space);
space[i].data=b;
space[i].cur=space[r].cur;
space[r].cur=i;
r=i;
}
}/*end insert*/
else
/*delete*/
{space[p].cur=space[k].cur;
space[k].cur=space[0].cur;
space[0].cur=k;
if(r==k) r=p;
}
if(length(space)==0)
{printf("the list is empty!\nyou can not delete any elem before
you insert!\n");
/*judge whether the list is empty or not*/
printf("the length of the list is 0\n");
label2: printf("do you want to insert(1) or exit(0)?");
scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\n");
goto label2;
}
}/*end if*/
printf("the length of the list is %d\n",length(space));
print(space);
if(length(space)==MAX-2)
{printf("the list is full!\nyou can not insert any elem before you
delete!\n");
printf("do you want to delete(1) or exit(0)?");
label3: scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\ninput again:");
goto label3;
}
}/*end if*/
}/*end for*/
printf("do you want to continue(1) or exit(0)?");
lab: scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\ninput again:");
goto lab;
}
}#define MAX 7
struct number
{int data;
int cur;
};
void init(struct number *list)
{int i=0;
for(;i<MAX-1;i++) list[i].cur=i+1;
/*inite list*/
list[MAX-1].cur=0;
/*end inite*/
}/*end init*/
int lmalloc(struct number *list)
/*get node from the list*/
{int i;
i=list[0].cur;
if(list[0].cur) list[0].cur=list[i].cur;
return(i);
}/*end lmalloc*/
int length(struct number *list)
{int len=0,n=list[1].cur;
while(n)
{n=list[n].cur;
len++;
}
return(len);
}/*end length*/
void print(struct number *list)
{int i=1;
printf("the list is :\n");
printf("
label
data
cur\n");
for(i=list[i].cur;list[i].cur!=0;i=list[i].cur)
printf("
%-4d
%-4d
%-4d\n",i,list[i].data,list[i].cur);
printf("
%-4d
%-4d
%-4d\n",i,list[i].data,list[i].cur);
for(i=0;list[i].cur!=0;i=list[i].cur)
printf("
%-4d
%-4d\n",i,list[i].cur);
if(list[0].cur!=0)
printf("
%-4d
0\n",i);
}/*end print*/
main()
{struct number space[MAX];
int r,s,t,n,m,i,j,b,k,p;
init(space);
s=lmalloc(space);
r=s;
loop:
{printf("input the amount of list A(between 1 and %d):",MAX-2);
scanf("%d",&n);
}
if(n<1||n>MAX-2)
{printf("input error!\n");
goto loop;
}
printf("input the elems of list A:");
for(j=1;j<=n;j++)
{i=lmalloc(space);
scanf("%d",&space[i].data);
space[r].cur=i;
r=i;
}
space[r].cur=0;
printf("the length of list A is %d\n",length(space));
print(space);
label:
{printf("input the amount of list B:");
scanf("%d",&m);
}
if(m<=0)
{label0:
printf("do you want to continue(1) or exit(0)?");
scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\n");
goto label0;
}
}/*end if*/
for(j=1;j<=m;j++)
{printf("input one elem of list B:");
scanf("%d",&b);
p=s;
k=space[s].cur;
while(k!=space[r].cur&&space[k].data!=b)
{p=k;
k=space[k].cur;
}
if(k==space[r].cur)
/*insert*/
{if(length(space)==MAX-2)
/*judge whether the list is full or
not*/
{printf("the list is full!\nyou can not insert any elem before you
delete!\n");
printf("the length of the list is %d\n",length(space));
print(space);
label1: printf("do you want to delete(1) or exit(0)?");
scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\n");
goto label1;
}
}/*end judge*/
else
/*insert b at the end of list A*/
{i=lmalloc(space);
space[i].data=b;
space[i].cur=space[r].cur;
space[r].cur=i;
r=i;
}
}/*end insert*/
else
/*delete*/
{space[p].cur=space[k].cur;
space[k].cur=space[0].cur;
space[0].cur=k;
if(r==k) r=p;
}
if(length(space)==0)
{printf("the list is empty!\nyou can not delete any elem before
you insert!\n");
/*judge whether the list is empty or not*/
printf("the length of the list is 0\n");
label2: printf("do you want to insert(1) or exit(0)?");
scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\n");
goto label2;
}
}/*end if*/
printf("the length of the list is %d\n",length(space));
print(space);
if(length(space)==MAX-2)
{printf("the list is full!\nyou can not insert any elem before you
delete!\n");
printf("do you want to delete(1) or exit(0)?");
label3: scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\ninput again:");
goto label3;
}
}/*end if*/
}/*end for*/
printf("do you want to continue(1) or exit(0)?");
lab: scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\ninput again:");
goto lab;
}
}#define MAX 7
struct number
{int data;
int cur;
};
void init(struct number *list)
{int i=0;
for(;i<MAX-1;i++) list[i].cur=i+1;
/*inite list*/
list[MAX-1].cur=0;
/*end inite*/
}/*end init*/
int lmalloc(struct number *list)
/*get node from the list*/
{int i;
i=list[0].cur;
if(list[0].cur) list[0].cur=list[i].cur;
return(i);
}/*end lmalloc*/
int length(struct number *list)
{int len=0,n=list[1].cur;
while(n)
{n=list[n].cur;
len++;
}
return(len);
}/*end length*/
void print(struct number *list)
{int i=1;
printf("the list is :\n");
printf("
label
data
cur\n");
for(i=list[i].cur;list[i].cur!=0;i=list[i].cur)
printf("
%-4d
%-4d
%-4d\n",i,list[i].data,list[i].cur);
printf("
%-4d
%-4d
%-4d\n",i,list[i].data,list[i].cur);
for(i=0;list[i].cur!=0;i=list[i].cur)
printf("
%-4d
%-4d\n",i,list[i].cur);
if(list[0].cur!=0)
printf("
%-4d
0\n",i);
}/*end print*/
main()
{struct number space[MAX];
int r,s,t,n,m,i,j,b,k,p;
init(space);
s=lmalloc(space);
r=s;
loop:
{printf("input the amount of list A(between 1 and %d):",MAX-2);
scanf("%d",&n);
}
if(n<1||n>MAX-2)
{printf("input error!\n");
goto loop;
}
printf("input the elems of list A:");
for(j=1;j<=n;j++)
{i=lmalloc(space);
scanf("%d",&space[i].data);
space[r].cur=i;
r=i;
}
space[r].cur=0;
printf("the length of list A is %d\n",length(space));
print(space);
label:
{printf("input the amount of list B:");
scanf("%d",&m);
}
if(m<=0)
{label0:
printf("do you want to continue(1) or exit(0)?");
scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\n");
goto label0;
}
}/*end if*/
for(j=1;j<=m;j++)
{printf("input one elem of list B:");
scanf("%d",&b);
p=s;
k=space[s].cur;
while(k!=space[r].cur&&space[k].data!=b)
{p=k;
k=space[k].cur;
}
if(k==space[r].cur)
/*insert*/
{if(length(space)==MAX-2)
/*judge whether the list is full or
not*/
{printf("the list is full!\nyou can not insert any elem before you
delete!\n");
printf("the length of the list is %d\n",length(space));
print(space);
label1: printf("do you want to delete(1) or exit(0)?");
scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\n");
goto label1;
}
}/*end judge*/
else
/*insert b at the end of list A*/
{i=lmalloc(space);
space[i].data=b;
space[i].cur=space[r].cur;
space[r].cur=i;
r=i;
}
}/*end insert*/
else
/*delete*/
{space[p].cur=space[k].cur;
space[k].cur=space[0].cur;
space[0].cur=k;
if(r==k) r=p;
}
if(length(space)==0)
{printf("the list is empty!\nyou can not delete any elem before
you insert!\n");
/*judge whether the list is empty or not*/
printf("the length of the list is 0\n");
label2: printf("do you want to insert(1) or exit(0)?");
scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\n");
goto label2;
}
}/*end if*/
printf("the length of the list is %d\n",length(space));
print(space);
if(length(space)==MAX-2)
{printf("the list is full!\nyou can not insert any elem before you
delete!\n");
printf("do you want to delete(1) or exit(0)?");
label3: scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\ninput again:");
goto label3;
}
}/*end if*/
}/*end for*/
printf("do you want to continue(1) or exit(0)?");
lab: scanf("%d",&t);
if(t==1) goto label;
else if(t==0) exit(0);
else {printf("input error!\ninput again:");
goto lab;
}
}