数据结构——顺序表

顺序表

概念:顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
分类:1.静态顺序表:使用定长数组存储元素
2.动态顺序表:使用动态开辟的内存存储
我们这里主要讲解动态的顺序表增删查改等操作

  • 我们把动态顺序表存放在一个结构体当中
typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* a;		//a指向顺序表的起始位置
	int size;			//size表示顺序表中的有效数据的个数
	int capacity;		//capacity表示顺序表的容量
}SeqList;

判断顺序表是否需要扩容

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

	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}

顺序表的初始化

void SeqListInit(SeqList* ps)
{
	assert(ps);		//断言一下ps是否为空指针
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;

}

顺序表的销毁

void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	if (ps->a != NULL)
	{
		ps->a = NULL;
		ps->size = 0;
		ps->capacity = 0; 
	}
}

顺序表的尾插

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);

	if (ps->size == ps->capacity)		//判断是否需要扩容
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;		// 如果capacity为0,则开辟4个SLDateType类型数据的空间,如果capacity不为0,则将顺序表的容量扩大为2倍。
		SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->capacity = newcapacity;
		ps->a = tmp;
				
	}
	ps->a[ps->size] = x;
	ps->size++;			//增加数据的最后,不要忘记将size++

}

顺序表的头插

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	//还是先判断是否需要扩容
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	//将数据依次向后覆盖
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		end--; 
	}
	ps->a[0] = x;
	ps->size++;		//size不要忘记++
}

顺序表的尾删

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

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

	//暴力检查
	assert(ps->size > 0);
	//直接将size--即可,但是在删除数据时需要注意size要大于0
	ps->size--;
}

顺序表的头删

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	//还是要先断言一下size是否大于0
	assert(ps->size > 0);
	//将数据依次向前覆盖
	for (int i = 1; i < ps->size; i++)
	{
		ps->a[i - 1] = ps->a[i];
	}
	ps->size--;
}

在顺序表下标为pos的位置插入数据

void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	//插入的位置要在顺序表内
	assert(pos >= 0 && pos <= ps->size);
	//判断是否需要扩容
	CheckCapacity(&ps);
	//向后挪动数据
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

在这里插入图片描述

删除顺序表下标为pos的数据

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	//将数据从前向后覆盖
	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

贝壳cc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值