第四周 【项目3

来源:互联网 发布:所有香烟条形码数据库 编辑:程序博客网 时间:2024/05/21 06:22

/*  烟台大学计算机学院    文件名称:xm.cpp    作者:宋昊    完成日期:2017年9月24日    问题描述:判断链表是否递增   输入描述:无   输出描述:是否为递增链表  用到了前面项目设计的算法库lish.h   */               #include <stdio.h>  #include "../list.h"    void Listicrease(Linklist *&L)  {      Linklist *p,*q;        p=L->next;//p首先指向L的首结点            while(p->next!=NULL)      {          q=p->next;          if(p->data>q->data || p->data==q->data)//如果链表后值大于前值不为递增直接跳出          {              printf("链表不为递增\n");              break;          }            p=q;          }        if(p->next==NULL)//遍历到最后p->next==NULL证明遍历完毕为递增      {           printf("链表为递增\n");        }  }    int main()  {      Linklist *A, *B;      int i;      ElemType a[]= {1, 3, 2, 9};      ElemType b[]= {0, 4, 5 ,6, 7, 8};      initList(A);      for(i=3; i>=0; i--)          ListInsert(A, 1, a[i]);      initList(B);      for(i=5; i>=0; i--)          ListInsert(B, 1, b[i]);          printf("A:");            Listicrease(A);//判断A            printf("B:");            Listicrease(B);//判断B              DestroyList(A);          DestroyList(B);            return 0;                }  


运行结果:


学习心得:


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