1.顺序表的查找运算
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define ElemType int #define MAXSIZE 100 /*此处的宏定义常量表示线性表可能达到的最大长度*/ typedef struct { ElemType elem[MAXSIZE]; /*线性表占用的数组空间*/ int last; /*记录线性表中最后一个元素在数组elem[ ]中的位置(下标值),空表置为-1*/ }SeqList; /*查找函数实现*/ int Locate(SeqList L, ElemType e) { int i=0; /*i为扫描计数器,初值为0,即从第一个元素开始比较*/ while ((i<=L.last)&&(L.elem[i]!=e)) i++; /*顺序扫描表,直到找到值为key的元素, 或扫描到表尾而没找到*/ if (i<=L.last)return(i+1); /*若找到值为e的元素,则返回其序号*/ else return(-1); /*若没找到,则返回空序号*/ } void main() { SeqList l; int p,q,r; int i; printf("Please input the length of SeqList:"); scanf("%d",&r); l.last = r-1; printf("Please input the each number of SeqList:/n"); for(i=0; i<=l.last; i++) { scanf("%d",&l.elem[i]); } printf("please input the number which you want to find:/n"); scanf("%d",&q); p=Locate(l,q); if(p == -1) printf("there is no number in the SeqList!/n"); else printf("The number which you want to find is at the place of :%d/n",p); }
最后更新:2017-04-02 03:42:38
上一篇:
一步一步SEO 之 网站内容SEO
下一篇:
javascript&JS代码防止复制、另存为的代码
机器学习:如何在安卓上集成TensorFlow
《Docker容器:利用Kubernetes、Flannel、Cockpit和Atomic构建和部署》——2.3 小结
Android SDK 更新不成功的解决方法
IBM WebSphere Application Server V6.1 Fix Pack 37于2011.04.04发布
BAT shell 随记
《计算机存储与外设》----1.4 Cache设计中要考虑的因素
qq安全原理
高晓松谈管理:自嘲总被员工管
org.springframework.core.enums类图
PostgreSQL 为什么不要滥用unlogged table & hash index