c语言中线性表
Here you will find program for linear search in C.
在这里,您将找到用C进行线性搜索的程序。
Linear search is the simplest searching algorithm which is sometimes known as sequential search. In this algorithm each element of array is compared with the targeted element sequentially.
线性搜索是最简单的搜索算法,有时也称为顺序搜索。 在该算法中,将数组的每个元素依次与目标元素进行比较。
C中的线性搜索 (Linear Search in C)
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
Output
输出量
How many elements?4 Enter array elements: 6 8 9 1
多少个元素?4 输入数组元素: 6 8 9 1
Enter element to search:9 Element found at index 2
输入要搜索的 元素 :9 在索引2处找到元素
翻译自: https://www.thecrazyprogrammer.com/2013/07/c-program-to-search-element-in-1d-array.html
c语言中线性表