• [C/C++]数据结构----顺序表的实现(增删查改)


    概念

            顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储,在数组上完成数据的增删改查.

    一般顺序表可以分为两种结构: 

    1.静态顺序表:采用定长数组存储元素

    1. #define N 7
    2. typedef int SLDataType;
    3. typedef struct SeqList
    4. {
    5. SLDataType arr[N]; //定长数组
    6. int size; //有效数据的个数
    7. }SeqList;

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

    1. typedef int SLDataType;
    2. typedef struct SeqList
    3. {
    4. SLDataType* arr; //指向动态开辟的数组
    5. int size; //有效数据个数
    6. int capacity; //容量空间大小
    7. }SeqList;

         由于静态顺序表只适用于确定知道需要多少数据的场景,如果静态顺序表的定长数组N定大了,就会造成空间浪费,如果N定小了, 空间又不够用.所以现实中通常都是使用动态顺序表,按需开辟空间.

    接口及实现

    1. typedef int SLDateType;
    2. typedef struct SeqList
    3. {
    4. SLDateType* a;
    5. int size;
    6. int capacity;
    7. }SeqList;
    8. // 对数据的管理:增删查改
    9. void SeqListInit(SeqList* ps);
    10. void SeqListDestroy(SeqList* ps);
    11. void SeqListPrint(SeqList* ps);
    12. void SeqListPushBack(SeqList* ps, SLDateType x);
    13. void SeqListPushFront(SeqList* ps, SLDateType x);
    14. void SeqListPopFront(SeqList* ps);
    15. void SeqListPopBack(SeqList* ps);
    16. // 顺序表查找
    17. int SeqListFind(SeqList* ps, SLDateType x);
    18. // 顺序表在pos位置插入x
    19. void SeqListInsert(SeqList* ps, int pos, SLDateType x);
    20. // 顺序表删除pos位置的值
    21. void SeqListErase(SeqList* ps, int pos);

    1. 顺序表初始化

            在进行操作前,先断言一下传过来的指针是否为空,若为空则会在终端提示错误信息,要注意使用asssert(),需要包含头文件assert.h

    1. void SeqListInit(SeqList* ps)
    2. {
    3. assert(ps);
    4. ps->a = NULL;
    5. ps->size = 0;
    6. ps->capacity = 0;
    7. }

       2.打印顺序表

            同样先检查指针是否为空,在遍历顺序表进行打印

    1. void SeqListPrint(SeqList* ps)
    2. {
    3. assert(ps);
    4. for (int i = 0; i < ps->size; i++)
    5. {
    6. printf("%d ", ps->a[i]);
    7. }
    8. printf("\n");
    9. }

    3.顺序表的摧毁

            如果传过来的顺序表本身就是空指针,说明顺序表就不存在,不需要进行其他操作.若指针不为空,就把其定长数组指向空,把有效数据个数和容量赋值为0

    1. void SeqListDestroy(SeqList* ps)
    2. {
    3. assert(ps);
    4. if (ps->a != NULL)
    5. {
    6. free(ps->a);
    7. ps->a = NULL;
    8. ps->size = 0;
    9. ps->capacity = 0;
    10. }
    11. }

    4.顺序表尾插

            在进行插入操纵前,需要判断一下顺序表的有效数据个数是否大于等于已开辟的容量,如果小于的话,直接尾插即可,如果大于等于容量的话,就需要先进行扩容操作,再尾插,为了使代码更加简洁明了,可以把扩容操作单独封装成一个函数

    1. void SeqListPushBack(SeqList* ps, SLDataType x)
    2. {
    3. assert(ps);
    4. CheckCapacity(ps);
    5. ps->a[ps->size] = x;
    6. ps->size++;
    7. }

    扩容函数:

            如果size==capacity的话就要进行扩容,我们规定每次扩容二倍.由于我们在初始化顺序表时将容量定为0,如果是第一次扩容的话,我们需要先给其扩容一个固定的大小,以后的扩容直接在这个容量上扩大二倍即可.

    1. void CheckCapacity(SeqList* ps)
    2. {
    3. assert(ps);
    4. if (ps->size == ps->capacity)
    5. {
    6. int newcapacity = ps->capacity == 0 ? 4 : ps->capacity*2;
    7. SLDataType* ret = (SLDataType*)realloc(ps->a, newcapacity* sizeof(SLDataType));
    8. if (ret == NULL) //检查是否开辟成功
    9. {
    10. perror("realloc"); //如果不成功就打印开辟错误信息并返回
    11. return;
    12. }
    13. else
    14. {
    15. ps->a = ret;
    16. ps->capacity = newcapacity; //更新容量大小
    17. }
    18. }
    19. }

    5.顺序表头插

    因为要在数组头部插入数据,所以需要把头部腾出一个位置,这就需要将所有数据向后移动一个单位

    1. void SeqListPushFront(SeqList* ps, SLDataType x)
    2. {
    3. assert(ps);
    4. CheckCapacity(ps);
    5. int end = ps->size - 1;
    6. while (end >= 0)
    7. {
    8. ps->a[end + 1] = ps->a[end];
    9. end--;
    10. }
    11. ps->a[0] = x;
    12. ps->size++;
    13. }

    6.顺序表头删

    1. void SeqListPopFront(SeqList* ps) //头删
    2. {
    3. assert(ps);
    4. for (int i = 1; i < ps->size; i++)
    5. {
    6. ps->a[i - 1] = ps->a[i];
    7. }
    8. ps->size--;
    9. }

    7.顺序表尾删

    1. void SeqListPopBack(SeqList* ps)
    2. {
    3. assert(ps);
    4. if (ps->size > 0)
    5. {
    6. ps->size--;
    7. }
    8. }

    8.顺序表查找

    1. int SeqListFind(SeqList* ps, SLDataType x)
    2. {
    3. assert(ps);
    4. for (int i = 0; i < ps->size; i++)
    5. {
    6. if (ps->a[i] == x)
    7. {
    8. return i;
    9. }
    10. }
    11. printf("未找到\n");
    12. return - 1;
    13. }

    9.顺序表在pos位置插入x

    1. void SeqListInsert(SeqList* ps, int pos, SLDataType x)
    2. {
    3. assert(ps);
    4. assert(pos >= 0 && pos <= ps->size);
    5. for (int i = ps->size; i > pos; i--)
    6. {
    7. ps->a[i] = ps->a[i - 1];
    8. }
    9. ps->a[pos] = x;
    10. ps->size++;
    11. }

    10.顺序表删除pos位置元素

    1. void SeqListDelete(SeqList* ps, int pos)
    2. {
    3. assert(ps);
    4. assert(pos >= 0 && pos < ps->size);
    5. int begin = pos + 1;
    6. while (begin < ps->size)
    7. {
    8. ps->a[begin - 1] = ps->a[begin];
    9. begin++;
    10. }
    11. ps->size--;
    12. }

  • 相关阅读:
    wpf工程中加入Hardcodet.NotifyIcon.Wpf生成托盘
    STL-deque
    德思特分享丨一文带你了解ADC测试参数有哪些?
    机器学习中的集成学习算法和K-近邻算法及其优缺点
    iOS15.4来袭:新增“男妈妈”表情及口罩面容解锁、AirTags反跟踪等新功能
    node---模块
    微信收款码提现要手续费吗
    Win11用户名和密码备份方法
    鼠标知识系列之星闪鼠标
    2种方法,jmeter用一个正则提取器提取多个值!
  • 原文地址:https://blog.csdn.net/m0_74910646/article/details/134436880