第四周 项目3

来源:互联网 发布:淘宝怎么设置员工旺旺 编辑:程序博客网 时间:2024/06/04 20:13

项目 - 单链表算法 (程序中利用了已经实现的单链表算法,头文件LinkList.h及其中函数的实现见单链表算法库)

  1、设计一个算法,将一个带头结点的数据域依次为a1,a2,…,an(n≥3)的单链表的所有结点逆置,即第一个结点的数据域变为an,…,最后一个结点的数据域为a1。实现这个算法,并完成测试。 
[参考解答] 

(程序中利用了已经实现的单链表算法,头文件LinkList.h及其中函数的实现见单链表算法库)

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "linklist.h"  
  4. void Reverse(LinkList *&L)  
  5. {  
  6.     LinkList *p=L->next,*q;  
  7.     L->next=NULL;  
  8.     while (p!=NULL)     //扫描所有的结点  
  9.     {  
  10.         q=p->next;      //让q指向*p结点的下一个结点  
  11.         p->next=L->next;    //总是将*p结点作为第一个数据结点  
  12.         L->next=p;  
  13.         p=q;            //让p指向下一个结点  
  14.     }  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     LinkList *L;  
  20.     ElemType a[]= {1,3,5,7, 2,4,8,10};  
  21.     CreateListR(L,a,8);  
  22.     printf("L:");  
  23.     DispList(L);  
  24.     Reverse(L);  
  25.     printf("逆置后L: ");  
  26.     DispList(L);  
  27.     DestroyList(L);  
  28.     return 0;  
  29. }  

2、已知L1和L2分别指向两个单链表的头结点,且已知其长度分别为m、n,请设计算法将L2连接到L1的后面。实现这个算法,完成测试,并分析这个算法的复杂度。 
   
[参考解答] 
(程序中利用了已经实现的单链表算法,头文件LinkList.h及其中函数的实现见单链表算法库

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "linklist.h"  
  4.   
  5. void Link(LinkList *&L1, LinkList *&L2)  
  6. {  
  7.     LinkList *p = L1;  
  8.     while(p->next != NULL)   //找到L1的尾节点  
  9.         p = p->next;  
  10.     p->next = L2->next;  //将L2的首个数据节点连接到L1的尾节点后  
  11.     free(L2);   //释放掉已经无用的L2的头节点  
  12. }  
  13.   
  14. int main()  
  15. {  
  16.     LinkList *A, *B;  
  17.     int i;  
  18.     ElemType a[]= {1,3,2,9};  
  19.     ElemType b[]= {0,4,7,6,5,8};  
  20.     InitList(A);  
  21.     for(i=3; i>=0; i--)  
  22.         ListInsert(A, 1, a[i]);  
  23.     InitList(B);  
  24.     for(i=5; i>=0; i--)  
  25.         ListInsert(B, 1, b[i]);  
  26.     Link(A, B);  
  27.     printf("A:");  
  28.     DispList(A);  
  29.     DestroyList(A);  
  30.     return 0;  
  31. }  

算法复杂度为O(m),只需要由L1的头节点找到其尾节点即可,与L1的长度相关,与L2的长度n无关。 
  

  3、设计一个算法,判断单链表L是否是递增的。实现这个算法,并完成测试。 
   
[参考解答] 
(程序中利用了已经实现的单链表算法,头文件LinkList.h及其中函数的实现见单链表算法库)

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "linklist.h"  
  4.   
  5. bool increase(LinkList *L)  
  6. {  
  7.     LinkList *p = L->next, *q;  //p指向第1个数据节点  
  8.     if(p != NULL)  
  9.     {  
  10.         while(p->next != NULL)  
  11.         {  
  12.             q = p->next;   //q是p的后继  
  13.             if (q->data > p->data)   //只要是递增的,就继续考察其后继  
  14.                 p = q;  
  15.             else  
  16.                 return false;    //只要有一个不是后继大于前驱,便不是递增  
  17.         }  
  18.     }  
  19.     return true;  
  20. }  
  21.   
  22. int main()  
  23. {  
  24.     LinkList *A, *B;  
  25.     int i;  
  26.     ElemType a[]= {1, 3, 2, 9};  
  27.     ElemType b[]= {0, 4, 5 ,6, 7, 8};  
  28.     InitList(A);  
  29.     for(i=3; i>=0; i--)  
  30.         ListInsert(A, 1, a[i]);  
  31.     InitList(B);  
  32.     for(i=5; i>=0; i--)  
  33.         ListInsert(B, 1, b[i]);  
  34.     printf("A: %c\n", increase(A)?'Y':'N');  
  35.     printf("B: %c\n", increase(B)?'Y':'N');  
  36.     DestroyList(A);  
  37.     DestroyList(B);  
  38.     return 0;  
  39. }