线性表的顺序存储实现及相关操作 C语言版

 

/*
	线性表的顺序存储实现  C语言版本
	作者:Shmily
	日期:2011年8月30日
	编译环境 VC++6.0
*/
/**************************************************/
#include <stdio.h>
/**************************************************/
#define MaxSize	20						//线性表最大长度
#define	OK		1
#define ERROR	0
#define TRUE	1
#define	FALSE	0
typedef	int		Status;				
typedef int		ElemType;
/**************************************************/
typedef struct {
	ElemType	data[MaxSize+1];		//为了操作方便,规定下标从1开始
	int			length;					//顺序表当前长度
}SqList;
/**************************************************/
//置空表
Status	SetNull(SqList *L)
{
	L->length = 0;
	return OK;
}
/**************************************************/
//在末尾追加
Status append_arr(SqList *L, ElemType val)
{
	if (L->length > MaxSize)
		return FALSE;
	L->data[++L->length] = val;
	return TRUE;
}
/**************************************************/
//求顺序表的长度
int GetLength(SqList *L)
{
	return L->length;
}
/**************************************************/
//输出顺序表
void DisLisy(SqList *L)
{
	int i;
	for (i=1; i<=L->length; ++i)
		printf("%d ", L->data[i]);
}
/**************************************************/
//顺序表判空
Status ListEmpty(SqList *L)
{
	if (0 == L->length)
		return TRUE;
	else
		return FALSE;
}
/**************************************************/
//取得顺序表中第i个元素的值,放入e中
Status GetElem(SqList *L, int i, ElemType *e)
{
	if (i<=0 || i>L->length)
		return ERROR;

	*e = L->data[i];
	return OK;
}
/**************************************************/
//查找顺序表中是否有与e相同的元素
//若有则返回该元素的下标
//否则返回0
Status LocateElem(SqList *L, ElemType e)
{
	int i;
	for (i=1; i<=L->length; ++i)
	{
		if (e == L->data[i])
			return i;
	}
	return FALSE;
}
/**************************************************/
//在顺序表中的第i个位置插入元素e
//若插入成功返回TRUE
//否则返回FALSE
Status ListInset(SqList *L, int i, ElemType e)
{
	int k;

	if ( (i<1 || i>L->length+1) 
		 && (L->length < MaxSize) )
		return FALSE;
	
	if (i<=L->length)
	{
		for (k=L->length; k>=i; --k)
			L->data[k+1] = L->data[k];
	}
	L->data[i] = e;
	++L->length;
	return TRUE;

}
/**************************************************/
//删除顺序表中第i个元素的值
//删除成功返回被删除的元素的值
//删除失败返回FALSE
Status ListDelete(SqList *L, int i, ElemType *e)
{
	int k;
	if (ListEmpty(L))
		return FALSE;
	if (i<1 || i>L->length)
		return FALSE;
	
	*e = L->data[i];
	if (i<L->length)
	{
		for (k=i; k<L->length; ++k)	
			L->data[k] = L->data[k+1];
	}
	--L->length;
	return TRUE;
}
/**************************************************/
int main(void)
{
	SqList L;
	ElemType e;
	SetNull(&L);
	append_arr(&L, 3);
	append_arr(&L, 2);
	append_arr(&L, 6);
	append_arr(&L, 2);
	ListDelete(&L, 2, &e);
	DisLisy(&L);
	printf("\n\n%d", e);
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值