基础数据结构——定长顺序表

线性表

1)顺序表

a. 定长顺序表的实现

首先是其结构体的定义部分:

#define N 10
typedef int ELEMTYPE;

//定长顺序表
typedef struct Sqlist
{
	ELEMTYPE arr[N];
	int length;
}Sqlist,*PSqlist;

首先是其结构体的定义部分:

接下来是简单函数的定义:

//初始化
void Init_sqlist(PSqlist plist)
{
	assert(plist != NULL);
	
	plist->length = 0;
}
//打印
void Print_sqlist(PSqlist plist)
{
	assert(plist != NULL);
	
	for (int i = 0; i < plist->length; i++)
	{
		printf("%d ", plist->arr[i]);
	}
	printf("\n");
}
//按位置插入
bool Insert_pos(PSqlist plist, int pos, int val)
{
	assert(plist != NULL);
	assert(pos >= 0 && pos <= plist->length);

	for (int i = plist->length - 1; i >= pos; i--)
	{
		plist->arr[i + 1] = plist->arr[i];
	}
	plist->arr[pos] = val;
	plist->length++;
	return true;
}
//按值删除
bool Del_val(PSqlist plist, int val)
{
	assert(plist != NULL);
	//是否空
	if (IsEmpty(plist))
	{
		return false;
	}

	int pos = Search_val(plist, val);
	if (pos == -1)	return false;
	else
	{
		Del_pos(plist, pos);
	}
	return true;
}
//按位置删除
bool Del_pos(PSqlist plist, int pos)
{
	assert(plist != NULL);
	assert(pos > 0 && pos <= plist->length);

	for (int i = pos; i < plist->length - 1; i++)
	{
		plist->arr[i] = plist->arr[i + 1];
	}
	plist->length--;
	return true;
}
//查找val的结点
int Search_val(PSqlist plist, int val)
{
	assert(plist != NULL);
	
	for (int i = 0; i < plist->length; i++)
	{
		if (plist->arr[i] == val)
			return i;
	}
	return -1;
}
//判空
bool IsEmpty(PSqlist plist)
{
	assert(plist != NULL);

	if (plist->length == 0)	return true;
	else
	{
		return false;
	}
}
//判满
bool IsFull(PSqlist plist)
{
	assert(plist != NULL);

	if (plist->length == N)	return true;

}
//清空
bool Clear(PSqlist plist)
{
	assert(plist != NULL);

	plist->length = 0;
	return true;
}
//销毁
bool Destory(PSqlist plist)
{
	assert(plist != NULL);

	//Clear(plist);
	free(plist);
	return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值