顺序表(动态)

上篇博客介绍了静态顺序表的增删查改以及排序,我们知道了:

静态顺序表的长度是固定的,但是如果我们在使用顺序表是并不知道顺序表的长度,或者在使用过程中,顺序表的长度不够了,难道我们要重新再创建一个新的顺序表吗?这样显然是很麻烦的。所以本篇博客将讨论动态顺序表。
那么动态顺序表和静态顺序表又有什么区别呢?
上面我们提到了静态顺序表的局限性,那么动态顺序表很有效的解决了静态的不足之处,当在使用顺序表过程中长度不够用时,动态顺序表可以自己扩容,这样就不会存在长度不够用的情况了。在插入时亦要考虑是否需要扩容
#### 动态顺序表的定义:
//动态顺序表的定义
typedef int DataType;
typedef struct SeqList
{
	DataType *data;//数据区 
	int sz;//有效个数 
	int capacity;//容量 
}SeqList, *pSeqList;

初始化
void InitSeqList(pSeqList ps)
{
	const int init_capacity = 5;
	assert(ps != NULL);
	ps->sz = 0;
	ps->capacity = init_capacity;//初始容量固定
	ps->data = (DataType*)malloc(sizeof(DataType)*init_capacity);
	assert(ps->data != NULL);
}

销毁顺序表
void DestroySeqList(pSeqList ps)
{
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->sz = 0;
	ps->capacity = 0;
}
判断是否需要扩容
void ExpandIfRequired(pSeqList ps)
{
	assert(ps);
	//不需要扩容
	if (ps->sz < ps->capacity)
	{
		return;
	}
	//容量不够,需要扩容
	int newcapacity = ps->capacity + Increase;
	DataType *newdata = (DataType*)malloc(sizeof(DataType)*newcapacity);
	assert(newdata);
	printf("扩容成功\n");
	//将老数据移到新数据里面
	for (int i = 0; i < ps->sz; i++)
	{
		newdata[i] = ps->data[i];
	}
	//释放老数组
	free(ps->data);
	//新数组放到顺序表结构里
	ps->data = newdata;
	//更新capacity
	ps->capacity = newcapacity;
}
打印顺序表
void PrintSeqList(const pSeqList ps)
{//打印顺序表
	int i = 0;
	assert(ps != NULL);
	if (ps->sz == 0)
	{
		printf("顺序表为空\n");
		return;
	}
	else
	{
		for (i = 0; i < ps->sz; i++)
		{
			printf("%d ", ps->data[i]);
		}
		printf("\n");
	}
}
增->尾插法
void PushBack(pSeqList ps, DataType d)
{//尾插
	assert(ps != NULL);
	//这里要考虑扩容
	ExpandIfRequired(ps);
	ps->data[ps->sz++] = d;
}

测试

void testPushBack()
{
	PushBack(&Seq, 1);
	PushBack(&Seq, 2);
	PushBack(&Seq, 3);
	PushBack(&Seq, 4);
	PushBack(&Seq, 5);
	PushBack(&Seq, 6);
	PrintSeqList(&Seq);
	//PopBack(&Seq);
	//PrintSeqList(&Seq);
	//PopFront(&Seq);
	//PrintSeqList(&Seq);
	/*Insert(&Seq, 2, 9);
	PrintSeqList(&Seq);*/
	//int i=Find(&Seq, 3);
	//printf("%d\n", i);
	//Erase(&Seq, 3);
	//PrintSeqList(&Seq);
	//Remove(&Seq, 2);
	//PrintSeqList(&Seq);
	//Sort(&Seq);
	//PrintSeqList(&Seq);
	/*int j=BinarySearch(&Seq, 4);
	printf("%d\n", j);*/
}

这里写图片描述

增->头插法
void PushFront(pSeqList ps, DataType d)
{//头插
	int i = 0;
	assert(ps != NULL);
	ExpandIfRequired(ps);

		for (i = ps->sz; i > 0; i--)
		{
			ps->data[i] = ps->data[i-1];
		}
		ps->data[0] = d;
		ps->sz++;

}

测试

void testPushFront()
{
	PushFront(&Seq, 1);
	PushFront(&Seq, 2);
	PushFront(&Seq, 3);
	PushFront(&Seq, 4);
	PushFront(&Seq, 5);
	PushFront(&Seq, 6);
	PrintSeqList(&Seq);
}

这里写图片描述

指定位置插入
void Insert(pSeqList ps, int pos, DataType d)
{//指定位置插入
	int i = 0;
	assert(ps != NULL);
	ExpandIfRequired(ps);
	assert(pos >= 0 && pos <= ps->sz);
	for (i = ps->sz - 1; i >= pos; i--)
	{
		ps->data[i + 1] = ps->data[i];
	}
	ps->data[pos] = d;
	ps->sz++;
}

测试

void testPushBack()
{
	PushBack(&Seq, 1);
	PushBack(&Seq, 2);
	PushBack(&Seq, 3);
	PushBack(&Seq, 4);
	PushBack(&Seq, 5);
	PushBack(&Seq, 6);
	PrintSeqList(&Seq);
	//PopBack(&Seq);
	//PrintSeqList(&Seq);
	//PopFront(&Seq);
	//PrintSeqList(&Seq);
	Insert(&Seq, 2, 9);
	PrintSeqList(&Seq);
	//int i=Find(&Seq, 3);
	//printf("%d\n", i);
	//Erase(&Seq, 3);
	//PrintSeqList(&Seq);
	//Remove(&Seq, 2);
	//PrintSeqList(&Seq);
	//Sort(&Seq);
	//PrintSeqList(&Seq);
	/*int j=BinarySearch(&Seq, 4);
	printf("%d\n", j);*/
}

这里写图片描述
其他的和静态顺序表思想上都是差不多的,这里不多做解释。可以参考上篇博客。
上一篇:顺序表(静态)https://blog.csdn.net/qq_40550018/article/details/82662838

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值