索引顺序表的查找!大家来帮帮忙!谢谢!

来源:百度知道 编辑:UC知道 时间:2024/07/04 08:11:04
顺序表为(3,8,7,36,35,21,40,60,47)

索引表
最大关键字 8 36 60
起始地址 1 4 7
思想我知道,就是不知道程序怎么写啊,哪位高手能帮帮忙啊

好好 看看 我写的 简洁 源代码
对你会有帮助的

#include<stdio.h>

int table[]={3,7,8,21,35,36,40,47,60};

struct searchtable{
int stelem;
int location;
};
struct searchtable stable[3]={8,2,36,5,60,8} ;
main(){
int i,j;
int a;
printf("Input a number you want to search:");
scanf("%d",&a);
//在素引表中先查找
for(i=0;i<3;i++)
if(a>stable[i].stelem)continue;
else break;
//确定在主表中需要查找的前后位置i,j
if(i==0||!i<3) j=0;
else j=stable[i-1].location;
if(i<3)i=stable[i].location;
else i=-1;
//通过以上位置在主表中查找
for(;i>j-1;i--)
if(table[i]==a)break;
if(i!=j-1&&i!=-1)printf("\nthe %d location is %d",a,i);
else printf("\nThe table has not the number %d",a);
}

1、顺序查找的基本思想
基本思想是:从表的一端开始,顺序扫描线性表,依次将扫描到的结点关键宇和给定值K相比较。若当前扫描到的结点关键字与K相等,则查找成功;若扫描结束后,仍未找到关键字等于K的结点,则查找失败。