第4周项目3单链表应用3

来源:互联网 发布:python 接口性能测试 编辑:程序博客网 时间:2024/06/05 18:26

问题及代码:

/*    文件名称:项目1.cpp    作者:孙洁    完成日期:2015.10.6问题描述:    3、设计一个算法,判断单链表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
原创粉丝点击