/*
* Copyright (c) 2015, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:test.cpp
* 作者:王雪洁
* 完成日期:2015年10月9日
* 版本号:vc++6.0
* 问题描述:设计一个算法,判断单链表L是否递增的。实现这个算法,并完成测试。
*/
#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;
}
运行结果:
学习心得:
熟能生巧。
本文介绍了一个用于判断单链表是否递增的算法,并通过示例代码展示了该算法的具体实现过程及测试方法。
&spm=1001.2101.3001.5002&articleId=48994827&d=1&t=3&u=bc07661ed58a4f958e0863a9731437c6)
323

被折叠的 条评论
为什么被折叠?



