顺序表C++

顺序表C++

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10000
#define SUCCESS 0
#define OVERFLOW 0
#define ERROR -1
typedef int ElemType;
typedef int Status;
typedef struct
{
	ElemType *elem;
	int length;
}SqList;
Status InitList(SqList &L);
Status PrintElem(SqList L);
Status InitElem(SqList &L,ElemType e[],int len);
Status GetElem(SqList L,int i,ElemType &e);
int LocateElem(SqList L,ElemType e);
Status ListInsert(SqList &L,int i,ElemType e);
Status ListDelete(SqList &L,int i);
int main(){
	SqList L;
	ElemType elem[8]={4,5,2,1,25,5,5,5},e=1;
	InitList(L);
	InitElem(L,elem,8);
	printf("\ne=%d",e);
	GetElem(L,1,e);
	printf("\ne=%d",e);
	printf("\n1--Locate=%d",LocateElem(L,1));
	ListInsert(L,3,6);
	ListDelete(L,3);
	return 0;
}
Status InitList(SqList &L)
{
	L.elem=new ElemType[MAXSIZE];
	if(!L.elem) exit(OVERFLOW);
	L.length=0;
	printf("init\n");
	return SUCCESS;
}
Status PrintElem(SqList L)
{
	int i;
	printf("\n");
	for(i=0;i<L.length;i++)
		printf("%d ",L.elem[i]);
	return SUCCESS;
}
Status InitElem(SqList &L,ElemType e[],int len)
{
	int i;
	for(i=0;i<len;i++)
		L.elem[i]=e[i];
	L.length=len;
	PrintElem(L);
	return SUCCESS;
}
Status GetElem(SqList L,int i,ElemType &e)
{
	if(i<1||i>L.length) exit(ERROR);
	e=L.elem[i-1];
	return SUCCESS;
}
int LocateElem(SqList L,ElemType e)
{
	int i;
	for(i=0;i<L.length;i++)
		if(L.elem[i]==e) return i+1;
	exit(ERROR);
}
Status ListInsert(SqList &L,int i,ElemType e)
{
	if(i<1||i>L.length) exit(ERROR);
	if(L.length==MAXSIZE) exit(ERROR);
	int j;
	for(j=L.length-1;j>=i-1;j--) L.elem[j+1]=L.elem[j];
	L.elem[i-1]=e;
	++L.length;
	PrintElem(L);
	return SUCCESS;
}
Status ListDelete(SqList &L,int i)
{
	if(i<1||i>L.length) exit(ERROR);
	int j;
	for(j=i;j<=L.length-1;j++) L.elem[j-1]=L.elem[j];
	--L.length;
	PrintElem(L);
	return SUCCESS;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值