索引的创建,查找,插入
索引优点
提高数据查询的速度
缺点
需要占用一定磁盘空间,另外减慢了数据的插入和删除速度。
#include<stdio.h>
#define INDEXTABLE_LEN 3
#define TABLE_LEN 30
typedef struct item
{
int index;
int start;
int length;
}INDEXITEM;
long stu[TABLE_LEN] = {1080101,1080102,1080103,1080104,1080105,1080106,0,0,0,0,
1080201,1080202,1080203,1080204,0,0,0,0,0,0,
1080301,1080302,1080303,1080304,0,0,0,0,0,0};
INDEXITEM indextable[INDEXTABLE_LEN] = {
{10801,0,6},
{10802,10,4},
{10803,20,4}
};
int IndexSearch(int key)
{
int i,index1,start,length;
index1 = key /100;
for(i=0;i<INDEXTABLE_LEN;i++)
{
if(indextable[i].index == index1)
{
start = indextable[i].start;
length = indextable[i].length;
break;
}
}
if(i >= INDEXTABLE_LEN)
return -1;
for(i=start;i<start+length;i++)
{
if(stu[i] == key)
return i;
}
return -1;
}
int InsertNode(key)
{
int i,index1,start,length;
index1 = key /100;
for(i=0;i<INDEXTABLE_LEN;i++)
{
if(indextable[i].index == index1)
{
start = indextable[i].start;
length = indextable[i].length;
break;
}
}
for(i=0;i<INDEXTABLE_LEN;i++)
{
if(indextable[i].index == index1)
{
start = indextable[i].start;
length = indextable[i].length;
break;
}
}
if(i >= INDEXTABLE_LEN)
return -1;
stu[start+length] = key;
indextable[i].length++;
return 0;
}
int main()
{
long key;
int i,pos;
printf("source data:");
for(i=0;i<TABLE_LEN;i++)
{
printf("%ld ",stu[i]);
}
printf("\n");
printf("input search word:");
scanf("%ld",&key);
pos = IndexSearch(key);
if(pos>0)
printf("search success---%d\n",pos);
else
printf("search failed\n");
printf("input insert word");
scanf("%ld",&key);
if(InsertNode(key) == -1)
printf("insert faild\n");
else
{
for(i=0;i<TABLE_LEN;i++)
printf("%ld ",stu[i]);
printf("\n");
}
return 0;
}