当前位置:   article > 正文

初阶数据结构(顺序表的实现)

初阶数据结构(顺序表的实现)

1. 线性表

线性表是(linear list)n个具有相同特性的数据元素的有限队列。线性表是一种在实际广泛应用的的数据结构,常见的线性表:顺序表,链表,栈,队列,字符串。。。

线性表在逻辑结构上是连续的。但在物理结构上不一定连续,线性表在物理上存储时,通常以数组和链表的形式存储。

2. 顺序表

2.1 概念与结构

概念:顺序表是用一段物理地址连续的存储单元一次存储数据元素的线性结构,一般采用与数组类似的存储方式。

顺序表与数组的区别

顺序表的底层是数组,对数组进行封装,实现了增删查改等接口

2.2 分类

2.2.1 静态顺序表

概念:使用定长数组进行存储数据的结构

    //静态顺序表
    typedef int SLDataType;
    #define N 7
    typedef struct SeqList
    {
        SLDataType arr[N];// 定长数组
        int size;        //有效数据个数
    }SL;

2.2.2 动态顺序表

//动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
    SLDataType* arr; //动态数组
    int size;        //有效数据个数
    int capacity;    //空间大小
}SL;

2.3 动态顺序表的实现

// 初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
// 扩容
void SLCheckCapacity(SL* ps);
// 头部插⼊删除 / 尾部插⼊删除
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
// 指定位置之前插⼊ / 删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
//查找
int SLFind(SL* ps, SLDataType x);

2.3.1 初始化,销毁,打印

  1. // 初始化
  2. void SeqListInit(SL* ps)
  3. {
  4. ps->arr = NULL;
  5. ps->capacity = ps->size = 0;
  6. }
  7. //销毁
  8. void SeqListDestroy(SL* ps)
  9. {
  10. if (ps->arr!=NULL)
  11. free(ps->arr);
  12. ps->arr = NULL;
  13. free(ps->capacity & ps->size);
  14. ps->capacity = ps->size = 0;
  15. }
  16. //打印
  17. void SeqListPrint(SL* ps)
  18. {
  19. for (int i = 0; i < ps->size; i++)
  20. {
  21. printf("%d->", ps->arr[i]);
  22. }
  23. printf("NULL\n");
  24. }

2.3.2 扩容

  1. void SLcheckCapacity(SL* ps)
  2. {
  3. //插入之前判断空间是否足够
  4. //三目操作符
  5. if (ps->capacity == ps->size)
  6. {
  7. int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
  8. SLDateType* tmp = (SLDateType*)realloc(ps->arr, sizeof(SLDateType) * newcapacity);
  9. if (tmp == NULL)
  10. {
  11. printf("realloc fail!\n");
  12. exit(1);
  13. }
  14. ps->arr = tmp;
  15. ps->capacity = newcapacity;
  16. }
  17. }

2.3.3 头部插⼊删除 / 尾部插⼊删除

  1. //头插
  2. void SeqListPushFront(SL* ps, SLDateType x)
  3. {
  4. SLcheckCapacity(ps);
  5. for (int i = ps->size;i>0; i--)
  6. ps->arr[i] = ps->arr[i - 1];
  7. ps->arr[0] = x;
  8. ps->size++;
  9. }
  10. //头删
  11. void SeqListPopFront(SL* ps)
  12. {
  13. assert(ps);
  14. assert(ps->size);
  15. for (int i = 0; i < ps->size-1; i++)
  16. ps->arr[i] = ps->arr[i + 1];
  17. ps->size--;
  18. }
  19. //尾删
  20. void SeqListPopBack(SL* ps)
  21. {
  22. assert(ps);
  23. assert(ps->size);
  24. ps->size--;
  25. }
  26. //尾插
  27. void SeqListPushBack(SL* ps, SLDateType x)
  28. {
  29. assert(ps);
  30. SLcheckCapacity(ps);//检查容量
  31. ps->arr[ps->size] = x;
  32. ps->size++;
  33. }

2.3.4 指定位置之前插⼊/删除数据

  1. // 顺序表在pos位置插入x
  2. void SeqListInsert(SL* ps, int pos, SLDateType x)
  3. {
  4. SLcheckCapacity(ps);
  5. assert(ps);
  6. assert(pos >= 0 && pos <= ps->size);
  7. for (int i = ps->size; i > pos; i--)
  8. ps->arr[i] = ps->arr[i - 1];
  9. ps->arr[pos] = x;
  10. ps->size++;
  11. }
  12. // 顺序表删除pos位置的值
  13. void SeqListErase(SL* ps, int pos)
  14. {
  15. assert(ps);
  16. assert(ps->size);
  17. for (int i = pos; i<ps->size-1; i++)
  18. {
  19. ps->arr[i] = ps->arr[i + 1];
  20. }
  21. ps->size--;
  22. }

2.3.5 查找

  1. // 顺序表查找
  2. int SeqListFind(SL* ps, SLDateType x)
  3. {
  4. assert(ps);
  5. for (int i = 0; i < ps->size; i++)
  6. {
  7. if (x == ps->arr[i])
  8. return i;
  9. }
  10. return -1;
  11. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/840088
推荐阅读
相关标签
  

闽ICP备14008679号