问题及描述:
/*
烟台大学计算机学院
文件名称:fa.cpp
作者:李金朴
完成日期:2017年9月27日
问题描述:判断链表是否递增
输入描述:无
输出描述:是否为递增链表
用到了前面项目设计的算法库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;
}
运行结果:
心得:
学会了如何用程序判断链表是否递增。