A Simple IndexSearch

来源:互联网 发布:手机游戏映射软件 编辑:程序博客网 时间:2024/06/06 00:27

A simple index search,no illustrate,just simple.write it and practise,and feel it

 

 

// IndesSearch.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.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},{10801,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(int key)
{
 int i,start,length;
 int 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;
 stu[start+length]=key;
 indextable[i].length++;
 return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
 int i=IndexSearch(1080201);
 int j=InsertNode(1080204);
 printf("%d,%d",i,j);
 system("pause");
 return 0;
}

 

原创粉丝点击