【数据结构】C语言实现基本顺序表——增删查改

目录

1.1概念及结构

1.2接口实现

初始化:void SLPrint(SL* psl)

打印:void SLDestroy(SL* psl)

销毁:void SLDestroy(SL* psl)

检查容量:void SLCheckCapacity(SL* psl)

尾插:void SLPushBack(SL* psl, SLDataType x)

头插:void SLPushFront(SL* psl, SLDataType x)

尾删:void SLPopBack(SL* psl)

头删:void SLPopFront(SL* psl)

任意位置插入:void SLInsert(SL* psl, int pos, SLDataType x)

任意位置删除:void SLErase(SL* psl, int pos)


1.1概念及结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存 储。在数组上完成数据的增删查改。

顺序表可以分为两种:

1:静态顺序表:使用定长数组去存储元素。

2:动态顺序表:使用动态开辟的数组存储。

今天我们主要了解动态顺序表。

1.2接口实现

静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空 间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间 大小,所以下面我们实现动态顺序表。

typedef int SLDataType;
 // 顺序表的动态存储
typedef struct SeqList
 {
 SLDataType* array;  // 指向动态开辟的数组
size_t size ;       // 有效数据个数
size_t capicity ;   //容量空间的大小
}SeqList;           // 基本增删查改接口
// 顺序表初始化
void SeqListInit(SeqList* psl);
 // 检查空间,如果满了,进行增容
void CheckCapacity(SeqList* psl);
 // 顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x);
 // 顺序表尾删
void SeqListPopBack(SeqList* psl);
 // 顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x);
 // 顺序表头删
void SeqListPopFront(SeqList* psl);
 // 顺序表查找
int SeqListFind(SeqList* psl, SLDataType x); 
// 顺序表在pos位置插入x
 void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
 // 顺序表删除pos位置的值
void SeqListErase(SeqList* psl, size_t pos);
 // 顺序表销毁
void SeqListDestory(SeqList* psl);
 // 顺序表打印
void SeqListPrint(SeqList* psl);

我们先声明这些函数名字,然后再挨个去实现并检查。

为了便于检查,我们先对顺序表的初始化,打印,和销毁函数进行实现。

初始化:void SLPrint(SL* psl)
void SLPrint(SL* psl)
{
	assert(psl);

	for (int i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->a[i]);
	}
	printf("\n");
}
打印:void SLDestroy(SL* psl)
void SLDestroy(SL* psl)
{
	assert(psl);

	if (psl->a != NULL)
	{
		free(psl->a);
		psl->a = NULL;
		psl->size = 0;
		psl->capacity = 0;
	}
}
销毁:void SLDestroy(SL* psl)
void SLDestroy(SL* psl)
{
	assert(psl);

	if (psl->a != NULL)
	{
		free(psl->a);
		psl->a = NULL;
		psl->size = 0;
		psl->capacity = 0;
	}
}

然后我们就可以实现增删查改了,为了保证函数的正确性,建议写一个函数就自行测试以下。

在这里,我们还需要写一个函数去检查空间的容量是否够用:

检查容量:void SLCheckCapacity(SL* psl)
void SLCheckCapacity(SL* psl)
{
	assert(psl);
	if (psl->size == psl->capacity)
	{
		int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(psl->a, sizeof(SLDataType)*newCapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		psl->a = tmp;
		psl->capacity = newCapacity;
	}
}
尾插:void SLPushBack(SL* psl, SLDataType x)
void SLPushBack(SL* psl, SLDataType x)
{
	assert(psl);

	SLCheckCapacity(psl);

	psl->a[psl->size] = x;
	psl->size++;
}
头插:void SLPushFront(SL* psl, SLDataType x)
void SLPushFront(SL* psl, SLDataType x)
{
	assert(psl);
	SLCheckCapacity(psl);// 挪动数据
	int end = psl->size - 1;
	while (end >= 0)
	{
		psl->a[end + 1] = psl->a[end];// 挪动数据
		--end;
	}
	psl->a[0] = x;
	psl->size++;
}
尾删:void SLPopBack(SL* psl)
void SLPopBack(SL* psl)
{
	assert(psl);

	// 空
	// 温柔的检查
	/*if (psl->size == 0)
	{
		return;
	}*/

	// 暴力检查
	assert(psl->size > 0);

	//psl->a[psl->size - 1] = -1;
	psl->size--;
}
头删:void SLPopFront(SL* psl)
void SLPopFront(SL* psl)
{
	assert(psl);

	// 暴力检查
	assert(psl->size > 0);

	int begin = 1;
	while (begin < psl->size)
	{
		psl->a[begin - 1] = psl->a[begin];
		++begin;
	}

	psl->size--;
}
任意位置插入:void SLInsert(SL* psl, int pos, SLDataType x)
void SLInsert(SL* psl, int pos, SLDataType x)
{
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);

	SLCheckCapacity(psl);
	// 挪动数据
	int end = psl->size - 1;
	while (end >= pos)
	{
		psl->a[end + 1] = psl->a[end];
		--end;
	}
	psl->a[pos] = x;
	psl->size++;
}
任意位置删除:void SLErase(SL* psl, int pos)

void SLErase(SL* psl, int pos)
{
	assert(psl);
	assert(pos >= 0 && pos < psl->size);

	// 挪动覆盖
	int begin = pos + 1;
	while (begin < psl->size)
	{
		psl->a[begin - 1] = psl->a[begin];
		++begin;
	}

	psl->size--;
}

简单顺序表就这样大概完成了。如有问题,谢谢大佬们指点!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何陈陈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值