顺序查找.doc
- 文档编号:344470
- 上传时间:2022-10-09
- 格式:DOC
- 页数:5
- 大小:22KB
顺序查找.doc
《顺序查找.doc》由会员分享,可在线阅读,更多相关《顺序查找.doc(5页珍藏版)》请在冰豆网上搜索。
顺序查找
#include
#defineMAXL10
typedefintKeyType;
typedefstruct
{
KeyTypekey;
}NodeType;
typedefNodeTypeSeqList[MAXL];
intSeqSearch(SeqListR,intn,KeyTypek)
{
inti=0;
while(i =k) { printf("%-3d",R[i].key); i++; } if(i>=n) return-1; else { printf("%d",R[i].key); returni; } } voidmain(){ SeqListR; intn=10; KeyTypek; inta[10],i; printf("输入数字: \n"); for(i=0;i scanf("%d",&a[i]); for(i=0;i R[i].key=a[i]; printf("\n"); printf("输入k: "); scanf("%2d",&k); if((i=SeqSearch(R,n,k))! =-1) printf("\n元素%d的位置是%d\n",k,i+1); else printf("k不存在\n"); } 顺序查找方法二: #include #include #defineOK1 #defineERROR0 #defineN5/*数据元素个数*/ typedefintStatus;/*Status是函数的类型,其值是函数结果状态代码,如OK等*/ typedefintBoolean;/*Boolean是布尔类型,其值是TRUE或FALSE*/ typedefintKeyType;/*设关键字域为整型*/ typedefstruct/*数据元素类型(以教科书图9.1高考成绩为例)*/ { longnumber;/*准考证号*/ charname[9];/*姓名(4个汉字加1个串结束标志)*/ intpolitics;/*政治*/ intChinese;/*语文*/ intEnglish;/*英语*/ intmath;/*数学*/ intphysics;/*物理*/ intchemistry;/*化学*/ intbiology;/*生物*/ KeyTypekey;/*关键字类型应为KeyType,域名应为key,与bo9-1.c中一致*/ }ElemType; ElemTyper[N]={{179324,"何芳芳",85,89,98,100,93,80,47}, {179325,"陈红",85,86,88,100,92,90,45}, {179326,"陆华",78,75,90,80,95,88,37}, {179327,"张平",82,80,78,98,84,96,40}, {179328,"赵小怡",76,85,94,57,77,69,44}};/*全局变量*/ #definetotalkey/*定义总分(total)为关键字*/ #defineEQ(a,b)((a)==(b)) #defineLT(a,b)((a)<(b)) #defineLQ(a,b)((a)<=(b)) typedefstruct { ElemType*elem;/*数据元素存储空间基址,建表时按实际长度分配,0号单元留空*/ intlength;/*表长度*/ }SSTable; StatusCreat_Seq(SSTable*ST,intn) {/*操作结果: 构造一个含n个数据元素的静态顺序查找表ST(数据来自全局数组r)*/ inti; (*ST).elem=(ElemType*)calloc(n+1,sizeof(ElemType));/*动态生成n个数据元素空间(0号单元不用)*/ if(! (*ST).elem) returnERROR; for(i=1;i<=n;i++) *((*ST).elem+i)=r[i-1];/*将全局数组r的值依次赋给ST*/ (*ST).length=n; returnOK; } voidAscend(SSTable*ST) {/*重建静态查找表为按关键字非降序排序*/ inti,j,k; for(i=1;i<(*ST).length;i++) { k=i; (*ST).elem[0]=(*ST).elem[i];/*待比较值存[0]单元*/ for(j=i+1;j<=(*ST).length;j++) ifLT((*ST).elem[j].key,(*ST).elem[0].key) { k=j; (*ST).elem[0]=(*ST).elem[j]; } if(k! =i)/*有更小的值则交换*/ { (*ST).elem[k]=(*ST).elem[i]; (*ST).elem[i]=(*ST).elem[0]; } } } StatusCreat_Ord(SSTable*ST,intn) {/*操作结果: 构造一个含n个数据元素的静态按关键字非降序查找表ST*/ /*数据来自全局数组r*/ Statusf; f=Creat_Seq(ST,n); if(f) Ascend(ST); returnf; } StatusDestroy(SSTable*ST) {/*初始条件: 静态查找表ST存在。 操作结果: 销毁表ST*/ free((*ST).elem); (*ST).elem=NULL; (*ST).length=0; returnOK; } intSearch_Seq(SSTableST,KeyTypekey) {/*在顺序表ST中顺序查找其关键字等于key的数据元素。 若找到,则函数值为*/ /*该元素在表中的位置,否则为0。 */ inti; ST.elem[0].key=key;/*哨兵*/ for(i=ST.length;! EQ(ST.elem[i].key,key);--i);/*从后往前找*/ returni;/*找不到时,i为0*/ } intSearch_Bin(SSTableST,KeyTypekey) {/*在有序表ST中折半查找其关键字等于key的数据元素。 若找到,则函数值为*/ /*该元素在表中的位置,否则为0。 */ intlow,high,mid; low=1;/*置区间初值*/ high=ST.length; while(low<=high) { mid=(low+high)/2; ifEQ(key,ST.elem[mid].key)/*找到待查元素*/ returnmid; elseifLT(key,ST.elem[mid].key) high=mid-1;/*继续在前半区间进行查找*/ else low=mid+1;/*继续在后半区间进行查找*/ } return0;/*顺序表中不存在待查元素*/ } StatusTraverse(SSTableST,void(*Visit)(ElemType)) {/*初始条件: 静态查找表ST存在,Visit()是对元素操作的应用函数*/ /*操作结果: 按顺序对ST的每个元素调用函数Visit()一次且仅一次。 */ /*一旦Visit()失败,则操作失败*/ ElemType*p; inti; p=++ST.elem;/*p指向第一个元素*/ for(i=1;i<=ST.length;i++) Visit(*p++); returnOK; } voidprint(ElemTypec)/*Traverse()调用的函数*/ {printf("%-8ld%-8s%4d%5d%5d%5d%5d%5d%5d%5d\n",c.number,c.name,c.politics,c.Chinese,c.English,c.math,c.physics,c.chemistry,c.biology,c.total); } voidmain() { SSTablest; inti,s; for(i=0;i r[i].total=r[i].politics+r[i].Chinese+r[i].English+r[i].math+r[i].physics+r[i].chemistry+r[i].biology; Creat_Seq(&st,N);/*由全局数组产生静态查找表st*/ printf("准考证号姓名政治语文外语数学物理化学生物总分\n"); Traverse(st,print);/*按顺序输出静态查找表st*/ printf("请输入待查找人的总分: "); scanf("%d",&s); i=Search_Seq(st,s);/*顺序查找*/ if(i) print(*(st.elem+i)); else printf("没找到\n"); Destroy(&st); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 顺序 查找
![提示](https://static.bdocx.com/images/bang_tan.gif)