main2-3-2.c 两个仅设表尾指针的循环链表的合并(教科书图2.13)

来源:互联网 发布:centos 火狐flash插件 编辑:程序博客网 时间:2024/05/16 13:37

 /* algo2-10.c 两个仅设表尾指针的循环链表的合并(教科书图2.13) */
 #include"c1.h"
 typedef int ElemType;
 #include"c2-2.h"
 #include"bo2-4.c"
 #include"func2-3.c" /* 包括equal()、comp()、print()、print2()和print1()函数 */

 void MergeList(LinkList *La,LinkList Lb)
 { /* 将Lb合并到La的表尾,由La指示新表 */
   LinkList p=Lb->next;
   Lb->next=(*La)->next;
   (*La)->next=p->next;
   free(p);
   *La=Lb;
 }                                            ?????????????

 int main()
 {
   int n=5,i;
   LinkList La,Lb;
   InitList(&La);
   for(i=1;i<=n;i++)
     ListInsert(&La,i,i);
   printf("La="); /* 输出链表La的内容 */
   ListTraverse(La,print);
   InitList(&Lb);
   for(i=1;i<=n;i++)
     ListInsert(&Lb,1,i*2);
   printf("Lb="); /* 输出链表Lb的内容 */
   ListTraverse(Lb,print);
   MergeList(&La,Lb);
   printf("La+Lb="); /* 输出合并后的链表的内容 */
   ListTraverse(La,print);
 }


 

运行:

[root@localhost algorithm]# gcc algo2-10.c -o algo2-10
[root@localhost algorithm]# ls
algo2-10  algo2-10.c  bo2-4.c  c1.h  c2-2.h  func2-3.c
[root@localhost algorithm]# ./algo2-10
La=1 2 3 4 5
Lb=10 8 6 4 2
La+Lb=1 2 3 4 5 10 8 6 4 2

原创粉丝点击