顺序表查找指定元素

版权声明:请勿商业化使用 https://blog.csdn.net/qq_40991687/article/details/89544130

问题描述:

本题要求实现一个函数,要求从顺序表中查找指定元素,并返回第一个查找成功的元素在表中的位置序号,若查找失败,则返回0;函数接口定义:int LocateElem(SqList L,ElemType e);
其中SqList结构定义如下:typedef struct{
	ElemType *elem;
	int length;
 }SqList;
	裁判测试程序样例:#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
typedef int ElemType;
typedef struct{
	ElemType *elem;
	int length;
 }SqList;
void InitList(SqList &L);/*细节在此不表*/
int LocateElem(SqList L,ElemType e);

int main()
{
	SqList L;
	InitList(L);
	ElemType e;
	int p;
	scanf("%d",&e);
	p=LocateElem(L,e);
	printf("The position of %d in SequenceList L is %d.",e,p);
	return 0;
}

/* 请在这里填写答案 */

问题分析:
按照数组长度遍历就ok啦!
输入:

2 6 4 9 13 -1 2
int LocateElem(SqList L,ElemType e)
{
 for(int i=0;i<L.length;i++)
 if(L.elem[i]==e)
 return i+1;
 return 0;
}

输出:

The position of 2 in SequenceList L is 1.

猜你喜欢

转载自blog.csdn.net/qq_40991687/article/details/89544130