/*顺序查找的基本运算与实现*/
#include <stdio.h>
#include <malloc.h>
typedef int keytype;
typedef struct
{
keytype num;
char name[10];
}datatype;
typedef struct
{
datatype *data;
int length;
}S_TBL;
int S_Search(S_TBL *tbl,keytype kx);
void main()
{
int i,flag;
keytype kx;
S_TBL *tbl=(S_TBL *)malloc(sizeof(S_TBL));
printf("please input table length:/n");
scanf("%d",&(tbl->length));
tbl->data=(datatype *)calloc( tbl->length, sizeof(datatype) );
printf("/nplease input element:/n");
for(i=1;i<=tbl->length;i++)
{
printf("please input num:/n");
scanf("%d",&((tbl->data[i]).num));
printf("please input name:/n");
scanf("%s",(tbl->data[i]).name);
}
printf("/nplease input a value that you want to find:/n");
scanf("%d",&kx);
flag=S_Search(tbl,kx);
if(flag==0)
{
printf("search failed!/n");
}
else
{
printf("num=%6d/tname=%s/n",tbl->data[flag].num,tbl->data[flag].name);
}
}
int S_Search(S_TBL *tbl,keytype kx)
{
int i;
tbl->data[0].num=kx;
for(i=tbl->length;tbl->data[i].num!=kx;i--)
{
;
}
return i;
}
/*顺序查找的基本运算与实现*/
最新推荐文章于 2024-05-20 17:18:14 发布