当前位置:   article > 正文

数据结构-顺序表与链表详解_顺序表和链表

顺序表和链表

目录

 一.顺序表

1.概念及分类

 2.动态顺序表的接口实现

二.链表

1.概念及结构

 3.无头单向非循环链表的接口实现

三.顺序表和链表的区别


 一.顺序表

1.概念及分类

顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素、使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系,采用顺序存储结构的线性表通常称为顺序表。

根据数组位置不同,顺序表一般可以分为:

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

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

 2.动态顺序表的接口实现

静态顺序表一般只适用于数据大小已知的场景。N无法运行中改变,可能会面临着N比较小,存储不够的风险,但是N设的比较大会导致过于浪费,且栈空间一般都是比较宝贵的。所以现实中基本都是使用动态顺序表,根据需要来动态的分配空间大小。

动态顺序表结构体定义:

  1. #define INIT_NUM 10 //初始时数组的大小
  2. #define ADD_NUM 5 //每次增加容量时一次增加的数量
  3. typedef int SLTYPE
  4. typedef struct SequenceList
  5. {
  6. SLTYPE* pSL;
  7. size_t size;
  8. size_t capcity;
  9. }SL;

功能1.初始化该顺序表

  1. //初始化
  2. void SLInit(SL* pSL)
  3. {
  4. SLTYPE* ret = calloc(INIT_NUM, sizeof(SLTYPE));
  5. if (ret)
  6. {
  7. pSL->SLhead = ret;
  8. }
  9. else
  10. {
  11. perror("INIT FAIL");
  12. }
  13. pSL->capcity = 0;
  14. pSL->size = 0;
  15. }

功能2. 判断容量是否允许插入数据,否则扩容

  1. //判断容量是否允许插入数据,否则扩容
  2. int SLCheckCapcity(SL* pSL)
  3. {
  4. if (pSL->capcity == pSL->size)
  5. {
  6. SLTYPE* ret = ralloc((pSL->capcity + ADD_NUM) * sizeof(SLTYPE));
  7. if (ret)
  8. {
  9. pSL->SLhead = ret;
  10. pSL->capcity += ADD_NUM;
  11. return 0; //代表增容成功
  12. }
  13. else
  14. {
  15. perror("PUSH FAIL,ENENOUGH CAPCITY!");
  16. return 1; //代表增容失败
  17. }
  18. }
  19. }

功能3.在数组头部或者尾部插入数据

  1. //头插
  2. void SLPushFront(SL* pSL, SLTYPE x)
  3. {
  4. //判断容量是否允许插入数据,否则扩容
  5. if (SLCheckCapcity(pSL))
  6. {
  7. return;
  8. }
  9. // 0 1 2 3 size-1
  10. for (int i = pSL->size ; i > 0; i--)
  11. {
  12. pSL->SLhead[i] = pSL->SLhead[i-1];
  13. }
  14. pSL->SLhead[0] = x;
  15. pSL->size++;
  16. }
  17. //尾插
  18. void SLPushBack(SL* pSL, SLTYPE x)
  19. {
  20. //判断容量是否允许插入数据,否则扩容
  21. if (SLCheckCapcity(pSL))
  22. {
  23. return;
  24. }
  25. pSL->SLhead[pSL->size] = x;
  26. pSL->size++;
  27. }

功能4.在数组头部或者尾部删除数据

  1. //尾部删除
  2. void SLPopBack(SL* pSL)
  3. {
  4. if (pSL->size == 0)
  5. {
  6. perror("NO DATA TO DELETE");
  7. return;
  8. }
  9. pSL->size--;
  10. }
  11. //头部删除
  12. void SLPopFront(SL* pSL)
  13. {
  14. if (pSL->size == 0)
  15. {
  16. perror("NO DATA TO DELETE");
  17. return;
  18. }
  19. for (int i = 0; i < pSL->size-1; i++)
  20. {
  21. pSL->SLhead[i] = pSL->SLhead[i + 1];
  22. }
  23. pSL->size--;
  24. }

功能5.顺序表打印

  1. //顺序表打印
  2. #define FORMAT "%d "
  3. void SLPrint(SL* pSL)
  4. {
  5. for (int i = 0; i < pSL->size; i++)
  6. {
  7. printf(FORMAT, pSL->SLhead[i]);
  8. }
  9. prinf("\n");
  10. }

功能6.查找指定数据

  1. //查找指定数据
  2. int SLFindIndex(SL* pSL, SLTYPE x)
  3. {
  4. for (int i = 0; i < pSL->size; i++)
  5. {
  6. if (pSL->SLhead[i] == x)
  7. return i;
  8. }
  9. return -1;
  10. }

功能7.在指定下标位置插入删除数据

  1. //在指定下标位置插入数据
  2. void SLInsertIndex(SL* pSL,size_t index ,SLTYPE x)
  3. {
  4. if (indx > pSL->size)
  5. {
  6. printf("INVAILD INDEX!");
  7. return;
  8. }
  9. if (SLCheckCapcity(pSL))
  10. {
  11. return;
  12. }
  13. for (int i = pSL->size; i > index; i--)
  14. {
  15. pSL->SLhead[i] = pSL->SLhead[i - 1];
  16. }
  17. pSL->SLhead[index] = x;
  18. pSL->size++;
  19. }
  20. //在指定下标位置删除数据
  21. void SLInsertIndex(SL* pSL, size_t index )
  22. {
  23. if (indx >= pSL->size)
  24. {
  25. printf("INVAILD INDEX!");
  26. return;
  27. }
  28. if (pSL->size == 0)
  29. {
  30. printf("NO DATA TO DELETE");
  31. return;
  32. }
  33. for (int i = index; i < pSL->size-1; i++)
  34. {
  35. pSL->SLhead[i] = pSL->SLhead[i + 1];
  36. }
  37. pSL->size--;
  38. }

功能8.销毁释放顺序表

  1. // 数据销毁
  2. void SLDestroy(SL* pSL)
  3. {
  4. free(pSL->SLhead);
  5. pSL->size = 0;
  6. pSL->capcity = 0;
  7. }

二.链表

1.概念及结构

链表是一种物理存储结构上非连续、非顺序的存储结构,但是逻辑上是顺序的。数据元素的逻辑顺序是通过链表 中的指针链接次序实现的 。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:

一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

 2.分类

实际中为了拓展链表功能,它的的结构非常多样,按照不同分类依据主要可以分成以下几种基础结构,其中每一种还可以组合在一起构成新结构。

(1)单向或者双向

指针域里指针个数不同,有的可以增加指针来指向前一个节点地址。

(2) 带头或者不带头

不带头的链表,链表第一个节点不存放元素方。带头的链表,链表首节点开始存放第一个元素。​ (3)循环或者非循环

 (4)多种结合

 3.无头单向非循环链表的接口实现

结构体定义:

  1. typedef int SLLDataType;
  2. #define FORMAT "%d "
  3. typedef struct SLLNode
  4. {
  5. SLLDataType data;
  6. struct SLLNode* next;
  7. }SLLNode;

功能1.生成新节点以及传入数据一次性生成一个链表

  1. //创建节点
  2. SLLNode* CreateNode(SLLDataType x)
  3. {
  4. SLLNode* p = (SLLNode*)malloc(sizeof(SLLNode));
  5. if (p)
  6. {
  7. p->data = x;
  8. p->next = NULL;
  9. return p;
  10. }
  11. else
  12. {
  13. perror("malloc fail");
  14. return NULL;
  15. }
  16. }
  17. //一次初始化n个节点
  18. SLLNode* CreateSLL(int n,SLLDataType* datas)
  19. {
  20. SLLNode* phead = NULL, *ptail = NULL;
  21. for (int i = 0; i < n; i++)
  22. {
  23. SLLNode* ret = CreateNode(datas[i]);
  24. if (ret)
  25. {
  26. if (i == 0)
  27. {
  28. phead = ptail = ret;
  29. }
  30. else
  31. {
  32. ptail->next = ret;
  33. ptail = ret;
  34. }
  35. }
  36. else
  37. {
  38. perror("create fail");
  39. return NULL;
  40. }
  41. }
  42. return phead;
  43. }

功能2.打印链表信息

  1. //打印
  2. void SLLPrint(SLLNode* pSLL)
  3. {
  4. if (pSLL == NULL)
  5. {
  6. printf("NULL\n");
  7. return;
  8. }
  9. SLLNode* cur = pSLL;
  10. while (cur)
  11. {
  12. printf(FORMAT, cur->data);
  13. cur = cur->next;
  14. }
  15. printf("\n");
  16. }

功能3.添加尾部节点或者删除

  1. //尾插
  2. void SLLPushBack(SLLNode** ppSLL, SLLDataType data)
  3. {
  4. SLLNode* ptail = *ppSLL;
  5. SLLNode* pnode = CreateNode(data);
  6. if (!pnode)
  7. {
  8. perror("malloc fail\n");
  9. return;
  10. }
  11. if (ptail == NULL)
  12. {
  13. *ppSLL = pnode;
  14. }
  15. else
  16. {
  17. while (ptail->next)
  18. {
  19. ptail = ptail->next;
  20. }
  21. ptail->next = pnode;
  22. }
  23. }
  24. //尾删
  25. void SLLPopBack(SLLNode** ppSLL)
  26. {
  27. if (*ppSLL == NULL)
  28. {
  29. perror("NO DATA TO DELETE");
  30. return;
  31. }
  32. else
  33. {
  34. if ((*ppSLL)->next == NULL)
  35. {
  36. free(*ppSLL);
  37. *ppSLL = NULL;
  38. return;
  39. }
  40. else
  41. {
  42. SLLNode* pre = *ppSLL;
  43. SLLNode* ptail = pre->next;
  44. while (ptail->next)
  45. {
  46. ptail=ptail->next;
  47. pre = pre->next;
  48. }
  49. pre->next = NULL;
  50. free(ptail);
  51. }
  52. }
  53. }

功能4.添加头部节点或者删除

  1. //头插
  2. void SLLPushFront(SLLNode** ppSLL, SLLDataType data)
  3. {
  4. SLLNode* pnode = CreateNode(data);
  5. if (!pnode)
  6. {
  7. perror("malloc fail\n");
  8. return;
  9. }
  10. pnode->next = *ppSLL;
  11. *ppSLL = pnode;
  12. }
  13. //头删
  14. void SLLPopFront(SLLNode** ppSLL)
  15. {
  16. if (*ppSLL == NULL)
  17. {
  18. perror("NO DATD TO DELETE!");
  19. return;
  20. }
  21. else if ((*ppSLL)->next == NULL)
  22. {
  23. free(*ppSLL);
  24. *ppSLL = NULL;
  25. }
  26. else
  27. {
  28. SLLNode* next = (*ppSLL)->next;
  29. free(*ppSLL);
  30. *ppSLL = next;
  31. }
  32. }

功能5.链表查找节点

  1. // 单链表查找
  2. SLLNode* SLLFind(SLLNode* pSLL, SLLDataType data)
  3. {
  4. if (!pSLL)
  5. {
  6. return NULL;
  7. }
  8. SLLNode* cur = pSLL;
  9. while (cur)
  10. {
  11. if (cur->data == data)
  12. {
  13. return cur;
  14. }
  15. cur = cur->next;
  16. }
  17. return NULL;
  18. }

功能6.在特定的节点位置删除或者假如元素

  1. // 单链表在pos位置之后插入x
  2. void SLLInsertAfter(SLLNode* pos, SLLDataType data)
  3. {
  4. if (!pos)
  5. {
  6. perror("POS INVAILD!");
  7. return;
  8. }
  9. if (CreateNode(data))
  10. {
  11. SLLNode* newnode = CreateNode(data);
  12. newnode->next = pos->next;
  13. pos->next = newnode;
  14. }
  15. else
  16. {
  17. perror("CREATE NODE FAIL!");
  18. return;
  19. }
  20. }
  21. // 单链表在pos位置之前插入x
  22. void SLLInsertBefore(SLLNode** ppSLL, SLLNode* pos, SLLDataType data)
  23. {
  24. if (!pos)
  25. {
  26. perror("POS INVAILD!");
  27. return;
  28. }
  29. if (*ppSLL == pos)
  30. {
  31. SLLPushFront(ppSLL, data);
  32. return;
  33. }
  34. SLLNode* pre = *ppSLL;
  35. while (pre->next != pos)
  36. {
  37. pre = pre->next;
  38. }
  39. if (CreateNode(data))
  40. {
  41. SLLNode* newnode = CreateNode(data);
  42. newnode->next = pos;
  43. pre->next = newnode;
  44. }
  45. else
  46. {
  47. perror("CREATE NODE FAIL!");
  48. return;
  49. }
  50. }
  51. // 单链表删除pos位置之后的值
  52. void SLLEraseAfter(SLLNode* pos)
  53. {
  54. if (!pos|| pos->next == NULL)
  55. {
  56. perror("POS INVAILD!");
  57. return;
  58. }
  59. SLLNode* ret = pos->next;
  60. pos->next = ret->next;
  61. free(ret);
  62. }
  63. // 单链表删除pos位置当前的值
  64. void SLLErase(SLLNode** ppSLL, SLLNode* pos)
  65. {
  66. if (!pos)
  67. {
  68. perror("POS INVAILD!");
  69. return;
  70. }
  71. if (*ppSLL == pos)
  72. {
  73. SLLPopFront(ppSLL);
  74. return;
  75. }
  76. else
  77. {
  78. SLLNode* pre = *ppSLL;
  79. while (pre->next != pos)
  80. {
  81. pre = pre->next;
  82. }
  83. pre->next = pos->next;
  84. free(pos);
  85. }
  86. }

功能7.销毁数据

  1. // 单链表的销毁
  2. void SListDestroy(SLLNode** ppSLL)
  3. {
  4. SLLNode* cur = *ppSLL;
  5. while (cur)
  6. {
  7. SLLNode* ret = cur->next;
  8. free(cur);
  9. cur = ret;
  10. }
  11. *ppSLL = NULL;
  12. }

  4.带头双向循环链表的接口实现

结构体和功能声明 

  1. typedef int DLLType;
  2. #define FORMAT "%d "
  3. typedef struct DoubleLinkedListNode
  4. {
  5. struct DoubleLinkedListNode* pre;
  6. DLLType val;
  7. struct DoubleLinkedListNode* next;
  8. }DLLNode;
  9. //初始化
  10. void DLLInit(DLLNode* guard);
  11. //创建节点
  12. DLLNode* CreateNode(DLLType x);
  13. //打印
  14. void DLLPrint(DLLNode* guard);
  15. // 查找
  16. DLLNode* DLLFind(DLLNode* guard, DLLType data);
  17. // 在pos位置之后插入x
  18. void DLLInsertAfter(DLLNode* pos, DLLType data);
  19. // 在pos位置之前插入x
  20. void DLLInsertBefore(DLLNode* pos, DLLType data);
  21. //头插
  22. void DLLPushFront(DLLNode* guard, DLLType data);
  23. //尾插
  24. void DLLPushBack(DLLNode* guard, DLLType data);
  25. // 删除pos位置之后的值
  26. void DLLEraseAfter(DLLNode* gaurd, DLLNode* pos);
  27. // 删除pos位置当前的值
  28. void DLLErase(DLLNode* gaurd, DLLNode* pos);
  29. //尾删
  30. void DLLPopBack(DLLNode* gaurd);
  31. //头删
  32. void DLLPopFront(DLLNode* gaurd);
  33. // 单链表的销毁
  34. void DListDestroy(DLLNode* gaurd);

功能实现

  1. //初始化
  2. void DLLInit(DLLNode* guard)
  3. {
  4. guard->next = guard, guard->pre = guard;
  5. }
  6. //创建节点
  7. DLLNode* CreateNode(DLLType x)
  8. {
  9. DLLNode* ret = malloc(sizeof(DLLNode));
  10. assert(ret);
  11. ret->val = x;
  12. ret->next = NULL, ret->pre = NULL;
  13. return ret;
  14. }
  15. //打印
  16. void DLLPrint(DLLNode* guard)
  17. {
  18. if (guard == NULL)
  19. {
  20. printf("NULL");
  21. return;
  22. }
  23. DLLNode* ret = guard->next;
  24. while (ret != guard)
  25. {
  26. printf(FORMAT, ret->val);
  27. ret = ret->next;
  28. }
  29. printf("\n");
  30. }
  31. // 查找
  32. DLLNode* DLLFind(DLLNode* guard, DLLType data)
  33. {
  34. assert(guard);
  35. DLLNode* ret = guard->next;
  36. while (ret != guard)
  37. {
  38. if (ret->val == data)
  39. {
  40. return ret;
  41. }
  42. ret = ret->next;
  43. }
  44. return NULL;
  45. }
  46. // 在pos位置之后插入x
  47. void DLLInsertAfter(DLLNode* pos, DLLType data)
  48. {
  49. DLLNode* ret=CreateNode(data);
  50. ret->next = pos->next;
  51. pos->next->pre = ret;
  52. pos->next = ret;
  53. ret->pre = pos;
  54. }
  55. // 在pos位置之前插入x
  56. void DLLInsertBefore(DLLNode* pos, DLLType data)
  57. {
  58. DLLNode* ret = CreateNode(data);
  59. ret->pre = pos->pre;
  60. pos->pre->next = ret;
  61. ret->next = pos;
  62. pos->pre = ret;
  63. }
  64. //头插
  65. void DLLPushFront(DLLNode* guard, DLLType data)
  66. {
  67. DLLInsertAfter(guard,data);
  68. }
  69. //尾插
  70. void DLLPushBack(DLLNode* guard, DLLType data)
  71. {
  72. DLLInsertBefore(guard, data);
  73. }
  74. // 删除pos位置之后的值
  75. void DLLEraseAfter(DLLNode* gaurd, DLLNode* pos)
  76. {
  77. assert(pos->next != gaurd);
  78. DLLNode* ret = pos->next;
  79. pos->next = ret->next;
  80. ret->next->pre = pos;
  81. free(ret);
  82. }
  83. // 删除pos位置当前的值
  84. void DLLErase(DLLNode* gaurd, DLLNode* pos)
  85. {
  86. assert(pos != gaurd);
  87. pos->next->pre = pos->pre;
  88. pos->pre->next = pos->next;
  89. free(pos);
  90. }
  91. //尾删
  92. void DLLPopBack(DLLNode* gaurd)
  93. {
  94. assert(gaurd->pre);
  95. DLLErase(gaurd, gaurd->pre);
  96. }
  97. //头删
  98. void DLLPopFront(DLLNode* gaurd)
  99. {
  100. assert(gaurd->next);
  101. DLLErase(gaurd, gaurd->next);
  102. }
  103. // 链表的销毁
  104. void DListDestroy(DLLNode* gaurd)
  105. {
  106. DLLNode* ret = gaurd->next;
  107. DLLNode* tmp = gaurd->next;
  108. while (ret != gaurd)
  109. {
  110. tmp=ret->next;
  111. free(ret);
  112. ret = tmp;
  113. }
  114. free(gaurd);
  115. }

 

三.顺序表和链表的区别

不同点顺序表链表
存储空间上物理上一定连续逻辑上连续,但物理上不一定
元素的删除插入效率需要移动大量数据,时间复杂度为O(N)只需要改变各节点的指向关系时间复杂度为O(1)
容量问题增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。只需要创建节点再将其链接进入链表即可
空间利用问题会有所需数据太多开辟不了的的问题,也有可能造成浪费不会有顺序表可能的问题
随机访问效率可以通过下标直接访问,实现O(1)只能挨个遍历,复杂度O(N)
应用场景元素高效存储+频繁访问任意位置插入和删除频繁
缓存利用率

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

闽ICP备14008679号