//分块查找,索引顺序查找
#include<stdio.h>
//定义顺序存储线性表的结点结构
struct student
{
long Xuehao;//关键字
int Jihao; //非关键字
};
struct indexNode
{
int address;
int maxKey;
};
int SearchPosition(struct indexNode index[],int length, int someXuehao)
{
int i;
if(someXuehao<=index[0].maxKey)
return index[0].address;
if(someXuehao>=index[length-1].maxKey)
return index[length-1].address;
for(i=0;i<length-1;i++)
{
if(someXuehao>index[i].maxKey && someXuehao<=index[i+1].maxKey)
return index[i+1].address;
}
return -1;
}
int SequencSearch(struct student students[],int startIndex,int length, int someXuehao)
{
//顺序查找,在students[]中查找someXuehao,表长length
//成功返回位置(0~length-1),失败返回-1。
int i;
for(i=startIndex;i<startIndex+length;i++)
{
if(students[i].Xuehao==someXuehao)
return i;
}
return -1;
}
void main()
{
struct student s[15]={1,11,2,22,3,33,4,44,5,55,
6,66,7,77,8,88,9,99,10,1000,
11,110,12,120,13,130,14,140,15,150};
struct indexNode index[3]={0,5,5,10,10,15};
int insertBlockIndex, searchPosition;
insertBlockIndex=SearchPosition(index,3,16);
printf("\ninsertBlockIndex:%d",insertBlockIndex);
searchPosition=SequencSearch(s,insertBlockIndex,5,16);
printf("\n下标为:%d\n",searchPosition);
}2008秋-计算机软件基础- 第四章- 分块查找,索引顺序查找
原创
©著作权归作者所有:来自51CTO博客作者emanlee的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
第四章、PL/SQL基础
1、PL/SQL概述2、PL/SQL块结构3、分支语句的使用4、循环语句的使用5、异常处理的使用6、记录的使用
Oracle PL/SQL PL/SQL块结构 PL/SQL分支语句 PL/SQL循环语句 PL/SQL异常处理 PL/SQL记录 -
《计算机操作系统-第四章》之进程
详细介绍进程
java 数据结构 jvm 原语 数据
















