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

来源:互联网 发布:游戏编程入门第四版 编辑:程序博客网 时间:2024/06/06 05:31
<pre class="csharp" name="code">/*      Copyright (c)2015,烟台大学计算机与控制工程学院      All rights reserved.      文件名称:项目3--单链表应用(3).cpp      作    者:佟兴锋    完成日期:2015年10月4日      版 本 号:v1.0            问题描述:设计一个算法,判断单链表L是否是递增的。实现这个算法,并完成测试。*/

(程序中利用了已经实现的单链表算法,头文件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;}

运行结果:

知识点:

        链表数据递增型判断。

0 0
原创粉丝点击