数据结构——顺序表(超详细解读)
创作时间:
作者:
@小白创作中心
数据结构——顺序表(超详细解读)
引用
CSDN
1.
https://blog.csdn.net/2303_81146519/article/details/139635625
顺序表是数据结构中的一个重要概念,它使用一段物理地址连续的存储单元来存储数据元素。本文将详细介绍顺序表的基本概念、结构以及实现方法,并提供完整的代码示例,帮助读者深入理解这一数据结构。
一、顺序表的概念与结构
顺序表(SeqList)是一种线性结构,使用一段物理地址连续的存储单元依次存储数据元素。通常使用数组来存储顺序表,相比于数组,顺序表多了增删查改等功能。
顺序表主要分为两种类型:
静态顺序表:使用定长数组存储元素,其缺陷是空间分配不够灵活,给少了不够用,给多了造成空间浪费。
动态顺序表:根据需要动态分配空间,实现了常用的增删改查等接口。虽然动态顺序表解决了空间分配的问题,但也存在一些缺陷:
- 中间/头部的插⼊删除,时间复杂度为O(N)
- 增容需要申请新空间,拷贝数据,释放旧空间,会有一定的消耗
- 增容一般呈2倍增长,可能会造成一定的空间浪费
二、动态顺序表的实现
准备工作及其注意事项
为了更好地组织代码,可以将顺序表的实现分为三个文件:
- SeqList.h:声明接口函数,定义顺序表结构体,以及一些公共用到的库函数集合。
- SeqList.c:具体实现接口函数。
- test.c:用于接口测试工作,即具体的使用场景。
注意事项
在实现顺序表的各种操作时,需要注意以下几点:
- 在处理任何接口函数之前都要进行断言检查,避免空指针。
- 在进行扩容操作时,要记得更新新空间的地址和容量。
- 插入操作后要增加size,删除操作后要减少size。
- 进行数据整体后移或前移时,一定要注意边界条件。
顺序表的基本接口
1. 创建顺序表
顺序表的结构体定义如下:
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr;
int capacity; // 记录动态顺序表申请空间的大小
int size; // 记录顺序表当前有效的数据个数
}SL;
2. 顺序表的初始化
void SLInit(SL* ps)
{
assert(ps);
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
3. 顺序表的销毁
void SLDestroy(SL* ps)
{
assert(ps);
free(ps->arr);
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
4. 顺序表的打印
void SLPrint(SL* ps)
{
for (int i = 0; i < ps->size; ++i) printf("%d ", ps->arr[i]);
printf("\n");
}
5. 顺序表的扩容检查
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLDataType* ptmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (ptmp == NULL)
{
perror("realloc fail!");
exit(1);
}
ps->arr = ptmp;
ps->capacity = newCapacity;
}
}
6. 顺序表的增加功能
尾插接口
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];
ps->arr[0] = x;
ps->size++;
}
指定位置插入接口
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(0 <= pos && pos < ps->size);
SLCheckCapacity(ps);
for (int i = ps->size; i >= pos + 1; --i) ps->arr[i] = ps->arr[i - 1];
ps->arr[pos] = x;
ps->size++;
}
7. 顺序表的删除功能
头删接口
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
for (int i = 1; i < ps->size; ++i) ps->arr[i - 1] = ps->arr[i];
ps->size--;
}
尾删接口
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
ps->size--;
}
指定位置删除接口
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(0 <= pos && pos < ps->size);
for (int i = pos + 1; i < ps->size; ++i) ps->arr[i - 1] = ps->arr[i];
ps->size--;
}
8. 顺序表的查找实现
int SLFind(SL* ps, int x)
{
for (int i = 0; i < ps->size; ++i)
{
if (ps->arr[i] == x) return i;
}
return -1;
}
总代码
以下是完整的代码实现:
SeqList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr;
int Capacity;
int size;
}SL;
void SLInit(SL* ps);
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);
void SLErase(SL* ps, int pos);
void SLInsert(SL* ps, int pos, SLDataType x);
void SLCheckCapacity(SL* ps);
void SLPrint(SL* ps);
SeqList.c
#include "SeqList.h"
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->Capacity = ps->size = 0;
}
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->Capacity)
{
int NewCapacity = ps->Capacity == 0 ? 4 : 2 * (ps->Capacity);
SLDataType* ptmp = realloc(ps->arr, NewCapacity * sizeof(SLDataType));
if (ptmp == NULL)
{
perror("realloc fail !");
exit(1);
}
ps->arr = ptmp;
ps->Capacity = NewCapacity;
}
}
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];
ps->arr[0] = x;
ps->size++;
}
void SLPopBack(SL* ps)
{
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];
ps->size--;
}
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(0 <= pos && pos < ps->size);
for (int i = pos; i < ps->size - 1; ++i) ps->arr[i] = ps->arr[i + 1];
ps->size--;
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(0 <= pos && pos < ps->size);
SLCheckCapacity(ps);
for (int i = ps->size; i >= pos + 1; --i) ps->arr[i] = ps->arr[i - 1];
ps->arr[pos] = x;
ps->size++;
}
void SLPrint(SL* ps)
{
for (int i = 0; i < ps->size; ++i) printf("%d ", ps->arr[i]);
printf("\n");
}
test.c
#include "SeqList.h"
void slTest01()
{
SL sl;
SLInit(&sl);
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPushBack(&sl, 4);
printf("测试尾插: ");
SLPrint(&sl);
SLPushFront(&sl, 10);
printf("测试头插: ");
SLPrint(&sl);
SLPopBack(&sl);
printf("测试尾删: ");
SLPrint(&sl);
SLPopFront(&sl);
printf("测试头删: ");
SLPrint(&sl);
SLInsert(&sl, 1, 15);
printf("指定插入: ");
SLPrint(&sl);
SLErase(&sl, 1);
printf("指定删除: ");
SLPrint(&sl);
}
int main()
{
slTest01();
return 0;
}
通过以上代码,读者可以完整地实现和测试顺序表的各种操作。顺序表是数据结构中的基础内容,掌握顺序表的实现对于理解更复杂的线性表(如链表)和其他数据结构具有重要意义。
热门推荐
男人什么时候刮胡子最好
10分钟快速恢复精力!斯坦福研究团队发现新型休息法
本来符合N+1,但签了协商解除协议的,恐变≦N甚至0
英超联赛前瞻:布赖顿、纽卡斯尔联等队将分别对阵维拉、布伦特福德等对手
区分软组织疼痛和神经病理性疼痛:病因诊断指南
碳酸锂对精神分裂症的作用
四季海棠花养殖方法与技巧
AI生成作品 究竟有没有著作权?
几点睡、几点起、睡多久?睡眠健康指南来了
不同香型龙井茶在不同冲泡温度下的香气物质特征
西湖龙井的品质特征
宝宝几岁可以喝纯牛奶代替奶粉
如何选择适合特定应用场景的气动O型球阀?
公司分配利润的时间期限与限制条件详解
汇金系券商整合大猜想!中金牵手银河概率几何?
体育课天天见,看这些地方,孩子们活力满满
老年人旅游注意事项:保障健康安全与舒适体验
电脑配置低?修图软件卡顿优化秘籍大公开!
儿童换牙期有哪些注意事项?需要口腔卫生/饮食搭配/心理关怀/及时就医
深入探讨铁氧体磁芯的多方面特性及其在不同领域的应用
长尾关键词的SEO优化策略与实践指南
计算机科学与技术干什么
“文旅+”人才,“新”“新”向荣
文旅项目如何组织管理
游览中领略工业文化!宁波国家高新区启动工业研学活动
开着奔驰GLC自驾游需要注意
“银龄行动”何以燃动体育
从红警系列剧情看时空穿越对世界的影响
管道平衡压袋(工厂管道平衡压袋)结构原理
食物中毒怎么办?如何照护?如何避免复发、传染?