线性表的查找
1.线性表的顺序查找与二分法查找
#include <iostream>
using namespace std;
#define MAXL 20
typedef int KeyType;
typedef int InfoType;
typedef struct
{
KeyType key;
InfoType data;
}NodeType;
typedef NodeType SeqList[MAXL];//顺序表类
int SeqSearch(SeqList R,int n,KeyType k)
{
int i=0;
while(i<n && R[i].key!=k)
{
cout<<"正在与"<<R[i].key<<"比较"<<endl;
i++;
}
if(i>=n)
{
cout<<"查找失败"<<endl;
return 0;
}
else
{
cout<<"查找成功"<<endl;
return i+1;
}
}
int BinSearch(SeqList R,int n,KeyType k)
{
int low=0,high=n-1,mid;
int i=1;
while(low<=high)
{
mid=(low+high)/2;
cout<<"进行第"<<i<<"次折半"<<endl;
if(R[mid].key>k)
{
high=mid-1;
cout<<"查找范围为:"<<R[low].key<<"-