#include
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int elem[MAXSIZE];
int last;
}SeqList;
SeqList Lq;
SeqList* Createselist() {
SeqList* Lq;
Lq = (SeqList*)malloc(MAXSIZE * sizeof(SeqList));
Lq->last = -1;
return Lq;
}
void CreateList(SeqList* Lq)
{
int i;
printf("请输入表长:\n");
scanf("%d", &Lq->last);
for (i = 0;i < Lq->last;i++)
{
printf("请输入表中的第%d个元素", i + 1);
scanf("%d", &Lq->elem[i]);
}
}
int Locate(SeqList* Lq, int e)
{
int i = 0; /*i为扫描计数器,初值为0,即从第一个元素开始比较*/
while ((i <= Lq->last) && (Lq->elem[i] != e))/*顺序扫描表,直到找到值为key的元素, 或扫描到表尾而没找到*/
i++;
if (i <= Lq->last)
return(i + 1); /*若找到值为e的元素,则返回其序号*/
else
return(-1); /*若没找到,则返回空序号*/
}
void main()
{
SeqList* Lq;
int x, i;
Lq = Createselist();
CreateList(Lq);
printf("请输入要查找的数字x:");
scanf("%d", &x);
Locate(Lq, x);
}