【数据结构】超详细--动态顺序表

本文介绍了一种使用结构体实现动态顺序表的方法,包括初始化、检查容量、打印、尾部及头部的插入与删除操作,以及任意位置的插入与删除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在设计动态顺序表的时候,我们主要通过结构体实现。但我们必须对结构体内部有一定的了解,因而在定义结构体时,其成员a 表示顺序表(本质上就是一个数组),size则记录了a中存储多少个有效数据, capacity表示存储空间容量的大小。

image-20221101221656478

接下来我们就要考虑动态顺序表的功能:在尾部增加或删除数据、头部增加或删除数据以及任意位置增删数据。

1. 前期准备

1.1 顺序表的初始化模块

void SLInit(SL* ps)
{
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

将顺序表清空,方便接下来的操作。

1.2 顺序表检查模块


void SLCheckCapacity(SL* ps)
{
	assert(ps);

	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->a,newCapacity*sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity = newCapacity;
	}
}

主要通过realloc来动态开辟堆中的存储空间,并检查顺序表是否耗尽,若耗尽则开辟新空间;若新开辟空间为空则退出。

1.3 打印模块

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

主要功能是将对应的操作在显示界面输出。

2. 尾部插入及删除数据

2.1 尾部插入数据

在运行该功能时,检查顺序表的空间是否有余,然后再插入:

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);

	SLCheckCapacity(ps);

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

思路详见下图:

image-20221101224241728

2.2 尾部删除

选择assert作为暴力检查,避免后期排故的繁杂。

void SLPopBack(SL* ps)
{
	assert(ps->size > 0);

	ps->size--;
}

2.3 测试

void TestSeqList1()
{
	SL sl;
	SLInit(&sl);

	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(&sl);

	SLPopBack(&sl);
	SLPrint(&sl);

	SLDestroy(&sl);
}

结果如下:

image-20221101235509825

3. 头部插入及删除数据

3.1 头部插入

通过end来控制顺序表中0~end的位置整体向后便宜一个sizeof(SLDataType)

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);

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

思路详见下图:

image-20221101230420008

3.2 头部删除

主要通过begin变量来将顺序表中存储内容整体向前移动1个位置。

void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);

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

image-20221101230208844

3.3 测试

功能测试时尽量使用函数测试,以避免菜单测试的不确定因素干扰。

void TestSeqList2()
{
	SL sl;
	SLInit(&sl);

	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPushBack(&sl, 5);
	SLPrint(&sl);

	SLPushFront(&sl, 25);
	/*SLPopBack(&sl);*/
	SLPrint(&sl);

	SLPopFront(&sl);
	SLPrint(&sl);

	SLDestroy(&sl);
}

结果如下:

image-20221101235332376

4. 任意位置插入及删除数据

4.1 任意位置插入

任意位置插入数据必须考虑到头部插入和尾部插入。

void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos <= ps->size-1);
	assert(pos >= 0);

	SLCheckCapacity(ps);

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

}

image-20221101232158503

4.2 任意位置删除

void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0);
	assert(pos < ps->size);

	SLCheckCapacity(ps);

	//挪动数据覆盖
	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}

	ps->size--;
}

image-20221101232851181

4.3 测试

void TestSeqList3()
{
	SL sl;
	SLInit(&sl);

	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	SLPrint(&sl);

	SLInsert(&sl, 2,80);
	SLPrint(&sl);

	SLErase(&sl,1);
	SLPrint(&sl);

	SLDestroy(&sl);
}

结果如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HvkHZoSm-1667370521486)(https://gitee.com/joes_ju/personal_images/raw/master/img/202211012351959.png)]

image-20221101234852009

5. 项目实现

头文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
//动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
	int* a;
	int size;
	int capacity;
}SL;
//初始化
void SLInit(SL* ps);
//检查
void SLCheckCapacity(SL* ps);
//打印
void SLPrint(SL* ps);
//尾插
void SLPushBack(SL* ps, SLDataType x);
// 尾删
void SLPopBack(SL* ps);
//头插
void SLPushFront(SL* ps,SLDataType x);
//头删
void SLPopFront(SL* ps);
//任意位置插入及删除
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
//删除
void SLDestroy(SL* ps);

顺序表实现

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void SLInit(SL* ps)
{
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

void SLCheckCapacity(SL* ps)
{
	assert(ps);

	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->a,newCapacity*sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity = newCapacity;
	}
}

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

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);

	SLCheckCapacity(ps);

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

void SLDestroy(SL* ps)
{
	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
		ps->size = ps->capacity = 0;
	}
}

void SLPopBack(SL* ps)
{
	assert(ps->size > 0);

	ps->size--;
}

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);

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

void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);

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

void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos <= ps->size-1);
	assert(pos >= 0);

	SLCheckCapacity(ps);

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

}

void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0);
	assert(pos < ps->size);

	SLCheckCapacity(ps);

	//挪动数据覆盖
	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}

	ps->size--;
}

菜单

void menu()
{
	printf("**********************************\n");
	printf("******1.尾插数据*****2.尾删数据****\n");
	printf("******3.头插数据*****4.头删数据****\n");
	printf("******5.打印数据****-1.退出********\n");
	printf("**********************************\n");

}
int main()
{
	SL s;
	SLInit(&s);

	int option = 0;
	int val = 0;//尾插的数据
	do
	{
		menu();
		printf("请输入你的选择:>");
		scanf("%d", &option);
		switch (option)
		{
		case 1:
			printf("请依次输入你要尾插的数据,以-1结束");
			scanf("%d",&val);
			while (val != -1)
			{
				SLPushBack(&s, val);
				scanf("%d", &val);
			}
			break;
		case 2:
			SLPopBack(&s);
			break;
		case 3:
			printf("请依次输入你要头插的数据,以-1结束");
			scanf("%d", &val);
			while (val != -1)
			{
				SLPushFront(&s, val);
				scanf("%d", &val);
			}
			break;
		case 4:
			SLPopFront(&s);
			break;
		case 5:
			SLPrint(&s);
			break;
		default:
			break;
		}
	} while (option != -1);
	
	SLDestroy(&s);
	return 0;
}

6. 优化:

在上述讲解中,可以看出任意插入及删除模块完全可以实现头部插入、删除及尾部插入及删除的功能,具体优化为:

头部插入

void SLPushFront(SL* ps, SLDataType x)
{
	SLInsert(ps, 0, x);
}

头部删除

void SLPopFront(SL* ps)
{
	SLErase(ps, 0);
}

尾部插入

void SLPushBack(SL* ps, SLDataType x)
{
	SLInsert(ps, ps->size, x);
}

尾部删除

void SLPopBack(SL* ps)
{
	SLErase(ps, ps->size - 1);
}

若果您有更好的优化方案或实现动态顺序表的方案,恳请留言指正!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值