数据结构之查找Word文档格式.docx
《数据结构之查找Word文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构之查找Word文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
=k)
j--;
returnj;
}
voidmain()
SSTABLEa;
inti,j,k;
printf("
请输入顺序表元素(整型量),用空格分开,-99为结束标志:
\n"
);
j=0;
k=1;
i=0;
scanf("
%d"
&
i);
while(i!
=-99)
{
j++;
a.r[k].key=i;
k++;
scanf("
}
a.len=j;
\n顺序表元素列表显示:
for(i=1;
i<
=a.len;
i++)
printf("
%d"
a.r[i].key);
\n输入待查元素关键字:
"
k=seq_search(i,a);
if(k==0)
表中的元素不存在\n\n"
else
表中待查元素存在,为第%d个元素\n"
k);
}运行结果
2.
intsearch_bin(SSTABLE&
st,KEYTYPEk,intlow,inthigh)
intmid;
mid=-1;
if(low<
=high)
mid=(low+high)/2;
if(st.r[mid].key<
k)
mid=search_bin(st,k,mid+1,high);
else
if(st.r[mid].key>
mid=search_bin(st,k,low,high-1);
returnmid;
请输人有序表元素,元素为整型量(从小到大输入),用空格分开,-99为结束标志:
j=8;
k=8;
a.len=8;
a.r[1].key=10;
a.r[2].key=24;
a.r[3].key=37;
a.r[4].key=56;
a.r[5].key=80;
a.r[6].key=91;
a.r[7].key=95;
a.r[8].key=97;
//key为什么丢了啊?
?
/
k=search_bin(a,i,1,a.len);
if(k==-1)
}运行结果
stdlib.h>
#defineMaxSize13
#defineNULLKEY0
typedefintKeyType;
typedefchar*InfoType;
structHASH
KeyTypekey;
structHASH*next;
};
voidcreat_hash(HASH*HTC[])
HASH*p;
inti=0,j,k;
//printf("
//scanf("
for(k=0;
k<
6;
k++)
//while(i!
inta[6]={50,69,15,47,38,21};
i=a[k];
j=i%13;
p=(HASH*)malloc(sizeof(HASH));
p->
next=HTC[j];
//p->
next=NULL;
key=i;
HTC[j]=p;
//scanf("
voidprint_hash(HASH*HTC[])
inti;
for(i=0;
13;
if(HTC[i]==NULL)
printf("
%3d|^\n"
i);
{
p=HTC[i];
%3d|"
while(p!
=NULL)
{
printf("
->
p->
key);
p=p->
next;
}
}
intsearch_hash(HASH*HTC[],intk)
j=k%13;
p=HTC[j];
if(p!
while((p->
key!
=k)&
&
(p->
next!
=NULL))
p=p->
if(p->
key==k)
return1;
return0;
return0;
voidinsert_hash(HASH*HTC[],inti)
j=i%13;
p=(HASH*)malloc(sizeof(HASH));
p->
HTC[j]=p;
HASH*HTC[MaxSize]={NULL};
intk,i;
creat_hash(HTC);
print_hash(HTC);
请输入要插入的key:
insert_hash(HTC,i);
请输入要查找的key:
k);
k=search_hash(HTC,k);
if(k)printf("
存在\n"
elseprintf("
不存在\n"
3查找
#include<
malloc.h>
string.h>
#defineMax5
charkey[Max];
}DataType;
typedefstructnode
DataTypedata;
structnode*lchild;
structnode*rchild;
}BiTreeNode;
intSearch(BiTreeNode*root,DataTypeitem)
BiTreeNode*p;
if(root!
p=root;
while(p!
if(strcmp(p->
data.key,item.key)==0)
return1;
if(strcmp(item.key,p->
data.key)<
0)
lchild;
else
rchild;
return0;
intInsert(BiTreeNode**root,DataTypeitem)
BiTreeNode*tree,*parent=NULL,*p;
tree=*root;
while(tree!
if(strcmp(tree->
data.key,item.key)==0)
parent=tree;
data.key,item.key)<
tree=tree->
p=(BiTreeNode*)malloc(sizeof(BiTreeNode));
if(p==NULL)
空间不够!
exit
(1);
data=item;
lchild=NULL;
rchild=NULL;
if(parent==NULL)
*root=p;
elseif(strcmp(item.key,parent->
0)
parent->
lchild=p;
else
rchild=p;
return1;
voidInTraverse(BiTreeNode*root)
if(root==NULL)
return;
if(root->
lchild!
InTraverse(root->
lchild);
%s"
root->
data.key);
rchild!
rchild);
voidmain(void)
DataTypea[]={"
Dac"
"
Fvb"
Nem"
Och"
Jhui"
Sept"
},x={"
Sep"
intn=6,i,s;
BiTreeNode*root=NULL;
n;
Insert(&
root,a[i]);
InTraverse(root);
s=Search(root,x);
if(s==1)
\n数据元素%s存在!
x.key);
\n数据元素不存在!
5.上机体会
对于查找来说,有很多的查找是在排序好的基础上进行的,评价查找方法的好坏不能只看其所需要的时间,还要将它所需要满足的前提条件考虑进去,进行综合评价。
我认为二分查找还是一个很好的查找方法,虽然它要求数据是有序的,正所谓磨刀不误砍柴工。
教师评价
优
良
中
及格
不及格
教师签名
日期