【数据结构初阶】顺序表_增删查改、查询、打印、销毁 (附源码+realloc深解)

源码下载地址

线性表

  • 线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
  • 线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

在这里插入图片描述

顺序表

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

顺序表一般可以分为:

  • 静态顺序表:使用定长数组存储元素
# define N 7
typedef int SLDataType;

typedef struct SeqList
{
	SLDataType arr[N];  // 定长数组
	size_t size;  // 有效数据的个数
}SeqList;

在这里插入图片描述

  • 动态顺序表:使用动态开辟的数组存储。
typedef int SLDataType;

// 动态顺序表
typedef struct SeqList
{
	SLDataType* a;  // 指向动态开辟的数组
	int size;  // 有效数据个数
	int capacity; // 容量空间的大小
}SeqList;

在这里插入图片描述

接口实现

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

// 对数据的管理 增删查改
void SeqListInit(SeqList* ps);
void SeqListDestroy(SeqList* ps);

void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDataType x);
void SeqListPushFront(SeqList* ps, SLDataType x);
void SeqListPopBack(SeqList* ps);
void SeqListPopFront(SeqList* ps);

// 顺序表查找 没有返回-1
int SeqListFind(SeqList* ps, SLDataType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDataType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);

顺序表初始化

  1. 判断指针是否为空
  2. 动态顺序表空间置0
void SeqListInit(SeqList* ps) {
	assert(ps);

	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

顺序表销毁

  1. 释放堆空间
  2. 指向数组的指针指向空
  3. 当前有效数字个数和空间容量置为0
void SeqListDestroy(SeqList* ps) {
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

顺序表打印

遍历思想

void SeqListPrint(SeqList* ps) {
	assert(ps);

	for (int i = 0; i < ps->size; ++i) {
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

扩容

在和插入元素有关的函数功能中,要求检查顺序表容量是否足够。如果顺序表空间不够,则需扩容。

void SeqListCheckCapacity(SeqList* ps) {
	if (ps->size == ps->capacity)
	{
		// 空间不够,扩容
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		if (tmp == NULL) 
		{
			printf("realloc fail!\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}

补充:realloc的用法

在这里插入图片描述

尾插

  1. 检查空间是否足够,不够扩容
  2. 增加数据
  3. 当前有效数据数加一
void SeqListPushBack(SeqList* ps, SLDataType x) {
	// 检查空间是否足够,不够扩容
	SeqListCheckCapacity(ps);

	ps->a[ps->size] = x;
	ps->size++;
}

头插

  1. 检查空间是否足够,不够扩容
  2. 挪动数据
  3. 添加数据
void SeqListPushFront(SeqList* ps, SLDataType x) {
	SeqListCheckCapacity(ps);

	int end = ps->size - 1;
	while (end >= 0) 
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[0] = x;
	ps->size++;
}

尾插

  1. 判断是否为空数组
  2. 有效数据数减一
void SeqListPopBack(SeqList* ps) {
	assert(ps->size > 0);
	ps->size--;
}

头删

  1. 空顺序表判断
  2. 挪动数据
  3. 有效数据数减一
void SeqListPopFront(SeqList* ps) {
	// 空顺序表判断
	assert(ps->size > 0);

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

	ps->size--;
}

顺序表查找

没有找到返回-1, 找到x位置返回下标。

int SeqListFind(SeqList* ps, SLDataType x) {
	for (int i = 0; i < ps->size; ++i) {
		if (x == ps->a[i])
			return i;  
	}
	return -1;
}

在指定pos下标插入x

  1. pos下标位置合法性判断
  2. 检查空间容量
  3. 挪动数据
void SeqListInsert(SeqList* ps, int pos, SLDataType x) {
	assert(pos >= 0 && pos <= ps->size);

	SeqListCheckCapacity(ps);

	// 挪动数据
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

删除pos下标位置的值

  1. pos下标位置合法性判断
  2. 挪动数据
void SeqListErase(SeqList* ps, int pos) {
	assert(pos >= 0 && pos < ps->size);

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

	ps->size--;
}

顺序表缺陷

  1. 空间不够了需要增容,增容是需要代价的。
    realloc分为原地扩容和异地扩容(高代价)。

原地扩容:
在这里插入图片描述
异地扩容:
在这里插入图片描述

  1. 避免频繁扩容,扩容模糊2倍,会有空间浪费
  2. 顺序表要求数据从开始位置连续存储,那么在头部或者中间增删数据,效率不高。

相关OJ

27. 移除元素
26. 删除有序数组中的重复项
88. 合并两个有序数组

  • 19
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 21
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JoyCheung-

赏颗糖吃吧~~~

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

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

打赏作者

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

抵扣说明:

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

余额充值