《数据结构》顺序表+算法代码+动画演示-C语言版

目录

顺序表概念

顺序表初始化

顺序表销毁

顺序表尾插

顺序表尾删

顺序表头删

顺序表头插

顺序表pos位置插入

顺序表pos位置删除

顺序表全部代码如下:


顺序表概念

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

  1. 静态顺序表:使用定长数组存储元素
  2. 动态顺序表:使用动态开辟的数组存储。

顺序表初始化

cpp 复制代码
void SLInit(SL* psl)
{
	assert(psl);

	psl->a = NULL;
	psl->size = 0;
	psl->capacity = 0;
}

顺序表销毁

cpp 复制代码
void SLDestory(SL* psl)
{
	if (psl->a != NULL)
	{
		free(psl->a);
		psl->a = NULL;
		psl->size = 0;
		psl->capacity = 0;
	}
}

顺序表尾插

  • 尾插不需要扩容的直接插入即可
  • 尾插需要扩容的

尾插的代码如下:

cpp 复制代码
void SLPushBack(SL* psl, SLDateType x)
{
	SLCheackCapacity(psl);

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

顺序表尾删

尾删代码如下:

cpp 复制代码
void SLPopBack(SL* psl)
{
	assert(psl->size > 0);
	psl->size--;

}

顺序表头删

顺序表头删代码如下

cpp 复制代码
void SLPopFront(SL* psl)
{
	assert(psl->size > 0);
	int begin = 0;
	while (begin <psl->size-1 )
	{
		psl->a[begin] = psl->a[begin + 1];
		begin++;
	}
	psl->size--; 
	/*int start = psl->a[0];
	while (1)
	{
		psl->a[] = psl > a[psl->];
	}*/
}

顺序表头插

顺序表头插代码如下:

cpp 复制代码
void SLPushFront(SL* psl, SLDateType x)
{
	//检查空间够不够
	SLCheackCapacity(psl);

	int end = psl->size - 1;

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

总结:顺序表的头删头插的复杂度极高,如果想进行头删头插需要将数据一个一个的挪动,建议:不要使用顺序表进行头删头插,效率太低了

顺序表pos位置插入

顺序表pos位置插入代码如下

cpp 复制代码
void SLInert(SL* psl, int pos, SLDateType x)
{
	assert(psl);

	assert(pos >= 0 && pos <= psl->size);

	SLCheackCapacity(psl);

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

}

顺序表pos位置删除

顺序表pos位置删除代码如下:

cpp 复制代码
void SLErace(SL* psl, int pos, SLDateType x)
{
	assert(psl);
	assert(pos >= 0 && pos < psl->size);
	int begin = pos + 1;
	//往前覆盖
	while (begin < psl->size)
	{
		psl->a[begin - 1] = psl->a[begin];
		begin++;
	}
	psl->size--;
}

有没有发现我们如果想快速找到pos位置的值我可以可以通过下标进行找到。

顺序表全部代码如下:

cpp 复制代码
void SLInit(SL* psl)
{
	assert(psl);

	psl->a = NULL;
	psl->size = 0;
	psl->capacity = 0;
}

void SLDestory(SL* psl)
{
	if (psl->a != NULL)
	{
		free(psl->a);
		psl->a = NULL;
		psl->size = 0;
		psl->capacity = 0;
	}
}

void SLPushBack(SL* psl, SLDateType x)
{
	SLCheackCapacity(psl);

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

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

void SLCheackCapacity(SL* psl)
{
	if (psl->size == psl->capacity)
	{
		int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
		SLDateType* temp = realloc(psl->a, sizeof(SLDateType) * newCapacity); //扩容
		if (temp == NULL)
		{
			perror("realloc fail:");
			return;
		}
		psl->a = temp;
		psl->capacity = newCapacity;
	}
}


void SLPushFront(SL* psl, SLDateType x)
{
	//检查空间够不够
	SLCheackCapacity(psl);

	int end = psl->size - 1;

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

void SLPopBack(SL* psl)
{

	//if (psl->size == 0)
	//{
	//	printf("顺序表现在是空的状态!");
	//	return;
	//}

	assert(psl->size > 0);

	psl->size--;
	//不能free  不支持分期还款
	//还有问题,若为空,在pop 就有问题了
	/*assert(psl->size == 0);*/
}

void SLPopFront(SL* psl)
{
	assert(psl->size > 0);
	int begin = 0;
	while (begin <psl->size-1 )
	{
		psl->a[begin] = psl->a[begin + 1];
		begin++;
	}
	psl->size--; 
	/*int start = psl->a[0];
	while (1)
	{
		psl->a[] = psl > a[psl->];
	}*/
}

void SLInert(SL* psl, int pos, SLDateType x)
{
	assert(psl);

	assert(pos >= 0 && pos <= psl->size);

	SLCheackCapacity(psl);

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

}

void SLErace(SL* psl, int pos, SLDateType x)
{
	assert(psl);
	assert(pos >= 0 && pos < psl->size);
	int begin = pos + 1;
	//往前覆盖
	while (begin < psl->size)
	{
		psl->a[begin - 1] = psl->a[begin];
		begin++;
	}
	psl->size--;
}

int SlFind(SL* psl, int pos, SLDateType x)
{
	assert(psl);
	for (int i = 0; i < psl->size; i++)
	{
		if (psl->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}
相关推荐
爱跳舞的烤冷面3 小时前
自学嵌入式第N天(Linux篇——文件编程)
java·开发语言·前端
richard_yuu3 小时前
JSON vs XML vs 二进制:3种序列化终极选型
xml·c++·qt·学习·json
没落之王3 小时前
易学使者郑氏正脉郑冰推演马航事件
大数据·人工智能·算法·机器学习·可用性测试
caimouse4 小时前
ReactOS 图形系统分析(33):字体子系统 — font.c
c语言·开发语言·reactos
阿部多瑞 ABU4 小时前
告别手工核图:基于 .NET + MuPDFCore 的 CAD 等轴测 PDF 材料表提取与新旧版本对比实战
后端·算法·ui·pdf·c#
郝学胜-神的一滴4 小时前
并查集深度入门:从玄学抽象到 QuickFind & QuickUnion 源码实战
数据结构·c++·python·程序人生·算法·软件开发
比奇堡裤头村4 小时前
统计学习方法——决策树
算法·决策树·学习方法
不悔哥4 小时前
Tinyftp 源码学习笔记:3000 行 C 实现的 FTP 服务器
服务器·c语言·网络·tcp/ip
cxr8284 小时前
HyperMind Lab M1 架构地基 Implementation Plan <二>
开发语言·人工智能·架构
灵晔君4 小时前
【Linux】进程控制(一)——进程创建、进程终止与进程等待
linux·c语言·算法