当前位置:   article > 正文

数据结构之顺序表的基本操作

数据结构之顺序表的基本操作

搭配食用更佳哦~~

数据结构之顺顺顺——顺序表-CSDN博客

1.定义一个动态顺序表

创建一个头文件 SeqList.h,进行准备工作

  1. #pragma once
  2. #define _CRT_SECURE_NO_WARNINGS 1
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<assert.h>
  6. #include"Contact.h"
  7. //定义顺序表的结构
  8. //动态顺序表
  9. typedef struct SeqList
  10. {
  11. SLDataType* arr;
  12. int size; //有效数据个数
  13. int capacity; //空间大小
  14. }SL;

 添加上要实现的方法:

  1. //顺序表初始化
  2. void SLInit(SL* ps);
  3. //顺序表的销毁
  4. void SLDestroy(SL* ps);
  5. void SLPrint(SL s);
  6. //头部插入删除 / 尾部插入删除
  7. void SLPushBack(SL* ps, SLDataType x);
  8. void SLPushFront(SL* ps, SLDataType x);
  9. void SLPopBack(SL* ps);
  10. void SLPopFront(SL* ps);
  11. //指定位置之前插入/删除/查找数据
  12. void SLInsert(SL* ps, int pos, SLDataType x);
  13. void SLErase(SL* ps, int pos);
  14. int SLFind(SL* ps, SLDataType x);

 2.实现基本操作

 创建一个 .c 文件

2.1顺序表的初始化 

  1. #include"SeqList.h"
  2. void SLInit(SL* ps)
  3. {
  4. ps->arr = NULL;
  5. ps->size = ps->capacity = 0;
  6. }

 2.2顺序表的销毁

  1. //顺序表的销毁
  2. void SLDestroy(SL* ps)
  3. {
  4. if (ps->arr) //等价于 if(ps->arr != NULL)
  5. {
  6. free(ps->arr);
  7. }
  8. ps->arr = NULL;
  9. ps->size = ps->capacity = 0;
  10. }

 2.5顺序表空间申请

  1. void SLCheckCapacity(SL* ps)
  2. {
  3. //插入数据之前先看空间够不够
  4. if (ps->capacity == ps->size)
  5. {
  6. //申请空间
  7. //malloc calloc realloc int arr[100] --->增容realloc
  8. //三目表达式
  9. int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
  10. SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));//要申请多大的空间
  11. if (tmp == NULL)
  12. {
  13. perror("realloc fail!");
  14. exit(1);//直接退出程序,不再继续执行
  15. }
  16. //空间申请成功
  17. ps->arr = tmp;
  18. ps->capacity = newCapacity;
  19. }
  20. }

2.4顺序表的头插和尾插

  1. //尾插
  2. void SLPushBack(SL* ps, SLDataType x)
  3. {
  4. 温柔的解决方式
  5. //if (ps == NULL)
  6. //{
  7. // return;
  8. //}
  9. assert(ps); //等价与assert(ps != NULL)
  10. //ps->arr[ps->size] = x;
  11. //++ps->size;
  12. SLCheckCapacity(ps);
  13. ps->arr[ps->size++] = x;
  14. }
  15. //头插
  16. void SLPushFront(SL* ps, SLDataType x)
  17. {
  18. assert(ps);
  19. SLCheckCapacity(ps);
  20. //先让顺序表中已有的数据整体往后挪动一位
  21. for (int i = ps->size; i > 0; i--)
  22. {
  23. ps->arr[i] = ps->arr[i - 1];//arr[1] = arr[0]
  24. }
  25. ps->arr[0] = x;
  26. ps->size++;
  27. }

2.5顺序表指定位置之前插入数据

  1. //在指定位置之前插入数据
  2. void SLInsert(SL* ps, int pos, SLDataType x)
  3. {
  4. assert(ps);
  5. assert(pos >= 0 && pos <= ps->size);
  6. //插入数据:空间够不够
  7. //申请空间
  8. SLCheckCapacity(ps);
  9. //让pos及其之后的数据整体往后移动
  10. for (int i = ps->size; i > pos ; i--)
  11. {
  12. ps->arr[i] = ps->arr[i - 1];//arr[pos+1]=arr[pos]
  13. }
  14. ps->arr[pos] = x;
  15. ps->size++;
  16. }

2.6顺序表指定位置删除数据

  1. //删除指定位置数据
  2. void SLErase(SL* ps, int pos)
  3. {
  4. assert(ps);
  5. assert(pos >= 0 && pos < ps->size);
  6. for (int i = 0; i < ps-> size-1 ; i++)
  7. {
  8. ps->arr[i] = ps->arr[i + 1];//当arr[size-2]等于arr[size-1]的时候
  9. }
  10. ps->size--;
  11. }

2.7顺序表查找数据

  1. 顺序表的查找
  2. int SLFind(SL* ps, SLDataType x)
  3. {
  4. assert(ps);
  5. for (int i = 0; i < ps->size; i++)
  6. {
  7. if (ps->arr[i] == x)
  8. {
  9. //找到啦
  10. return i;
  11. }
  12. }
  13. //没有找到返回-1
  14. return -1;
  15. }

       当我们可以使用这些基本操作后,我们就可以将里面的字符数字替换成结构体,来实现更复杂的操作~~~例如通讯录的实现~~

    本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/508004

推荐阅读
相关标签