1.顺序表的查找运算

 <textarea cols="105" rows="47" name="code" class="cpp:nogutter:nocontrols:collapse">#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;malloc.h&gt; #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&lt;=L.last)&amp;&amp;(L.elem[i]!=e)) i++; /*顺序扫描表,直到找到值为key的元素, 或扫描到表尾而没找到*/ if (i&lt;=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",&amp;r); l.last = r-1; printf("Please input the each number of SeqList:/n"); for(i=0; i&lt;=l.last; i++) { scanf("%d",&amp;l.elem[i]); } printf("please input the number which you want to find:/n"); scanf("%d",&amp;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); } </textarea>

转载于:https://my.oschina.net/zhangqs008/blog/712745

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值