第四周项目3——单链表应用(3)
2015-10-23 09:06
246 查看
项目名称:第四周项目3——单链表应用(3)
作者:江楠
完成时间:2015年10月23日
问题描述:
3、设计一个算法,判断单链表L是否是递增的。实现这个算法,并完成测试。
输出:
如果是递增输出Y,如果不是递增输出N。
(程序中利用了已经实现的单链表算法,头文件LinkList.h及其中函数的实现见单链表算法库)
测试结果:
作者:江楠
完成时间: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; }
测试结果:
相关文章推荐
- 整合Unity游戏引擎和 Visual Studio Code 代码编辑器 第二弹
- NSTimer知识点和倒计时的实现
- Quartz调度器学习--基本概念
- 第七周项目5-排队看病模拟
- 第七周 项目三 负数把正数赶出队列
- 利用switch语句解决问题:年月日
- 第七周项目 建立链队算法库
- 第六周项目5后缀表达式
- mysql 2013错误解决
- 第8周项目4-字符串加密
- ArcEngine CSharp绘制图形经典代码
- 第七周项目5--排队看病模拟
- iOS 9 学习系列:UIKit Dynamics
- 芯片搜索网站 http://easydatasheet.cn/?c=wp
- 第7周 项目1-建立顺序环形队列算法库
- 第七周项目5-排队看病模拟
- 第八周项目1--建立顺序串的算法库
- 亟待解决,跟着《第一行代码》做得
- java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext
- UI第二天总结