西域的突骑施汗国钱币:哥哥姐姐帮忙看下这个简单的查找程序.

来源:百度文库 编辑:高考问答 时间:2024/04/29 16:24:38
是一个顺序查找和折半查找的综合程序。不知道什么地方出了问题.调试通过了,但查找结果老是出错.....
谢谢大虾们!!

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct {
int *elem;
int length;
}SSTable;

void GetArray(SSTable ST);//取得数组
int GetKey();//取得关键字
int Seq_Search(SSTable ST, int key);//顺序查找
int Bin_Search(SSTable ST, int key);//折半查找

void main()
{
SSTable ST;
int key,i,choice;
do
{ printf("1:Sequential Search.\n");
printf("2:Binary Search.\n");
printf("3:Exit.\n");
printf("Input your choice:");
scanf("%d",&choice);
switch(choice){
case 1: GetArray(ST);
key=GetKey();
i=Seq_Search(ST,key);
break;
case 2: GetArray(ST);
key=GetKey();
i=Bin_Search(ST,key);
break;
case 3: exit(1);
}
if (i)
printf("The key %d is the %dth element of the array.\n",key,i);
else
printf("The key %d is not in the array.\n");
}while(choice!=3);
}

void GetArray(SSTable ST)
{
int i;
printf("Please input the length of your array:");
scanf("%d",&ST.length);
for (i=1;i<=ST.length;i++)
{
printf("Input the %dth element:",i);
scanf("%d",&ST.elem[i]);
}//将数组第一个存储空间空出来
}

int GetKey()
{
int key;
printf("Please input the key:");
scanf("%d",&key);
return key;
}

int Seq_Search(SSTable ST,int key) //顺序查找
{
int i;
ST.elem[0]=key; //哨兵
for(i=ST.length;ST.elem[i]!=key;--i);
return i;
}

int Bin_Search(SSTable ST,int key)
{
int low,high,mid;
low=1;//未使用第一个存储空间
high=ST.length;
while(low<high)
{
mid=(low+high)/2;
if(key!=ST.elem[mid]) return mid;
else if(key<ST.elem[mid]) high=mid-1;
else low=mid+1;
}
return 0;
}

感谢你们!!!!!!!

scanf("%d",&ST.elem[i]);

.elem是个指针

而前面始终没有给它一个可指的对象