Posted on 2006-06-13 14:16
Enjoy Life 阅读(213)
评论(0) 编辑 收藏 引用 所属分类:
DS study
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 100
typedef int KeyType;
typedef struct {
KeyType *elem;
int length;
}SSTable; //顺序表的存储结构
#define EQ(a, b) ((a) == (b))
#define LT(a, b) ((a) < (b))
#define LQ(a, b) ((a) <= (b))
int Search_Seq(SSTable ST, KeyType key){
int mid,low,high;
low = 1;
high = ST.length;
while(low <= high){
mid = (low + high)/2;
if(EQ(key, ST.elem[mid]))
return mid; //找到数据所在的位置
else if(LT(key, ST.elem[mid]))
high = mid - 1;
else
low = mid + 1;
}
return 0; //没有找到数据
}
void main()
{
int i, key;
SSTable T;
T.elem = (KeyType *)malloc(sizeof(KeyType));
printf("How Many Entries Do You Want input\n");
scanf("%d", &T.length);
for(i=1; i<=T.length; i++){
printf("Please input the %dth entries \n", i);
scanf("%d", &T.elem[i]);
}
for (i=1; i<=T.length; i++)
printf("%5d",T.elem[i]); //显示已经输入的所有数据
printf("\nPlease input the data you want to search\n");
scanf("%d", &key);
i = Search_Seq(T,key);
printf("the search data is locate the %dth(0 indicate can not find)\n",i);
}