数据结构实验八:查找

第八次实验,实验报告为在课上单独提交,作为实验成绩考核的一部分。

一、实验目的

1、掌握查找的不同方法,并能用高级语言实现查找算法。

2、熟练掌握顺序表的查找方法和有序顺序表的折半查找算法以及静态查找树的构造方法和查找算法。

3、掌握二叉排序树的生成、插入、删除、输出运算。

二、实验内容

1、有序顺序表的二分查找的递归算法。

#include<iostream>
using namespace std;

#define MAXSIZE 100
#define OK 1
#define OVERFLOW -2

typedef struct{
	int key;
}Elemtype;

typedef struct{
	Elemtype *elem;
	int length;
}SSTable;

int Init(SSTable &L){
	L.elem=new Elemtype[MAXSIZE];
	if(!L.elem)
		exit(OVERFLOW);
	L.length=0;
	return OK;
}

int Insert(SSTable &L){
	int j=1;
	for(int i=1;i<MAXSIZE;i++){
		L.elem[i].key=j;
		L.length++;
		j++;
	}
	return OK;
}

int Binsearch(SSTable L,int k,int low,int high){
	int mid;
	if(low<=high){
		mid=(low+high)/2;
		if(L.elem[mid].key==k)
			return mid;
		else if(L.elem[mid].key<k)
			return Binsearch(L,k,mid+1,high);
		else
			return Binsearch(L,k,low,mid-1);
	}
	else
		return 0;
}

void Show(int result,int key){
	if(result==0)
		cout<<"Can't find "<<key<<endl;
	else
		cout<<"Find the "<<key<<" at "<<result<<endl;

}

int main(){
	SSTable T;
	Init(T);
	Insert(T);
	int key_one,result;
	cout<<"input the key:";
	cin>>key_one;
	result=Binsearch(T,key_one,1,99);
	Show(result,key_one);
}
1.腾龙梦屋文章内容无特殊注明皆为源儿原创,转载请注明来源,谢谢!
2.若有相关文章侵犯您的权益,请联系源儿删除,谢谢!
3.相关软件、资料仅供学习参考使用,在24h内务必删除!
腾龙梦屋 » 数据结构实验八:查找

14 评论

  1. Some genuinely great content on this website , appreciate it for contribution. Elijah Wobser

  2. I think this web site has very superb written articles articles . Clifton Motl

  3. Hi there friends, nice paragraph and fastidious urging commented here, I am actually enjoying by these. Junior Reinke

  4. I got what you mean,saved to fav, very nice website. Wilton Persinger

  5. Article writing is also a excitement, if you know afterward you can write or else it is complicated to write. Barton Boggi

  6. I am glad that I observed this site , exactly the right information that I was searching for! . Samual Luebbert

  7. Great, thanks for sharing this blog article. Really thank you! Really Great. Marlin Ahaus

  8. Thankfulness to my father who told me concerning this web site, this webpage is really amazing. Wilbur Loeven

  9. This is my first time visit at here and i am in fact happy to read everthing at alone place. Terrence Fiscal

  10. Awesome! Its genuinely remarkable article, I have got much clear idea on the topic of from this article. Bradford Gabbert

  11. This is a very respected post. Thanks for posting this. Josiah Girillo

    1. Thanks

  12. This is one awesome blog post. Really thank you! Much obliged. Alexis Atkinson

  13. I am regular visitor, how are you everybody? This piece of writing posted at this web page is truly good. Myles Shamlin

评论已关闭

加速支持