顺序表相关知识点

一、线性表

线性表(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;
}
相关推荐
华玥作者8 小时前
uniapp 万条数据不卡顿:我写了个虚拟列表组件 hy-list,原生支持瀑布流
数据结构·uni-app·list·vue3
2401_841495649 小时前
【数据结构】B*树
数据结构·c++·b树·算法·删除·插入·三分分裂
不如语冰9 小时前
AI大模型入门-模块导入import
数据结构·人工智能·pytorch·python
岑梓铭9 小时前
《考研408数据结构》第七章(7.1 查找:顺序查找、折半查找、分块查找)复习笔记
数据结构·笔记·考研·408·ds·查找
壹号用户10 小时前
c++入门之list了解及使用
数据结构·list
来一碗刘肉面10 小时前
什么是双端队列
数据结构·链表
流浪00111 小时前
数据结构篇(五):线性表——栈
数据结构·c++·算法
拂拉氏12 小时前
【知识讲解】 链式哈希表的实现与unordered_map和unordered_set的封装
数据结构·哈希算法·散列表
haolin123.12 小时前
数据结构--二叉树
数据结构