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

来源:互联网 发布:sql别名的范围 编辑:程序博客网 时间:2024/05/26 16:00
/*  * Copyright (c).2014, 烟台大学计算机学院  * All rights reserved.  *文件名称:aaa.cpp  *作    者:董子宾  *完成日期:2015年 10月 5日  *版 本 号:v1.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;}

运行结果:

0 0