顺序表查找-C语言实现
时间:2010-11-06 来源:shinaimiao
#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));
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("%3d",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-1;i++)
{
if(e==L->elem[i])
return i+1;
}
return 0;
}
void main ()
{
SqList L;
int location,element;
if(!InitList_Sq(&L))
{
printf("E001\n");
exit(ERROR);
}
Create_SqList(&L);
printf("请输入查找的位置:\n");
scanf("%d",&location);
while(location>L.length || 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));
}
#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));
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("%3d",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-1;i++)
{
if(e==L->elem[i])
return i+1;
}
return 0;
}
void main ()
{
SqList L;
int location,element;
if(!InitList_Sq(&L))
{
printf("E001\n");
exit(ERROR);
}
Create_SqList(&L);
printf("请输入查找的位置:\n");
scanf("%d",&location);
while(location>L.length || 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));
}
相关阅读 更多 +