Status comp(ElemType c1,ElemType c2)
{
if(c1==c2)
return TRUE;
else
return FALSE;
}
void visit(ElemType c)
{
printf("%4d",c);
}
void main()
{
LinkList *L;
LinkList *mid;
mid = (struct LNode *)malloc(sizeof(struct LNode));
ElemType *e,e0,*e1;
Status i;
int j,k;
e = (ElemType *)malloc(sizeof(ElemType));
e1 = (ElemType *)malloc(sizeof(ElemType));
i = InitList(&L);
for(j=1;j<=6;j++)
{
i = ListInsert(L,1,j);
}
printf("在L的表头依次插入1~6后:L=");
ListTraverse(L,visit);
printf("L中间节点的值为mid=:");
mid = Searchmid(L);
printf("%d\n",mid->data);
printf("L逆置后的输出:L=");
ListTraverse(reverse(L),visit);
printf("L排序后依次为:L=");
ListTraverse(sort(L),visit);
i = ListEmpty(L);
printf("L 是否为空:i=%d(1:是,0:否)\n",i);
i = ClearList(L);
printf("清空L后:L=");
ListTraverse(L,visit);
i = ListEmpty(L);
printf("L是否为空:i=%d\n",i);
for(j=1;j<=10;j++)
{
ListInsert(L,j,j);
}
printf("在L的表尾依次插入1~10后:L=");
ListTraverse(L,visit);
GetElem(L,5,e);
printf("第5个元素的值为:%d\n",*e);
for(j=0;j<=1;j++)
{
k = LocateElem(L,j,comp);
if(k)
printf("第%d个元素的值为%d\n",k,j);
else
printf("没有值为%d的元素\n",j);
}
for(j=1;j<=2;j++)
{
GetElem(L,j,e1);
i = PriorElem(L,*e1,e);
if(i== INFEASIBLE)
printf("元素%d无前驱\n",*e1);
else
printf("元素%d的前驱为:%d\n",*e1,*e);
}
for(j=ListLength(L) -1;j<=ListLength(L);j++)
{
GetElem(L,j,e1);
i = NextElem(L,*e1,e);
if(i==INFEASIBLE)
printf("元素%d无后继\n",*e1);
else
printf("元素%d的后继为:%d\n",*e1,*e);
}
k = ListLength(L);
for(j=k+1;j>=k;j--)
{
i = ListDelete(L,j,e);
if(i==ERROR)
printf("删除第%d个数据失败\n",j);
else
printf("删除的元素为:%d\n",*e);
}
printf("依次输出L的元素:");
ListTraverse(L,visit);
DestroyList(L);
printf("销毁L后:L=%u\n",L);
printf("*************************************************\n");
LinkList *La,*Lb;
i = InitList(&La);
if(i==1)
for(j=1;j<=5;j++)
i= ListInsert(La,j,j);
printf("La=");
ListTraverse(La,print);
InitList(&Lb);
for(j=1;j<=5;j++)
i = ListInsert(Lb,j,2*j);
printf("Lb = ");
ListTraverse(Lb,print);
Union(La,Lb);
printf("new La=");
ListTraverse(La,print);
printf("*************************************************\n");
LinkList *La_1,*Lb_1,*Lc_1;
int a[4]={3,5,8,11},b[7]= {2,6,8,9,11,15,20};
InitList(&La_1);
for(j=1;j<=4;j++)
ListInsert(La_1,j,a[j-1]);
printf("La_1=");
ListTraverse(La_1,print);
InitList(&Lb_1);
for(j=1;j<=7;j++)
ListInsert(Lb_1,j,b[j-1]);
printf("Lb_1=");
ListTraverse(Lb_1,print);
MergeList(La_1,Lb_1,&Lc_1);
printf("Lc_1=");
ListTraverse(Lc_1,print);
}
|