当前位置:   article > 正文

【数据结构】顺序表和链表_链式列表和顺序表

链式列表和顺序表

目录

1、线性表

2、顺序表

2.1 概念和结构

 3、链表

3.1 概念及结构

3.2 链表的种类

3.3 链表的实现

4、顺序表和链表的比较

1、线性表

线性表(linear list)是一种常见的数据结构,它由一组相同类型的数据元素组成,这些元素按照一定的顺序排列。因此线性表是n个具有相同特性的数据元素组成的有限序列

常见的线性表有顺序表、链表、栈、队列、字符串等等。

线性表的常见操作包括插入、删除、查找、遍历等。插入和删除操作需要注意边界条件,查找操作可以用顺序查找或二分查找,遍历操作可以用循环或递归实现。

线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的

线性表可以用数组或链表来实现。数组实现的线性表称为顺序表,链表实现的线性表称为链表。

2、顺序表

2.1 概念和结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存
储,同时记录表的长度和容量。在数组上完成数据的增删查改。

顺序表的特点是随机访问快,插入和删除慢,而链表的特点是随机访问慢,插入和删除快。

顺序表一般可以分为:

  1.  静态顺序表:使用定长数组存储元素。
    1. #define MAX_SIZE 100
    2. typedef struct {
    3. int id;
    4. char name[20];
    5. int age;
    6. } Student;
    7. typedef struct {
    8. Student data[MAX_SIZE];//定长数组
    9. int length; //有效元素个数
    10. } StaticTable;
  2.  动态顺序表:使用动态开辟的数组存储。
     (是指在程序运行时才确定表的大小,表中元素的个数可以动态地改变)
    1. typedef struct {
    2. int *data;
    3. int length;
    4. int capacity;
    5. } DynamicTable;

 2.2 顺序表的实现

静态顺序表只适用于确定知道需要存多少数据的场景。如果不知道需要存多少数据,定义静态顺序表的定长数组时可能导致N定大了或者定小了,定大了浪费空间,定少了空间不够用。所以现实中基本都是使用动态顺序表,根据需要来动态地分配空间大小,所以下面我们实现动态顺序表。

  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. typedef int SLDateType;
  5. typedef struct SeqList
  6. {
  7. SLDateType* a;
  8. int size;
  9. int capacity;
  10. }SL;
  11. void IntSL(SL* psl);//初始化
  12. void DestorySL(SL* psl);//删除
  13. void SLPushBack(SL* psl);//尾插
  14. void SLPrint(SL* psl);//打印
  15. void SLPopback(SL* psl);//尾删
  16. void SLPushFront(SL* psl);//头插
  17. void SLPopFront(SL* psl);//头删
  18. void SLInsert(SL* psl, int pos);//在pos位置插入
  19. void SLDelete(SL* psl, int pos);//在pos位置删除
  20. int SLSearch(SL* psl);//查找
  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "SeqList.h"
  3. void IntSL(SL* psl)//初始化
  4. {
  5. psl->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
  6. if (psl->a == NULL)
  7. {
  8. perror("malloc");
  9. return;
  10. }
  11. psl->capacity = 4;
  12. psl->size = 0;
  13. }
  14. void DestorySL(SL* psl)
  15. {
  16. free(psl->a);
  17. psl->a = NULL;
  18. psl->size = 0;
  19. psl->capacity = 0;
  20. }
  21. void CheckCapacity(SL* psl)
  22. {
  23. if (psl->size == psl->capacity)
  24. {
  25. SLDateType* tmp = (SLDateType*)realloc(psl->a, sizeof(SLDateType) * psl->capacity * 2);
  26. if (tmp == NULL)
  27. {
  28. perror("realloc");
  29. }
  30. psl->a = tmp;
  31. psl->capacity *= 2;
  32. }
  33. }
  34. void SLPushBack(SL* psl)//尾插
  35. {
  36. CheckCapacity(psl);
  37. SLDateType num = 0;
  38. printf("Enter a number:");
  39. scanf("%d", &num);
  40. psl->a[psl->size] = num;
  41. psl->size++;
  42. }
  43. void SLPrint(SL* psl)//打印
  44. {
  45. if (psl->size == 0)
  46. {
  47. printf("The sequence table is empty!\n");
  48. return;
  49. }
  50. for (int i = 0; i < psl->size; i++)
  51. {
  52. printf("%d ", psl->a[i]);
  53. if (i % 10 == 0 && i != 0)
  54. {
  55. printf("\n");
  56. }
  57. }
  58. printf("\n");
  59. }
  60. void SLPopback(SL* psl)//尾删
  61. {
  62. //if (psl->size == 0)
  63. //{
  64. // printf("The sequence table is empty!\n");
  65. // return;
  66. //}
  67. //psl->a[psl->size] = 0;
  68. //psl->size--;
  69. SLDelete(psl, psl->size);
  70. }
  71. void SLInsert(SL* psl, int pos)//在pos位置插入
  72. {
  73. CheckCapacity(psl);
  74. printf("Enter the number to insert:");
  75. int tmp = 0;
  76. scanf("%d", &tmp);
  77. psl->size++;
  78. for (int i = psl->size - 1; i >= pos ; i--)
  79. {
  80. psl->a[i] = psl->a[i - 1];
  81. }
  82. psl->a[pos - 1] = tmp;
  83. }
  84. void SLDelete(SL* psl, int pos)//在pos位置删除
  85. {
  86. if (psl->size == 0)
  87. {
  88. printf("The sequence table is empty!\n");
  89. return;
  90. }
  91. for (int i = pos-1; i < psl->size-1; i++)
  92. {
  93. psl->a[i] = psl->a[i + 1];
  94. }
  95. psl->a[psl->size - 1] = 0;
  96. psl->size--;
  97. }
  98. void SLPushFront(SL* psl)//头插
  99. {
  100. SLInsert(psl, 1);//第一个元素
  101. }
  102. void SLPopFront(SL* psl)//头删
  103. {
  104. SLDelete(psl, 1);//第一个元素
  105. }
  106. int SLSearch(SL* psl)//查找
  107. {
  108. if (psl->size == 0)
  109. {
  110. printf("The sequence table is empty!\n");
  111. return;
  112. }
  113. printf("Enter the value to look for:");
  114. int tmp = 0;
  115. scanf("%d", &tmp);
  116. for (int i = 0; i < psl->size; i++)
  117. {
  118. if (psl->a[i] == tmp)
  119. {
  120. printf("The value's subscript is %d\n", i);
  121. return i;
  122. }
  123. }
  124. printf("The value does not exist\n");
  125. return -1;
  126. }

 3、链表

 3.1 概念及结构

链表是一种动态数据结构,它由一组节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。链表的大小可以动态地改变,可以在表的任意位置插入或删除元素。链表的实现方式是用指针来连接节点。

链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表
中的指针链接次序实现的 。

3.2 链表的种类

  1. 单向或者双向
  2. 带头或者不带头
  3. 循环或者非循环

 以上情况组合起来就有8种链表结构,其中比较常用的是:

  1. 无头单向非循环链表
  2. 带头双向循环链表

 3.3 链表的实现

下面是一个用无头单向非循环链表实现的动态顺序表的例子:

  1. //Slist.h
  2. #pragma once
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. typedef int SLTDateType;
  7. typedef struct SListNode
  8. {
  9. SLTDateType data;
  10. struct SListNode* next;
  11. }SListNode;
  12. // 动态申请一个节点
  13. SListNode* BuySListNode(SLTDateType x);
  14. // 单链表打印
  15. void SListPrint(SListNode* plist);
  16. // 单链表尾插
  17. void SListPushBack(SListNode** pplist, SLTDateType x);
  18. // 单链表的头插
  19. void SListPushFront(SListNode** pplist, SLTDateType x);
  20. // 单链表的尾删
  21. void SListPopBack(SListNode** pplist);
  22. // 单链表头删
  23. void SListPopFront(SListNode** pplist);
  24. // 单链表查找
  25. SListNode* SListFind(SListNode* plist, SLTDateType x);
  26. // 单链表在pos位置之后插入x
  27. // 分析思考为什么不在pos位置之前插入?
  28. void SListInsertAfter(SListNode* pos, SLTDateType x);
  29. //单链表删除pos位置之后的值
  30. // 分析思考为什么不删除pos位置?
  31. void SListEraseAfter(SListNode* pos);
  32. // 单链表的销毁
  33. void SListDestroy(SListNode* plist);
  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "Slist.h"
  3. // 动态申请一个节点
  4. SListNode* BuySListNode(SLTDateType x)
  5. {
  6. SListNode* tmp = (SListNode*)malloc(sizeof(SListNode));
  7. if (tmp == NULL)
  8. {
  9. perror("malloc");
  10. }
  11. else
  12. {
  13. tmp->data = x;
  14. tmp->next = NULL;
  15. return tmp;
  16. }
  17. }
  18. // 单链表打印
  19. void SListPrint(SListNode* plist)
  20. {
  21. assert(plist);
  22. while (plist->data != EOF)
  23. {
  24. printf("%d ", plist->data);
  25. if (plist->next == NULL)
  26. {
  27. return;
  28. }
  29. else
  30. {
  31. plist = plist->next;
  32. }
  33. }
  34. printf("\n");
  35. }
  36. // 单链表尾插
  37. void SListPushBack(SListNode** pplist, SLTDateType x)
  38. {
  39. assert(*pplist);
  40. SListNode* tmp = *pplist;
  41. while (tmp->next != NULL)
  42. {
  43. tmp = tmp->next;
  44. }
  45. tmp->next = BuySListNode(x);
  46. }
  47. // 单链表的头插
  48. void SListPushFront(SListNode** pplist, SLTDateType x)
  49. {
  50. if (*pplist == NULL)
  51. {
  52. SListNode* tmp = BuySListNode(x);
  53. *pplist = tmp;
  54. return;
  55. }
  56. else
  57. {
  58. SListNode* tmp = BuySListNode(x);
  59. tmp->next = *pplist;
  60. *pplist = tmp;
  61. return;
  62. }
  63. }
  64. // 单链表的尾删
  65. void SListPopBack(SListNode** pplist)
  66. {
  67. assert(*pplist);
  68. SListNode* tmp = *pplist;
  69. SListNode* next = tmp->next;
  70. while (next->next != NULL)
  71. {
  72. tmp = tmp->next;
  73. next = next->next;
  74. }
  75. free(next);
  76. tmp->next = NULL;
  77. }
  78. // 单链表头删
  79. void SListPopFront(SListNode** pplist)
  80. {
  81. assert(*pplist);
  82. SListNode* tmp = *pplist;
  83. *pplist = (*pplist)->next;
  84. free(tmp);
  85. }
  86. // 单链表查找
  87. SListNode* SListFind(SListNode* plist, SLTDateType x)
  88. {
  89. assert(plist);
  90. int flag = 0;
  91. while (plist->data != x)
  92. {
  93. if (plist->next != NULL)
  94. {
  95. plist = plist->next;
  96. }
  97. else
  98. {
  99. return NULL;
  100. }
  101. }
  102. return plist;
  103. }
  104. // 单链表在pos位置之后插入x
  105. void SListInsertAfter(SListNode* pos, SLTDateType x)
  106. {
  107. SListNode* tmp = BuySListNode(x);
  108. tmp->next = pos->next;
  109. pos->next = tmp;
  110. }
  111. // 单链表删除pos位置之后的值
  112. void SListEraseAfter(SListNode* pos)
  113. {
  114. SListNode* tmp = pos->next;
  115. pos->next = tmp->next;
  116. free(tmp);
  117. }
  118. // 单链表的销毁
  119. void SListDestroy(SListNode* plist)
  120. {
  121. assert(plist);
  122. if (plist->next == NULL)
  123. {
  124. free(plist);
  125. return;
  126. }
  127. else
  128. {
  129. SListDestroy(plist->next);
  130. free(plist);
  131. return;
  132. }
  133. }

 下面是一个用带头双向循环链表实现的动态顺序表的例子:

  1. //ddlist.h
  2. #pragma once
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. typedef int LTDataType;
  6. typedef struct ListNode
  7. {
  8. LTDataType data;
  9. struct ListNode* next;
  10. struct ListNode* prev;
  11. }ListNode;
  12. // 创建返回链表的头结点.
  13. ListNode* ListCreate();
  14. // 双向链表销毁
  15. void ListDestory(ListNode* pHead);
  16. // 双向链表打印
  17. void ListPrint(ListNode* pHead);
  18. // 双向链表尾插
  19. void ListPushBack(ListNode* pHead, LTDataType x);
  20. // 双向链表尾删
  21. void ListPopBack(ListNode* pHead);
  22. // 双向链表头插
  23. void ListPushFront(ListNode* pHead, LTDataType x);
  24. // 双向链表头删
  25. void ListPopFront(ListNode* pHead);
  26. // 双向链表查找
  27. ListNode* ListFind(ListNode* pHead, LTDataType x);
  28. // 双向链表在pos的前面进行插入
  29. void ListInsert(ListNode* pos, LTDataType x);
  30. // 双向链表删除pos位置的节点
  31. void ListErase(ListNode* pos);
  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "dllist.h"
  3. // 创建返回链表的头结点
  4. ListNode* ListCreate()
  5. {
  6. ListNode* ListHead = (ListNode*)malloc(sizeof(ListNode));
  7. if (ListHead == NULL)
  8. {
  9. perror("malloc error");
  10. }
  11. ListHead->next = ListHead;
  12. ListHead->prev = ListHead;
  13. return ListHead;
  14. }
  15. //创建新节点
  16. ListNode* CreatNode(LTDataType x)
  17. {
  18. ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
  19. if (tmp == NULL)
  20. {
  21. perror("malloc error");
  22. }
  23. tmp->data = x;
  24. tmp->next = NULL;
  25. tmp->prev = NULL;
  26. return tmp;
  27. }
  28. //尾插
  29. void ListPushBack(ListNode* pHead, LTDataType x)
  30. {
  31. ListInsert(pHead, x);
  32. return;
  33. }
  34. //尾删
  35. void ListPopBack(ListNode* pHead)
  36. {
  37. ListErase(pHead->prev);
  38. return;
  39. }
  40. // 双向链表头插
  41. void ListPushFront(ListNode* pHead, LTDataType x)
  42. {
  43. ListInsert(pHead->next, x);
  44. return;
  45. }
  46. // 双向链表头删
  47. void ListPopFront(ListNode* pHead)
  48. {
  49. ListErase(pHead->next);
  50. }
  51. // 双向链表在pos的前面进行插入
  52. void ListInsert(ListNode* pos, LTDataType x)
  53. {
  54. ListNode* pre = pos->prev;
  55. ListNode* Node = CreatNode(x);
  56. Node->prev = pre;
  57. Node->next = pos;
  58. pos->prev = Node;
  59. pre->next = Node;
  60. return;
  61. }
  62. // 双向链表删除pos位置的节点
  63. void ListErase(ListNode* pos)
  64. {
  65. ListNode* pre = pos->prev;
  66. ListNode* next = pos->next;
  67. pre->next = next;
  68. next->prev = pre;
  69. free(pos);
  70. return;
  71. }
  72. //打印
  73. void ListPrint(ListNode* pHead)
  74. {
  75. ListNode* cur = pHead->next;
  76. while (cur != pHead)
  77. {
  78. printf("%d ", cur->data);
  79. cur = cur->next;
  80. }
  81. printf("\n");
  82. return;
  83. }
  84. // 双向链表销毁
  85. void ListDestory(ListNode* pHead)
  86. {
  87. ListNode* cur = pHead->next;
  88. while (cur != pHead)
  89. {
  90. pHead->next = cur->next;
  91. free(cur);
  92. cur = pHead->next;
  93. }
  94. free(pHead);
  95. return;
  96. }
  97. // 双向链表查找
  98. ListNode* ListFind(ListNode* pHead, LTDataType x)
  99. {
  100. ListNode* cur = pHead->next;
  101. while (cur != pHead)
  102. {
  103. if (cur->data == x)
  104. {
  105. return cur;
  106. }
  107. else
  108. {
  109. cur = cur->next;
  110. }
  111. }
  112. return NULL;
  113. }

4、顺序表和链表的比较 

不同点顺序表链表
存储空间
物理上一定连续
逻辑上连续,但物理上不一定 连续
随机访问
支持,O(1)
不支持下标随机访问,O(N)
应用场景
元素高效存储+频繁访问
任意位置插入和删除频繁
缓存利用率
插入
动态顺序表,空间不够时需要 扩容
按需申请空间
删除

可能需要搬移元素,效率低

O(N)

只需修改指针指向
O(1)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/769643
推荐阅读
相关标签
  

闽ICP备14008679号