2008秋-计算机软件基础- 第四章- 分块查找,索引顺序查找

//分块查找,索引顺序查找
#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);
}

posted @ 2007-10-14 20:15  emanlee  阅读(634)  评论(0编辑  收藏  举报