• 数据结构初阶:顺序表和链表


    线性表

    线性表 linear list n 个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使
    用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串 ...
    线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,
    线性表在物理上存储时,通常以数组和链式结构的形式存储。

    顺序表

    概念及结构

    顺序表是用一段 物理地址连续 的存储单元依次存储数据元素的线性结构,一般情况下采用数组存
    储。在数组上完成数据的增删查改。
    与数组的区别:
    特点:只能从头开始连续存储

    顺序表一般可以分为:
    1. 静态顺序表:使用定长数组存储元素
    缺点:不知道需要多少,N给小了不够用,N给大了浪费。

    2. 动态顺序表:使用动态开辟的数组存储。

     接口实现

    静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致 N 定大了,空
    间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间
    大小,所以下面我们实现动态顺序表。
    注意:

    Seqlist.h

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. typedef int SLDataType;
    6. typedef struct SeqList
    7. {
    8. SLDataType* a;
    9. int size; // 有效数据
    10. int capacity; // 空间容量
    11. }SL;
    12. void SLInit(SL* psl);
    13. void SLDestroy(SL* psl);
    14. void SLPrint(SL* psl);
    15. void SLCheckCapacity(SL* psl);
    16. // 头尾插入删除
    17. void SLPushBack(SL* psl, SLDataType x);
    18. void SLPushFront(SL* psl, SLDataType x);
    19. void SLPopBack(SL* psl);
    20. void SLPopFront(SL* psl);
    21. // pos下标位置前的插入
    22. void SLInsert(SL* psl, int pos, SLDataType x);
    23. // pos下标位置的删除
    24. void SLErase(SL* psl, int pos);

    SeqList.c

    1. #include"SeqList.h"
    2. void SLInit(SL* psl)
    3. {
    4. assert(psl);
    5. psl->a = NULL;
    6. psl->size = 0;
    7. psl->capacity = 0;
    8. }
    9. void SLDestroy(SL* psl)
    10. {
    11. assert(psl);
    12. if (psl->a != NULL)
    13. {
    14. free(psl->a);
    15. psl->a = NULL;
    16. psl->size = 0;
    17. psl->capacity = 0;
    18. }
    19. }
    20. void SLPrint(SL* psl)
    21. {
    22. assert(psl);
    23. for (int i = 0; i < psl->size; i++)
    24. {
    25. printf("%d ", psl->a[i]);
    26. }
    27. printf("\n");
    28. }
    29. void SLCheckCapacity(SL* psl)
    30. {
    31. assert(psl);
    32. if (psl->size == psl->capacity)
    33. {
    34. int newCapacity = psl->capacity
  • 相关阅读:
    软件设计模式系列之二十二——状态模式
    SpringBoot 配置文件使用详解
    Nacos 配置中心
    一波免费、好用的API接口分享
    docker save与docker export的区别
    Spring框架系列(6) - Spring IOC实现原理详解之IOC体系结构设计
    什么是鉴权?这些postman鉴权方式你又知道多少?
    手写java冒泡、插入、选择、快速、归并排序算法
    分布式文件存储——分块上传和断点续传
    在Qt中,怎么获取到在mainwindow.ui文件中添加的控件
  • 原文地址:https://blog.csdn.net/qq_71925940/article/details/137261079