顺序表相关知识点

一、线性表

线性表(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;
}
相关推荐
白狐_7988 小时前
408数据结构第5章:二叉树遍历序列题——技巧、判断与真题型总结
数据结构
Forever Nore11 小时前
学完C语言力扣第一题做不来正常吗
数据结构·算法
旖旎夜光14 小时前
LeetCode 11:盛最多水的容器(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针
不一样的故事12615 小时前
芯片设计是一个高度复杂、分工精细的系统工程
数据结构·经验分享
梁辰兴16 小时前
软件工程:面向数据结构的设计方法
数据结构·软件工程·梁辰兴·面向数据结构的设计方法·jackson方法·warnier方法·方法比较
白狐_79817 小时前
408数据结构第7章:查找②——顺序、折半、分块查找秒杀 + 真题
数据结构·算法
hold?fish:palm18 小时前
链表的基本原理和实现(C++版本)
数据结构·c++·链表
码完就睡18 小时前
数据结构——树、二叉树基础概念
数据结构·算法
疯狂打码的少年20 小时前
【数据结构】栈:定义、顺序栈与链式栈
数据结构·笔记
wabs66620 小时前
关于图论【最短路径之Bellman_ford 算法|卡码网94.城市间货物运输的思考】
数据结构·算法·图论·卡码网·bellman_ford·求最短路径