• 数据结构之顺序表


    目录

    前言

    顺序表的基本概念

    顺序表的结构

    静态顺序表

    动态顺序表

    顺序表的接口实现

    1.顺序表的初始化

    2.顺序表的销毁

    3.顺序表显示

    4.顺序表尾插元素

    顺序表尾插元素测试

    5.顺序表尾删元素

    顺序表尾删元素测试

    6.顺序表头插元素

    扩容函数的封装

    顺序表头插元素的实现

    顺序表头插元素测试

    7.顺序表头删元素

    顺序表头删元素测试

    8.顺序表在pos位置插入元素x

    顺序表在pos位置插入元素x测试

    9.顺序表查找某元素

    顺序表查找某元素测试

    10.顺序表删除指定位置pos处的值

    顺序表删除指定位置pos处的值测试

     11.头插函数改造版本SeqlistPushfront()函数

    SeqlistPushfront()函数测试

    12.尾插函数改造版本SeqlistPushback()函数

    SeqlistPushback()函数测试

    13.头删函数改造版本SeqlistPopfront()函数

     SeqlistPopfront()函数测试

    14.尾删函数改造版本SeqlistPopback()函数

    SeqlistPopback()函数测试

    15.顺序表修改pos位置的值

    顺序表修改pos位置的值测试 


    前言

    顺序表采用模块化编程思路,顺序表的实现使用3个模块,test.c—测试模块 Seqlist.c—接口函数的实现模块  seqlist.h—接口函数声明

    顺序表的基本概念

    顺序表是在计算机内存中通常以数组形式存储的线性表,线性表是n个具有相同特性的数据元素的有限序列线性表的顺序存储是用一组地址连续的存储单元依次存储线性表中的各个元素;采用顺序存储结构的线性表通常称为顺序表,顺序表是将表中的节点依次存放在计算机内存中一组地址连续的存储单元之中;

    顺序表的结构

    静态顺序表

    静态顺序表:使用定长数组存储元素;

    1. //静态顺序表
    2. # define N 100
    3. typedef int SeqlistDatatye;
    4. typedef struct Seqlist
    5. {
    6. SeqlistDatatye nums[N];
    7. int size;//记录有效数据的个数
    8. }Seqlist;

    静态顺序表的缺点:当定义容量的N太小,空间不够使用,当N过大,浪费多余的空间;

    为解决这种缺点,创建了动态顺序表,可以根据需要分配空间的大小;

    动态顺序表

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

    1. //动态顺序表
    2. typedef int SLDataType;
    3. typedef struct Seqlist
    4. {
    5. SLDataType* nums;//nums指向动态开辟的数组
    6. int size;//记录有效数据的个数
    7. int capacity;//存储有效数据的容量大小,单位为每个结构体的大小
    8. }Seqlist;

    如图所示:

    动态顺序表的优点:动态顺序表的容量由动态内存函数所开辟,当容量不够使用时可以增加容量capacity, 扩容方法是利用realloc()函数动态开辟一块新的空间(新空间的大小一般为原空间的两倍),若为本地扩容,直接返回原先空间的起始地址,若为异地扩容,首先将旧空间的数据拷贝到新的空间,其次释放旧的空间,最后返回新空间的起始地址

    顺序表的接口实现

    1.顺序表的初始化

    1. //test.c文件
    2. void Test()
    3. {
    4. Seqlist SL;//创建顺序表
    5. InitSeqlist(&SL);//初始化顺序表
    6. }
    7. int main()
    8. {
    9. Test();
    10. return 0;
    11. }
    1. //Seqlist.c文件
    2. //初始化顺序表
    3. void InitSeqlist(Seqlist* ps)
    4. {
    5. assert(ps != NULL);
    6. //开辟一定容量的空间 (*ps).nums成员类型为SLDataType*,malloc返回值类型为void*,所以发生强转
    7. ps->nums = (SLDataType*)malloc(4 * sizeof(SLDataType));
    8. //判断空间开辟是否成功
    9. if (ps->nums == NULL)
    10. {
    11. perror("malloc");
    12. exit(-1);
    13. //exit()函数 exit(0)表示正常退出,exit(x),x不为0表示异常退出,终止正在执行的进程;
    14. }
    15. //空间开辟成功
    16. ps->size = 0;
    17. ps->capacity = 4;//单位大小为sizeof(SLDataType)
    18. }

     调试窗口:

    2.顺序表的销毁

    1. //Seqlist.c文件
    2. //顺序表的销毁
    3. void DestroySeqlist(Seqlist* ps)
    4. {
    5. assert(ps != NULL);
    6. free(ps->nums);
    7. ps->nums = NULL;
    8. ps->size = 0;
    9. ps->capacity = 0;
    10. }

    3.顺序表显示

    1. //Seqlist.c文件
    2. void printSeqlist(Seqlist* ps)
    3. {
    4. assert(ps != NULL);
    5. int i = 0;
    6. for (i = 0; i < ps->size; i++)
    7. {
    8. printf("%d ", ps->nums[i]);
    9. }
    10. printf("\n");
    11. }

    4.顺序表尾插元素

    1. //Seqlist.c文件
    2. void Seqlistpushback(Seqlist* ps, SLDataType x)
    3. {
    4. assert(ps != NULL);
    5. //首先应该判断顺序表是否已满,若空间已满,进行扩容;
    6. //当ps->size=ps->capacity说明空间已满
    7. if (ps->capacity == ps->size)
    8. {
    9. //用realloc()函数进行扩容,出现两种情况 1.同地扩容 2.异地扩容
    10. //必须新建变量接收扩容后的地址变量;
    11. SLDataType* tmp = (SLDataType*)realloc(ps->nums, (ps->capacity) * 2 * sizeof(SLDataType));
    12. if (tmp == NULL)
    13. {
    14. perror("realloc failed");
    15. exit(-1);
    16. }
    17. ps->capacity = (ps->capacity) * 2;
    18. ps->nums = tmp;
    19. }
    20. //扩容成功,尾插元素
    21. //总共有ps->size个有效数据,数组下标范围为0到(ps->size)-1;
    22. ps->nums[ps->size] = x;
    23. ps->size++;
    24. }

    顺序表尾插元素测试

    1. void Test1()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. Seqlistpushback(&SL, 1);
    6. Seqlistpushback(&SL, 2);
    7. Seqlistpushback(&SL, 3);
    8. Seqlistpushback(&SL, 4);
    9. Seqlistpushback(&SL, 5);
    10. printSeqlist(&SL);
    11. DestroySeqlist(&SL);
    12. }
    13. int main()
    14. {
    15. Test1();
    16. return 0;
    17. }

    运行结果:

    5.顺序表尾删元素

    1. void Seqlistpopback(Seqlist* ps)
    2. {
    3. assert(ps != NULL);
    4. //首先检查顺序表中是否可以有元素删除,若不做检查,可能导致指针越界访问;
    5. //检查方式一
    6. /*if (ps->size == 0)
    7. {
    8. return;
    9. }*/
    10. //检查方式二
    11. assert(ps->size > 0);
    12. //尾删元素
    13. ps->size--;
    14. }

    顺序表尾删元素测试

    1. //尾删元素测试
    2. void Test2()
    3. {
    4. Seqlist SL;
    5. InitSeqlist(&SL);
    6. Seqlistpushback(&SL, 1);
    7. Seqlistpushback(&SL, 2);
    8. Seqlistpushback(&SL, 3);
    9. Seqlistpushback(&SL, 4);
    10. Seqlistpushback(&SL, 5);
    11. printSeqlist(&SL);
    12. Seqlistpopback(&SL);
    13. printSeqlist(&SL);
    14. DestroySeqlist(&SL);
    15. }
    16. int main()
    17. {
    18. Test2();
    19. return 0;
    20. }

    运行结果:

    6.顺序表头插元素

    当顺序表头插和尾插元素时,需要检查空间是否足够使用,若空间不够,需要进行扩容,为使代码不要冗余,单独封装为Checkcapacity()函数,方便使用;

    扩容函数的封装

    1. void CheckCapacity(Seqlist* ps)
    2. {
    3. assert(ps != NULL);
    4. //首先应该判断顺序表是否已满,若空间已满,进行扩容;
    5. //当ps->size=ps->capacity说明空间已满
    6. if (ps->capacity == ps->size)
    7. {
    8. //用realloc()函数进行扩容,出现两种情况 1.本地扩容 2.异地扩容
    9. //必须新建变量接收扩容后的地址变量;
    10. SLDataType* tmp = (SLDataType*)realloc(ps->nums, (ps->capacity) * 2 * sizeof(SLDataType));
    11. if (tmp == NULL)
    12. {
    13. perror("realloc failed");
    14. exit(-1);
    15. }
    16. ps->capacity = (ps->capacity) * 2;
    17. ps->nums = tmp;
    18. }
    19. }

    顺序表头插元素的实现

    1. void Seqlistpushfront(Seqlist* ps, SLDataType x)
    2. {
    3. assert(ps != NULL);
    4. CheckCapacity(ps);
    5. //从后向前依次拷贝数据,直至首元素
    6. int end = ps->size - 1;
    7. while (end >= 0)
    8. {
    9. ps->nums[end + 1] = ps->nums[end];
    10. --end;
    11. }
    12. //插入元素
    13. ps->nums[0] = x;
    14. ps->size++;
    15. }

    顺序表头插元素测试

    1. //头插元素测试
    2. void Test3()
    3. {
    4. Seqlist SL;
    5. InitSeqlist(&SL);
    6. Seqlistpushfront(&SL, 1);
    7. Seqlistpushfront(&SL, 2);
    8. Seqlistpushfront(&SL, 3);
    9. Seqlistpushfront(&SL, 4);
    10. Seqlistpushfront(&SL, 5);
    11. printSeqlist(&SL);
    12. DestroySeqlist(&SL);
    13. }
    14. int main ()
    15. {
    16. Test3();
    17. return 0;
    18. }

    运行结果如下:

    7.顺序表头删元素

    1. //顺序表头删元素
    2. void Seqlistpopfront(Seqlist* ps)
    3. {
    4. assert(ps != NULL);
    5. assert(ps->size > 0);
    6. int begin = 1;
    7. while (begin < ps->size)
    8. {
    9. ps->nums[begin - 1] = ps->nums[begin];
    10. ++begin;
    11. }
    12. ps->size--;
    13. }

    顺序表头删元素测试

    1. void Test4()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. Seqlistpushback(&SL, 1);
    6. Seqlistpushback(&SL, 2);
    7. Seqlistpushback(&SL, 3);
    8. Seqlistpushback(&SL, 4);
    9. Seqlistpushback(&SL, 5);
    10. printSeqlist(&SL);
    11. Seqlistpopfront(&SL);
    12. printSeqlist(&SL);
    13. Seqlistpopfront(&SL);
    14. printSeqlist(&SL);
    15. DestroySeqlist(&SL);
    16. }
    17. int main()
    18. {
    19. Test4();
    20. return 0;
    21. }

    运行结果:

    8.顺序表在pos位置插入元素x

    1. void SeqlistInsert(Seqlist* ps, int pos, SLDataType x)
    2. {
    3. assert(ps != NULL);
    4. assert(pos >= 0 && pos <= ps->size);
    5. CheckCapacity(ps);
    6. //从顺序表最后一个元素向前拷贝,向后移动,直到pos位置为止
    7. int end = ps->size - 1;
    8. while (end >= pos)
    9. {
    10. ps->nums[end + 1] = ps->nums[end];
    11. end--;
    12. }
    13. ps->nums[pos] = x;
    14. ps->size++;
    15. }

    顺序表在pos位置插入元素x测试

    1. void Test5()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. Seqlistpushback(&SL, 1);
    6. Seqlistpushback(&SL, 2);
    7. Seqlistpushback(&SL, 3);
    8. Seqlistpushback(&SL, 4);
    9. Seqlistpushback(&SL, 5);
    10. printSeqlist(&SL);
    11. SeqlistInsert(&SL, 3, 20);
    12. printSeqlist(&SL);
    13. DestroySeqlist(&SL);
    14. }
    15. int main()
    16. {
    17. Test5();
    18. return 0;
    19. }

    运行结果:

    9.顺序表查找某元素

    1. //顺序表查找某个元素(查找到该元素返回下标,查找不到返回-1)
    2. int SeqlistFind(Seqlist* ps, SLDataType x)
    3. {
    4. assert(ps != NULL);
    5. int j = 0;
    6. for (j = 0; j < ps->size; j++)
    7. {
    8. if (ps->nums[j] == x)
    9. {
    10. return j;
    11. break;
    12. }
    13. }
    14. //查找不到该元素
    15. return -1;
    16. }

    顺序表查找某元素测试

    1. void Test6()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. Seqlistpushback(&SL, 1);
    6. Seqlistpushback(&SL, 2);
    7. Seqlistpushback(&SL, 3);
    8. Seqlistpushback(&SL, 4);
    9. Seqlistpushback(&SL, 5);
    10. int x;
    11. printf("请输入要查找的元素\n");
    12. scanf("%d", &x);
    13. int pos = SeqlistFind(&SL, x);
    14. if (pos != -1)
    15. {
    16. SeqlistInsert(&SL, pos, x * 10);
    17. printSeqlist(&SL);
    18. }
    19. else
    20. {
    21. printf("查找的元素不存在\n");
    22. }
    23. DestroySeqlist(&SL);
    24. }
    25. int main()
    26. {
    27. Test6();
    28. return 0;
    29. }

    运行结果:

    10.顺序表删除指定位置pos处的值

    1. void SeqlistErase(Seqlist* ps, int pos)
    2. {
    3. assert(ps != NULL);
    4. //首先判断pos位置下标是否合法,避免数组越界访问
    5. assert(pos >= 0 && pos < ps->size);
    6. //坐标合法,从pos下一个位置从前向后拷贝
    7. int begin = pos + 1;
    8. while (beginsize)
    9. {
    10. ps->nums[begin - 1] = ps->nums[begin];
    11. begin++;
    12. }
    13. ps->size--;
    14. }

    顺序表删除指定位置pos处的值测试

    1. void Test7()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. Seqlistpushback(&SL, 1);
    6. Seqlistpushback(&SL, 2);
    7. Seqlistpushback(&SL, 3);
    8. Seqlistpushback(&SL, 4);
    9. Seqlistpushback(&SL, 5);
    10. printSeqlist(&SL);
    11. int x;
    12. printf("请输入要删除的元素\n");
    13. scanf("%d", &x);
    14. int pos = SeqlistFind(&SL, x);
    15. if (pos != -1)
    16. {
    17. SeqlistErase(&SL, pos);
    18. printSeqlist(&SL);
    19. }
    20. else
    21. {
    22. printf("删除的元素不存在\n");
    23. }
    24. DestroySeqlist(&SL);
    25. }
    26. int main()
    27. {
    28. Test7();
    29. return 0;
    30. }

    运行结果:

     11.头插函数改造版本SeqlistPushfront()函数

    利用SeqlistInsert()函数改造头插 ,尾插函数;

    1. //头插函数改造版
    2. void SeqlistPushfront(Seqlist* ps, SLDataType x)
    3. {
    4. SeqlistInsert(ps, 0, x);
    5. }

    SeqlistPushfront()函数测试

    1. void Test8()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. SeqlistPushfront(&SL, 10);
    6. SeqlistPushfront(&SL, 20);
    7. SeqlistPushfront(&SL, 30);
    8. SeqlistPushfront(&SL, 40);
    9. SeqlistPushfront(&SL, 50);
    10. printSeqlist(&SL);
    11. DestroySeqlist(&SL);
    12. }
    13. int main()
    14. {
    15. Test8();
    16. return 0;
    17. }

    运行结果:

    12.尾插函数改造版本SeqlistPushback()函数

    1. void SeqlistPushback(Seqlist* ps, SLDataType x)
    2. {
    3. SeqlistInsert(ps, ps->size, x);
    4. }

    SeqlistPushback()函数测试

    1. void Test9()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. SeqlistPushback(&SL, 11);
    6. SeqlistPushback(&SL, 12);
    7. SeqlistPushback(&SL, 13);
    8. SeqlistPushback(&SL, 14);
    9. SeqlistPushback(&SL, 15);
    10. printSeqlist(&SL);
    11. DestroySeqlist(&SL);
    12. }
    13. int main()
    14. {
    15. Test9();
    16. return 0;
    17. }

    运行结果:

    13.头删函数改造版本SeqlistPopfront()函数

    利用SeqlistErase()函数改造头删 尾删函数;

    1. void SeqlistPopfront(Seqlist* ps)
    2. {
    3. SeqlistErase(ps, 0);
    4. }

     SeqlistPopfront()函数测试

    1. void Test10()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. Seqlistpushback(&SL, 1);
    6. Seqlistpushback(&SL, 2);
    7. Seqlistpushback(&SL, 3);
    8. Seqlistpushback(&SL, 4);
    9. Seqlistpushback(&SL, 5);
    10. printSeqlist(&SL);
    11. SeqlistPopfront(&SL);
    12. printSeqlist(&SL);
    13. DestroySeqlist(&SL);
    14. }
    15. int main()
    16. {
    17. Test10();
    18. return 0;
    19. }

    运行结果:

    14.尾删函数改造版本SeqlistPopback()函数

    1. void Test11()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. Seqlistpushback(&SL, 1);
    6. Seqlistpushback(&SL, 2);
    7. Seqlistpushback(&SL, 3);
    8. Seqlistpushback(&SL, 4);
    9. Seqlistpushback(&SL, 5);
    10. printSeqlist(&SL);
    11. SeqlistPopback(&SL);
    12. printSeqlist(&SL);
    13. SeqlistPopback(&SL);
    14. printSeqlist(&SL);
    15. DestroySeqlist(&SL);
    16. }
    17. int main()
    18. {
    19. Test11();
    20. return 0;
    21. }

    SeqlistPopback()函数测试

    15.顺序表修改pos位置的值

    1. void SeqlistModify(Seqlist* ps, int pos, SLDataType x)
    2. {
    3. assert(ps != NULL);
    4. //检查要修改某个元素的下标是否合法
    5. assert(pos >= 0 && pos < ps->size );
    6. ps->nums[pos] = x;
    7. }

    顺序表修改pos位置的值测试 

    1. void Test12()
    2. {
    3. Seqlist SL;
    4. InitSeqlist(&SL);
    5. Seqlistpushback(&SL, 1);
    6. Seqlistpushback(&SL, 2);
    7. Seqlistpushback(&SL, 3);
    8. Seqlistpushback(&SL, 4);
    9. Seqlistpushback(&SL, 5);
    10. printSeqlist(&SL);
    11. SeqlistModify(&SL, 2, 100);
    12. printSeqlist(&SL);
    13. SeqlistModify(&SL, 2, 200);
    14. printSeqlist(&SL);
    15. DestroySeqlist(&SL);
    16. }
    17. int main()
    18. {
    19. Test12();
    20. return 0;
    21. }

    运行结果:

  • 相关阅读:
    .NET主流的ORM框架 2023年
    通过WSL2搭建Pytorch1.10+CUDA11.4+NVIDIA Driver深度学习框架
    米尔AM62x核心板助力新一代工业4.0升级
    Java深拷贝与浅拷贝
    神经网络模型的基本原理,神经网络模型原理图
    【总结】有三AI所有原创GAN相关的技术文章汇总(2022年8月)
    数据库存储过程
    NGINX源码之:listen和server_name命令与listening监听创建
    屡获大奖的界面控件开发包DevExpress v22.1官宣发布
    ES学习看这一篇文章就够了
  • 原文地址:https://blog.csdn.net/m0_58963318/article/details/133234259