数据结构——顺序表(超详细解读)
创作时间:
作者:
@小白创作中心
数据结构——顺序表(超详细解读)
引用
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;
}
通过以上代码,读者可以完整地实现和测试顺序表的各种操作。顺序表是数据结构中的基础内容,掌握顺序表的实现对于理解更复杂的线性表(如链表)和其他数据结构具有重要意义。
热门推荐
360度评估:揭秘综合评价下的全面视角和深度分析
泽连斯基:从喜剧演员到战时总统,一个人的国家保卫战
解决Win11系统无法检测到第二块显示器的困扰
可以用空气炸锅烹饪冷冻蔬菜吗?是的,方法如下!
什么是首件检验FAI?为什么首件检验是必要的?首件检验的流程?
太空为什么需要女性?女航天员飞出地球的历史与未来
《诗经》里的名字,古风雅致,诗意十足!
山西GDP下滑背后:中国北方产业凋零的秘密与破局之路
肝郁屁多,脾虚拉稀,肺虚汗多,三个中成药,疏肝、健脾、强肺!
“梅须逊雪三分白,雪却输梅一段香”:卢梅坡《雪梅》中的修辞艺术
静息电位及其形成原理
江河湖海皆可爱,莞邑文脉源流长
非遗民俗焕新彩:东莞厚街“抢炮头” 万人欢乐庆祥瑞
中国米粉看江西,江西米粉好吃吗为啥总遭外地人吐槽,了解一下
凝血功能障碍的护理诊断
《局外人》:荒诞世界的真实写照?
数学史上最伟大的数学家之一:拉格朗日
金庸小说中的人物与真实历史的差异
四六级备考指南:考试流程、备考策略与学习资源推荐
人际关系中的心理学:从误解到理解
探秘递增的序列:揭秘等比数列的奇妙规律
双一流院校研究生,毕业薪酬统计!
信息管理与信息系统专业全解析:学什么?就业去向?未来前景如何
做了一次胃镜多久可以做第二次
坚持跑步12年:从马拉松小白到心态平和的跑者旅程
吃饭时 1 个动作,不仅控血糖、减肥,还有诸多好处,很多人不知道……
最正宗“兰州拉面”最早来自哪里?不是甘肃,也不是青海
探访花山岩画:倾听千年前的骆越故事
中药牛大力功效与作用
虚拟币交易受法律保护吗?