问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

数据结构——顺序表(超详细解读)

创作时间:
作者:
@小白创作中心

数据结构——顺序表(超详细解读)

引用
CSDN
1.
https://blog.csdn.net/2303_81146519/article/details/139635625

顺序表是数据结构中的一个重要概念,它使用一段物理地址连续的存储单元来存储数据元素。本文将详细介绍顺序表的基本概念、结构以及实现方法,并提供完整的代码示例,帮助读者深入理解这一数据结构。

一、顺序表的概念与结构

顺序表(SeqList)是一种线性结构,使用一段物理地址连续的存储单元依次存储数据元素。通常使用数组来存储顺序表,相比于数组,顺序表多了增删查改等功能。

顺序表主要分为两种类型:

  • 静态顺序表:使用定长数组存储元素,其缺陷是空间分配不够灵活,给少了不够用,给多了造成空间浪费。

  • 动态顺序表:根据需要动态分配空间,实现了常用的增删改查等接口。虽然动态顺序表解决了空间分配的问题,但也存在一些缺陷:

    1. 中间/头部的插⼊删除,时间复杂度为O(N)
    2. 增容需要申请新空间,拷贝数据,释放旧空间,会有一定的消耗
    3. 增容一般呈2倍增长,可能会造成一定的空间浪费

二、动态顺序表的实现

准备工作及其注意事项

为了更好地组织代码,可以将顺序表的实现分为三个文件:

  1. SeqList.h:声明接口函数,定义顺序表结构体,以及一些公共用到的库函数集合。
  2. SeqList.c:具体实现接口函数。
  3. test.c:用于接口测试工作,即具体的使用场景。

注意事项

在实现顺序表的各种操作时,需要注意以下几点:

  1. 在处理任何接口函数之前都要进行断言检查,避免空指针。
  2. 在进行扩容操作时,要记得更新新空间的地址和容量。
  3. 插入操作后要增加size,删除操作后要减少size。
  4. 进行数据整体后移或前移时,一定要注意边界条件。

顺序表的基本接口

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;
}

通过以上代码,读者可以完整地实现和测试顺序表的各种操作。顺序表是数据结构中的基础内容,掌握顺序表的实现对于理解更复杂的线性表(如链表)和其他数据结构具有重要意义。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号