Head.H
#define TabSize 100
typedef struct _sstab
{
int elem[TabSize];
int len;
}SSTab, *sstab;
sstab InitSSTABLE(void);
void AssignSSTABLE(sstab Tab);
void PrintSSTABLE(sstab Tab);
int FoldSearchSSTABLE(sstab Tab, int aimval);
Function.C
sstab InitSSTABLE(void)
{
sstab Tab = (sstab)malloc(sizeof(SSTab));
Tab->len = 0;
return Tab;
}
void AssignSSTABLE(sstab Tab)
{
for (int i = 0; i < 91; i++)
{
Tab->elem[i] = i;
}
Tab->len = 91;
}
void PrintSSTABLE(sstab Tab)
{
for (int i = 0; i < Tab->len; i++)
{
printf("%d ", Tab->elem[i]);
}
printf("\n");
}
int FoldSearchSSTABLE(sstab Tab, int aimval)
{
int low, high, mid;
low = 0;
high = Tab->len - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (Tab->elem[mid] == aimval)
{
return mid;
}
else if (Tab->elem[mid] > aimval)
{
high = mid - 1;
}
else if (Tab->elem[mid] < aimval)
{
low = mid + 1;
}
}
return -1;
}
Main.C
void main(void)
{
sstab Tab = InitSSTABLE();
AssignSSTABLE(Tab);
PrintSSTABLE(Tab);
int index = FoldSearchSSTABLE(Tab, 13);
printf("index: %d \n", index);
printf("\n");
system("pause");
}