C语言 顺序查找
#include <iostream.h> #include <stdio.h> #define MAXITEM 100 struct element{ int key; // 关键字 //int data; // 其他数据 }; typedef struct element sqlist[MAXITEM]; int find(sqlist r, int k, int n) // k 为给定值,返回 i 为关键字等于 k 的记录在表 r 中的序号, // i 值为 0 表示查找不成功 { int i; r[0].key=k; i=n; while (r[i].key!=k) i--; // 逐个向前比较 return (i); } void main(void) { sqlist b; int j=0,findnum,result; do { cout<<"please enter a number:"<<ends; cin>>b[j+1].key; // 从下标为1开始输入 j++; }while(j<5); cout<<endl<<b[0].key; // 下标为零的数字为监视哨 不需要存放任何值 cout<<endl<<"please enter a number you want to find"<<ends; cin>>findnum; result=find(b,findnum,j); if(result!=0) cout<<endl<<"you find number is the "<<result<<" s"<<endl; else cout<<endl<<"can not find"<<endl; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论