【数据结构(C语言描述)】线性表之顺序表


一、线性表

线性表(linear list):是n个具有相同特性的数据元素的有限序列。
常见的线性表:顺序表、链表、栈、队列、字符串…

🔵特点
    逻辑上:线性结构
    物理上:不一定是连续(一般以数组和链式结构的形式存储)

在这里插入图片描述

二、顺序表

    2.1 概念和分类

顺序表:是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储

顺序表可以分为两种:

  • 静态顺序表:使用定长数组存储元素
    在这里插入图片描述
  • 动态顺序表:使用动态开辟的数组存储
    在这里插入图片描述

    2.2 接口实现

动态顺序表是静态顺序表的升级版,这里我们的接口实现考虑动态顺序表:

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

       1.初始化

// 顺序表初始化
void SeqListInit(SeqList* psl)
{
	psl =NULL;
	psl->capicity = 0;
	psl->size = 0;
}

       2.扩容

// 检查空间,如果满了,进行增容
void CheckCapacity(SeqList* psl)
{
	//新增的空间大小
	//考虑两种情况:①啥也没②有东西但满
	int newcapacity = psl->capicity == 0 ? 4 : psl->capicity * 2;//*2是为了避免不断开辟空间
	//新空间的指针
	SLDataType* tmp = (SLDataType*)realloc(psl->array, sizeof(SLDataType) * newcapacity);
	if (tmp == NULL)
	{
		printf("fail\n");
		exit(-1);
	}
	//新空间指针给旧空间,原始数据迁移旧空间销毁
	psl->array = tmp;
	//容量变成新的数值
	psl->capicity = newcapacity;
}

       3.销毁顺序表

// 顺序表销毁
void SeqListDestory(SeqList* psl)
{
	free(psl->array);
	psl->array = NULL;
	psl->size = psl->capicity = 0;
}

       4.打印数据

// 顺序表打印
void SeqListPrint(SeqList* psl)
{
	for (int i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->array[i]);
	}
}

       5.尾插

// 顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x)
{
	if (psl->capicity == psl->size)
	{
		CheckCapacity(psl);
	}
	psl->array[psl->size] = x;
	psl->size++;
}

       6.尾删

// 顺序表尾删
void SeqListPopBack(SeqList* psl)
{
	if (psl->size == 0)
	{
		printf("empty!fail!\n");
		exit(-1);
	}
	//暴力点
	//assert(ps->size>0);
	psl->array[psl->size-1] = 0;//这条只是初始化数据实际没啥用可以屏蔽
	//|0 1 2 3 4 5|
	//size=6,要删除最后一个数据需要-1
	psl->size--;
}

       7.头插

// 顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x)
{
	if (psl->size == psl->capicity)
	{
		CheckCapacity(psl);
	}
	for (int i = psl->size; i > 0; i--)
	{
		psl->array[i] = psl->array[i - 1];
	}
	psl->array[0] = x;
	psl->size++;
}

       8.头删

// 顺序表头删
void SeqListPopFront(SeqList* psl)
{
	if (psl->size == 0)
	{
		printf("empty!fail!");
		exit(-1);
	}
	//直接覆盖前一个数据
	for (int i = 1; i < psl->size - 1; i++)
	{
		psl->array[i - 1] = psl->array[i];
	}
	psl->size--;
}

       9.某一位置增

在这里插入图片描述

// 顺序表在pos位置插入x
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
{
	if (psl->size == psl->capicity)
	{
		CheckCapacity(psl);
	}
	for (int i = psl->size; i > pos-1; i--)
	{
		psl->array[i] = psl->array[i - 1];
	}
	psl->array[pos-1] = x;
	psl->size++;
}

       10.某一位置删

// 顺序表删除pos位置的值
void SeqListErase(SeqList* psl, size_t pos)
{
	//pos的值有误
	if (pos<-1 || (pos-1)>(psl->size - 1))
	{
		printf("fail!");
		exit(-1);
	}
	//表是空的
	if (psl->size == 0)
	{
		printf("empty!fail!");
		exit(-1);
	}
	for (int i = pos - 1; i < psl->size - 2; i++)
	{
		psl->array[i] = psl->array[i + 1];
	}
	psl->size--;
}

       11.找

// 顺序表查找
int SeqListFind(SeqList* psl, SLDataType x)
{
	for (int i = 0; i < psl->size - 1; i++)
	{
		if (x == psl->array[i])
			return i;
	}
	return -1;
}

    2.3 顺序表缺点

  • 中间/头部的插入删除,时间复杂度为 O ( N ) O(N) O(N)
  • 增容一般是呈2倍的增长,势必会有一定的空间浪费。
  • 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

XiYang-DING

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

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

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

打赏作者

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

抵扣说明:

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

余额充值