c语言中线性表_C中的线性搜索

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语言中线性表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值