第四周项目三

来源:互联网 发布:计算机编程自学网站 编辑:程序博客网 时间:2024/05/17 16:54
  1. 烟台大学计算机学院  
  2.   
  3. 文件名称:sq.cpp  
  4.   
  5. 作者:zhangsiqi  
  6. 完成日期:2017年9月24日  
  7.   
  8. 问题描述:判断链表是否递增 
  9.   
  10. 输入描述:无 
  11.   
  12. 输出描述:是否为递增链表 
  13.  
  14. 用到了前面项目设计的算法库lish.h 
  15.   
  16. */   
  17.   
  18.   
  19.   
  20.   
  21.   
  22.   
  23. #include <stdio.h>   
  24. #include "../list.h"   
  25.   
  26. void Listicrease(Linklist *&L)  
  27. {  
  28.     Linklist *p,*q;  
  29.   
  30.     p=L->next;//p首先指向L的首结点   
  31.   
  32.   
  33.   
  34.     while(p->next!=NULL)  
  35.     {  
  36.         q=p->next;  
  37.         if(p->data>q->data || p->data==q->data)//如果链表后值大于前值不为递增直接跳出  
  38.         {  
  39.             printf("链表不为递增\n");  
  40.             break;  
  41.         }  
  42.   
  43.         p=q;  
  44.   
  45.   
  46.     }  
  47.   
  48.     if(p->next==NULL)//遍历到最后p->next==NULL证明遍历完毕为递增  
  49.     {  
  50.          printf("链表为递增\n");  
  51.   
  52.     }  
  53. }  
  54.   
  55. int main()  
  56. {  
  57.     Linklist *A, *B;  
  58.     int i;  
  59.     ElemType a[]= {1, 3, 2, 9};  
  60.     ElemType b[]= {0, 4, 5 ,6, 7, 8};  
  61.     initList(A);  
  62.     for(i=3; i>=0; i--)  
  63.         ListInsert(A, 1, a[i]);  
  64.     initList(B);  
  65.     for(i=5; i>=0; i--)  
  66.         ListInsert(B, 1, b[i]);  
  67.         printf("A:");  
  68.   
  69.         Listicrease(A);//判断A   
  70.   
  71.         printf("B:");  
  72.   
  73.         Listicrease(B);//判断B   
  74.   
  75.   
  76.         DestroyList(A);  
  77.         DestroyList(B);  
  78.   
  79.         return 0;  
  80.   
  81.   
  82.   
  83.   
  84.   
  85.   
  86.   
  87. }  



运行结果:


学习心得:


学会了如何用程序判断链表是否递增

 

原创粉丝点击