初阶数据结构(顺序表的实现)

1. 线性表

线性表是(linear list)n个具有相同特性的数据元素的有限队列。线性表是一种在实际广泛应用的的数据结构,常见的线性表:顺序表,链表,栈,队列,字符串。。。

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

2. 顺序表

2.1 概念与结构

概念:顺序表是用一段物理地址连续的存储单元一次存储数据元素的线性结构,一般采用与数组类似的存储方式。

顺序表与数组的区别

顺序表的底层是数组,对数组进行封装,实现了增删查改等接口

2.2 分类

2.2.1 静态顺序表

概念:使用定长数组进行存储数据的结构

//静态顺序表

typedef int SLDataType;

#define N 7

typedef struct SeqList

{

SLDataType arr[N];// 定长数组

int size; //有效数据个数

}SL;

2.2.2 动态顺序表

//动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr; //动态数组
int size; //有效数据个数
int capacity; //空间大小
}SL;

2.3 动态顺序表的实现

// 初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
// 扩容
void SLCheckCapacity(SL* ps);
// 头部插⼊删除 / 尾部插⼊删除
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
// 指定位置之前插⼊ / 删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
//查找
int SLFind(SL* ps, SLDataType x);

2.3.1 初始化,销毁,打印

复制代码
// 初始化
void SeqListInit(SL* ps)
{
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

//销毁
void SeqListDestroy(SL* ps)
{
	if (ps->arr!=NULL)
		free(ps->arr);
	ps->arr = NULL;
	free(ps->capacity & ps->size);
	ps->capacity = ps->size = 0;
}
//打印
void SeqListPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d->", ps->arr[i]);
	}
	printf("NULL\n");
}

2.3.2 扩容

复制代码
void SLcheckCapacity(SL* ps)
{
	//插入之前判断空间是否足够
    //三目操作符
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDateType* tmp = (SLDateType*)realloc(ps->arr, sizeof(SLDateType) * newcapacity);
		if (tmp == NULL)
		{
			printf("realloc fail!\n");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}

2.3.3 头部插⼊删除 / 尾部插⼊删除

复制代码
//头插
void SeqListPushFront(SL* ps, SLDateType x)
{
	SLcheckCapacity(ps);
	for (int i = ps->size;i>0; i--)
		ps->arr[i] = ps->arr[i - 1];
	ps->arr[0] = x;
	ps->size++;
}

//头删
void SeqListPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size-1; i++)
		ps->arr[i] = ps->arr[i + 1];
	ps->size--;
}
//尾删
void SeqListPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}
//尾插
void SeqListPushBack(SL* ps, SLDateType x)
{
	assert(ps);
	SLcheckCapacity(ps);//检查容量
	ps->arr[ps->size] = x;
	ps->size++;
}

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

复制代码
// 顺序表在pos位置插入x
void SeqListInsert(SL* ps, int pos, SLDateType x)
{
	SLcheckCapacity(ps);
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	for (int i = ps->size; i > pos; i--)
		ps->arr[i] = ps->arr[i - 1];
	ps->arr[pos] = x;
	ps->size++;
}

// 顺序表删除pos位置的值
void SeqListErase(SL* ps, int pos)
{
	assert(ps);
	assert(ps->size);
	for (int i = pos; i<ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

2.3.5 查找

复制代码
// 顺序表查找
int SeqListFind(SL* ps, SLDateType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (x == ps->arr[i])
			return i;
	}
	return -1;
}
相关推荐
一起努力啊~2 小时前
算法刷题--长度最小的子数组
开发语言·数据结构·算法·leetcode
小北方城市网2 小时前
第1课:架构设计核心认知|从0建立架构思维(架构系列入门课)
大数据·网络·数据结构·python·架构·数据库架构
好易学·数据结构2 小时前
可视化图解算法77:零钱兑换(兑换零钱)
数据结构·算法·leetcode·动态规划·力扣·牛客网
独自破碎E2 小时前
【归并】单链表的排序
数据结构·链表
L_09072 小时前
【C++】高阶数据结构 -- 平衡二叉树(AVLTree)
数据结构·c++
冰冰菜的扣jio2 小时前
Redis基础数据结构
数据结构·数据库·redis
阿华hhh3 小时前
项目(购物商城)
linux·服务器·c语言·c++
Qhumaing3 小时前
C++学习:【PTA】数据结构 7-2 实验6-2(图-邻接表)
数据结构·c++·学习
方便面不加香菜3 小时前
基于顺序表实现通讯录项目
c语言·数据结构
MQLYES4 小时前
03-BTC-数据结构
数据结构·算法·哈希算法