当要实现顺序表的查询的时候,查询的函数就多了一个形参,此形参用来传递查询的数据,如果查询到要查找的数据就返回1,否则返回0来判断。
#include <stdio.h>
#define N 10
int FindElement(int *a, int len, int x) //顺序表的查询
{
for(int i=0; i<len; i++)
{
if(a[i]==x)
return 1;
}
return 0;
}
void main()
{
int a[N] = {1, 9, 2, 3, 4, 8, 7, 0, 5, 6};
int x;
printf("请输入你要查询的数据.\n");
scanf("%d",&x);
int result = FindElement(a,N,x);
if(result==1)
{
printf("你要查询的数据存在.\n");
}
else
{
printf("你要查询的数据不存在.\n");
}
}