线性表、顺序表(数据结构、C语言)

本文介绍了线性表的基本概念,重点讲解了动态顺序表的结构和操作,包括初始化、增容、尾插、尾删、头插、头删、查找、修改、插入和删除等操作,并提供了C语言实现。动态顺序表在空间不足时会自动增容,具有O(1)的尾部操作效率,但头部操作为O(n)。总结了顺序表的优点和问题,指出其适合尾插尾删操作。
摘要由CSDN通过智能技术生成

前言

  • 本篇博客整理了数据结构初阶的线性表以及顺序表的相关知识
  • 知识总结详细,还有丰富的插图帮助理解
  • 代码实现:C语言

1. 线性表

1.1 线性表的相关概念

线性表linear list)是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串……


线性表在逻辑上是线性结构,也就是说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储


2. 顺序表

2.1 顺序表的概念及结构

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


顺序表一般可以分为:

  1. 静态顺序表:使用定长数组存储元素。
    > ![在这里插入图片描述](https://img-blog.csdnimg.cn/4b20a49292ed4822bd59a4e58b237dde.png
    静态顺序表 – N太小,可能不够用,N太大,可能浪费空间
  2. 动态顺序表:使用动态开辟的数组存储。
    在这里插入图片描述
    动态顺序表按需申请内存,空间不够则增容
2.2 顺序表接口函数

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

下面实现动态顺序表的增删查改

typedef int SLDataType;
// 顺序表的动态存储
typedef struct SeqList
{
	SLDateType* a;//指向动态开辟的数组
	int size;	  //有效数据个数
	int capacity; //容量大小
}SL;
// 基本增删查改接口

// 顺序表初始化
void SLInit(SL* ps);

// 检查空间,如果满了,进行增容
void SLCheckCapacity(SL* ps);

// 顺序表尾插	O(1)
void SLPushBack(SL* ps, SLDateType x);

// 顺序表尾删	O(1)
void SLPopBack(SL* ps);

// 顺序表头插	O(n)
void SLPushFront(SL* ps, SLDateType x);

// 顺序表头删	O(n)
void SLPopFront(SL* ps);

// 顺序表查找
int SLFind(SL* ps, SLDateType x);

// 顺序表修改pos位置的值
void SLModify(SL* ps, int pos, SLDateType x);

// 顺序表在pos位置插入x
void SLInsert(SL* ps, int pos, SLDateType x);

// 顺序表删除pos位置的值
void SLErase(SL* ps, int pos);

// 顺序表销毁
void SLDestroy(SL* ps);

// 顺序表打印
void SLPrint(SL* ps);

下面逐个实现这些函数

2.2.1 顺序表初始化
// 顺序表初始化
void SLInit(SL* ps)
{
	assert(ps != NULL);

	ps->a = NULL;
	ps->size = ps->capacity = 0;
}
2.2.2 检查空间,如果满了,进行增容
void SLCheckCapacity(SL* ps)
{
	assert(ps != NULL);

	//检查容量空间,满了扩容
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, newCapacity * sizeof(SLDateType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);//结束整个程序
			//return;//结束该函数
		}

		ps->a = tmp;
		ps->capacity = newCapacity;
	}
}
2.2.3 顺序表尾插 O(1)
  1. 第一种写法
void SLPushBack(SL* ps, SLDateType x)
{
	assert(ps != NULL);
	
	SLCheckCapacity(ps);
	
	ps->a[ps->size] = x;
	ps->size++;
}
  1. 第二种写法(推荐✔✔✔)
void SLPushBack(SL* ps, SLDateType x)
{
	//复用
	SLInsert(ps, ps->size, x);
}
2.2.4 顺序表尾删 O(1)
  1. 第一种写法
void SLPopBack(SL* ps)
{
	//温柔检查
	//if (ps->size == 0)
	//{
	//	printf("SeqList is empty\n");
	//	return;
	//}
	
	//暴力检查
	assert(ps != NULL);
	assert(ps->size > 0);
	
	ps->size--;
}
  1. 第二种写法(推荐✔✔✔)
void SLPopBack(SL* ps)
{
	//复用
	SLErase(ps, ps->size - 1);
}
2.2.5 顺序表头插 O(n)
  1. 第一种写法
void SLPushFront(SL* ps, SLDateType x)
{
	assert(ps != NULL);
	
	SLCheckCapacity(ps);
	
	//挪动数据
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	
	ps->a[0] = x;
	ps->size++;
}
  1. 第二种写法(推荐✔✔✔)
void SLPushFront(SL* ps, SLDateType x)
{
	//复用
	SLInsert(ps, 0, x);
}
2.2.6 顺序表头删 O(n)
  1. 第一种写法
void SLPopFront(SL* ps)
{
	assert(ps != NULL);
	assert(ps->size > 0);
	
	int begin = 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	
	ps->size--;
}
  1. 第二种写法(推荐✔✔✔)
void SLPopFront(SL* ps)
{
	//复用
	SLErase(ps, 0);
}
2.2.7 顺序表查找
int SLFind(SL* ps, SLDateType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}

	return -1;
}
2.2.8 顺序表修改pos位置的值
void SLModify(SL* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

	ps->a[pos] = x;
}
2.2.9 顺序表在pos位置插入x(核心代码)
void SLInsert(SL* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);

	SLCheckCapacity(ps);

	//挪动数据
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}
2.2.10 顺序表删除pos位置的值(核心代码)
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

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

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

	ps->size--;
}
2.2.11 顺序表销毁
void SLDestroy(SL* ps)
{
	assert(ps != NULL);

	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
		ps->size = ps->capacity = 0;
	}
}
2.2.12 顺序表打印
void SLPrint(SL* ps)
{
	assert(ps != NULL);

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

2.3 顺序表总结

顺序表的优缺点及问题

  1. 顺序表的尾插尾删时间复杂度是O(1),头插头删时间复杂度是O(n),所以顺序表适合尾插尾删
  2. 写一个顺序表可以先写Insert和Erase两个接口函数,头插头删、尾插尾删可以直接复用
  3. 中间/头部插入删除,时间复杂度为O(n)
  4. 增容需要申请新的空间,拷贝数据,释放旧的空间。会有不小的消耗
  5. 增容一般是呈2倍增长,势必会有一定的空间浪费

想要解决上面的问题,我们需要学习链表


学习记录:

  • 本篇博客整理于2022.6.21
  • 如果有错误的地方请多多指教👀
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

如何写出最优雅的代码

感谢支持,我将继续努力!

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

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

打赏作者

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

抵扣说明:

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

余额充值