顺序表相关知识点

一、线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是⼀种在实际中⼴泛使⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串...

线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

二、顺序表

2.1 概念与结构

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

顺序表和数组的区别:顺序表的底层结构是数组,对数组的封装,实现了常用的增删改查等接口。

2.2 分类

2.2.1 静态顺序表

概念:使用定长数组存储元素

静态顺序表缺陷:空间少了不够用,空间多了浪费。

2.2.2 动态顺序表

三、顺序表的具体操作

3.1 初始化

复制代码
void SLInit(SL* ps);

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

void SLCheckCapacity(SL* ps)
{
	//判断空间是否足够
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		//增容
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}

3.2 打印

复制代码
void SLPrint(SL* ps);

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

3.3 尾插

复制代码
void SLPushBack(SL* ps, SLDataType x);

void SLPushBack(SL* ps, SLDataType x)
{
	//判断空间是否足够
	SLCheckCapacity(ps);
	//空间足够情况下
	ps->arr[ps->size++] = x;
}

3.4 头插

复制代码
void SLPushFront(SL* ps, SLDataType x);

void SLPushFront(SL* ps, SLDataType x)
{
	//if (ps == NULL)
	//{
	//	return;
	//}
	assert(ps);
	// 判断空间是否足够
	SLCheckCapacity(ps);
	//将顺序表中所有数据向后挪动一位
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	++ps->size;
}

3.5 尾删

复制代码
void SLPopBack(SL* ps);

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

3.6 头删

复制代码
void SLPopFront(SL* ps);

void SLPopFront(SL* ps)
{
	assert(ps && ps->size);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	--ps->size;
}

3.7 指定位置之前插入数据

复制代码
void SLInsert(SL* ps, int pos, SLDataType x);

void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	//pos及之后的数据整体向后挪动一位
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	++ps->size;
}

3.8 删除pos位置的数据

复制代码
void SLErase(SL* ps, int pos);

void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	for (int i = pos; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i+1];
	}
	--ps->size;
}

3.9 查找

复制代码
int SLFind(SL* ps, SLDataType x);

int SLFind(SL* ps, SLDataType x)
{
	for (int i = 0; i < ps -> size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}

3.10 销毁

复制代码
void SLDestroy(SL* ps);

void SLDestroy(SL* ps)
{
	assert(ps);
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
相关推荐
wabs66611 小时前
关于二叉树【力扣572.另一棵树的子树的思考】
数据结构·c++·算法·leetcode·二叉树
雨落在了我的手上12 小时前
Java数据结构(十四):Map和Set
数据结构
码行山野赴时序归途13 小时前
链表家族收官篇:循环链表
c语言·开发语言·数据结构·链表
船厂电气自动化ai大模型15 小时前
AI大模型与数学|第81天 课程:正交向量、正交基、格拉姆‑施密特(Gram‑Schmidt)正交化
开发语言·数据结构·人工智能·线性代数·机器学习
库玛西15 小时前
数据结构与算法之美:树、森林与二叉树全解析
c语言·数据结构·笔记·考研·算法·学习方法
careathers15 小时前
【数据结构】队列
java·数据结构
Rabitebla16 小时前
【Linux系统编程】指令(三):创建、删除、复制、移动、看内容
linux·数据结构·c++·算法
我不会起名字32217 小时前
一天一道力扣Hot100(36):深度优先算法---组合总和
数据结构·c++·后端·python·算法·go
雨落在了我的手上18 小时前
Java数据结构(十五):哈希表
数据结构
有点。1 天前
C++03阶段练习(练习题)
数据结构·算法·图论