//算法功能: 分别按序号和按内容在顺序表中查找元素
//算法思路:
//GetElem(SqList &L, int i)查找顺序表L中第i个数据元素,直接在表中定位,并返回L.elem[i-1]
//LocateElem(SqList &L, ElemType e)查找顺序表L中与给定值e相等的数据元素,若找到
//与e相等的第1个元素则返回该元素在顺序表中的序号;否则查找失败返回0
#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
//定义顺序表存储结构
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
//初始化顺序表
Status InitList_Sq(SqList &L)
{
L.elem = (ElemType*) malloc (LIST_INIT_SIZE * sizeof(ElemType));
if(!L.elem)
exit(ERROR);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
//创建顺序表
void Create_SqList(SqList &L)
{
int c, i = 0;
int *newBase;
printf("请输入顺序表元素,按Ctrl+Z结束:\n");
while(scanf("%d", &c) != EOF)
{
if(i >= L.listsize)
{
newBase = (ElemType *) realloc (L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
//为初始顺序表以LISTINCREMENT大小重新增加存储空间
if(!newBase)
exit(OVERFLOW);
L.elem = newBase;
L.listsize += LISTINCREMENT;
}
L.elem[i++] = c;
}
L.length = i;
printf("输入的顺序表元素是:\n");
for(i = 0; i < L.length; i++) //输出新建顺序表
printf("%d ", L.elem[i]);
printf("\n");
}
//返回指定位置的元素
ElemType GetElem(SqList &L, int i)
{
ElemType *e;
if(!L.elem || i > L.length || i < 1)
exit (ERROR);
e = L.elem+i-1;
return *e;
}
//定位指定元素,如果有,返回第一个匹配的元素的位置
int LocateElem(SqList &L, ElemType e)
{
int i;
if(!L.elem)
exit (ERROR);
for(i = 0; i < L.length; i++)
if(e == L.elem[i])
return i+1;
return 0;
}
int main()
{
SqList L;
int location, element;
if(!InitList_Sq(L))
{
printf("初始化顺序表失败!\n");
exit(ERROR);
}
Create_SqList(L);
//按序号查找
printf("输入查找的位置:");
scanf("%d", &location);
while(location > L.length+1 || location < 1)
{
printf("输入位置错误,请重新输入!\n");
scanf("%d", &location);
}
printf("第%d个元素是: %d\n", location, GetElem(L, location));
//按内容查找
printf("输入查找的元素:");
scanf("%d", &element);
if(!LocateElem(L, element))
printf("该顺序表中没有%d这个元素。\n", element);
else
printf("%d在顺序表中是第%d个元素\n", element, LocateElem(L, element));
return 0;
}
//算法思路:
//GetElem(SqList &L, int i)查找顺序表L中第i个数据元素,直接在表中定位,并返回L.elem[i-1]
//LocateElem(SqList &L, ElemType e)查找顺序表L中与给定值e相等的数据元素,若找到
//与e相等的第1个元素则返回该元素在顺序表中的序号;否则查找失败返回0
#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
//定义顺序表存储结构
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
//初始化顺序表
Status InitList_Sq(SqList &L)
{
L.elem = (ElemType*) malloc (LIST_INIT_SIZE * sizeof(ElemType));
if(!L.elem)
exit(ERROR);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
//创建顺序表
void Create_SqList(SqList &L)
{
int c, i = 0;
int *newBase;
printf("请输入顺序表元素,按Ctrl+Z结束:\n");
while(scanf("%d", &c) != EOF)
{
if(i >= L.listsize)
{
newBase = (ElemType *) realloc (L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
//为初始顺序表以LISTINCREMENT大小重新增加存储空间
if(!newBase)
exit(OVERFLOW);
L.elem = newBase;
L.listsize += LISTINCREMENT;
}
L.elem[i++] = c;
}
L.length = i;
printf("输入的顺序表元素是:\n");
for(i = 0; i < L.length; i++) //输出新建顺序表
printf("%d ", L.elem[i]);
printf("\n");
}
//返回指定位置的元素
ElemType GetElem(SqList &L, int i)
{
ElemType *e;
if(!L.elem || i > L.length || i < 1)
exit (ERROR);
e = L.elem+i-1;
return *e;
}
//定位指定元素,如果有,返回第一个匹配的元素的位置
int LocateElem(SqList &L, ElemType e)
{
int i;
if(!L.elem)
exit (ERROR);
for(i = 0; i < L.length; i++)
if(e == L.elem[i])
return i+1;
return 0;
}
int main()
{
SqList L;
int location, element;
if(!InitList_Sq(L))
{
printf("初始化顺序表失败!\n");
exit(ERROR);
}
Create_SqList(L);
//按序号查找
printf("输入查找的位置:");
scanf("%d", &location);
while(location > L.length+1 || location < 1)
{
printf("输入位置错误,请重新输入!\n");
scanf("%d", &location);
}
printf("第%d个元素是: %d\n", location, GetElem(L, location));
//按内容查找
printf("输入查找的元素:");
scanf("%d", &element);
if(!LocateElem(L, element))
printf("该顺序表中没有%d这个元素。\n", element);
else
printf("%d在顺序表中是第%d个元素\n", element, LocateElem(L, element));
return 0;
}