l81.cpp

来自「《数据结构(C++描述)》-李根强-源代码」· C++ 代码 · 共 24 行

CPP
24
字号
//顺序查找的实现
#include<iostream.h>
const   int  n=10 ;
typedef int elemtype;                            //n为表的最大长度
struct    node
{elemtype   key;                              //key为关键字,类型设定为elemtype
};
node R[n+1]={0,2,4,6,8,1,3,5,7,10,9};
int  seqsearch (node  R[n+1],elemtype k)             //在表R中查找关键字值为K的元素
{
R[0].key=k; int i=n;                      //从表尾开始向前扫描
while(R[i].key!=k)    i--;
return i;
}
void main()
{ int k,j;
cout<<" 线性表为:"<<endl;
for(j=1;j<=n;j++) cout<<R[j].key<<"  ";
cout<<endl<<"请输入待查元素";
cin>>k;
j=seqsearch(R,k);
cout<<endl<<"待查元素"<<k<<"的位置为:"<<j<<endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?