【数据结构-C】顺序表接口

顺序表的概念及结构

1.线性表(linearlist)是n个具有相同特性的数据元素的有限序列。

2.常见的线性表:顺序表、链表、栈、队列、字符串...

3.线性表在逻辑上是线性结构,但是在物理结构上并不⼀定是连续的

4.线性表在物理上存储时,通常以数组和链式结构的形式存储。

顺序表分类

###顺序表和数组的区别

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

###顺序表分类

####静态顺序表---就是定长数组存储元素

c 复制代码
#define N 7
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType arr[N];//定长数组
	int size;//有效个数
}SL;

静态顺序表缺陷:空间给少了不够⽤,给多了造成空间浪费

####动态顺序表---可增容

c 复制代码
typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效元素个数
	int capacity;//有效空间
}SL;

动态顺序表的接口实现

头文件:顺序表的结构+方法的声明

源文件:实现的方法 源文件里包含头文件

###初始化和销毁

c 复制代码
#include"SeqList.h"
//初始化和销毁 
void SLInit(SL* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->arr);
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

###扩容+尾插和头插

c 复制代码
//扩容 
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->arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		//申请成功
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}
//尾插 凡是插入就一定要size++
//插入之前一定要保证有位置插入
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);//先判断够不够,在插
	ps->arr[ps->size++] = x;//先插入在++
}
//头插  只要是插入就要判断空间够不够,判断函数单独封装
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);//先判断够不够,在插
	//后移
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];//arr[1]=arr[0]最后一步:0挪到1-->结束条件
	}
	ps->arr[0] = x;
	ps->size++;
}

###打印+尾删头删

c 复制代码
//打印
void SLPrint(SL ps)//打印不会改变顺序表,不用传地址
{
	//不是指针,用点号就访问了
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d ", ps.arr[i]);
	}
	printf("\n");
}

//尾删
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	assert(ps->size);//删除数据表不能为空
	ps->size--;
}

//头删
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	//前移
	for (int i = 0; i > ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];//arr[size-2]=arr[size-1]--->结束条件
	}
	ps->size--;
}

###指定位置之前插⼊/删除数据

c 复制代码
//指定位置之前插⼊
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos>=0&&pos<=ps->size);
	SLCheckCapacity(ps);
	//pos及pos之后的元素后移
	//size-1挪到size 处,所以i初始为size
	for (int i = ps->size; i>pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];//arr[pos+1]=arr[pos]
	}
	ps->arr[pos] = x;
	ps->size++;
}

//指定位置前删除数据
SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	//删除直接覆盖就好
	for (int i = ps->size-1; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i + 1];//arr[size-2]=arr[size-1]
	}
	ps->size--;
}

###查找

c 复制代码
//查找
int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;//找到了就返回下标
		}
		return -1;//没找到就返回无效下标,判断find是否>0就知道有没有这个数据了
	}
}
相关推荐
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
LDR0063 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术3 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园3 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob3 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
小小工匠3 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
源分享3 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.3 天前
C语言--day30
c语言·开发语言
玖玥拾3 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
何以解忧,唯有..3 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang