您的位置:首页 > 其它

第四周项目3——单链表应用(3)

2015-10-23 09:06 246 查看
项目名称:第四周项目3——单链表应用(3)

作者:江楠

完成时间:2015年10月23日

问题描述:

3、设计一个算法,判断单链表L是否是递增的。实现这个算法,并完成测试。

输出:

如果是递增输出Y,如果不是递增输出N。

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

#include <stdio.h>
#include <malloc.h>
#include "linklist.h"

bool increase(LinkList *L)
{
LinkList *p = L->next, *q;  //p指向第1个数据节点
if(p != NULL)
{
while(p->next != NULL)
{
q = p->next;   //q是p的后继
if (q->data > p->data)   //只要是递增的,就继续考察其后继
p = q;
else
return false;    //只要有一个不是后继大于前驱,便不是递增
}
}
return true;
}

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: %c\n", increase(A)?'Y':'N');
printf("B: %c\n", increase(B)?'Y':'N');
DestroyList(A);
DestroyList(B);
return 0;
}


测试结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: