马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
目次
1. 线性表
2. 序次表
2.1 概念及结构
2.1.1 静态序次表(不常用)
2.1.2 动态序次表(常用)
编辑
2.2 训练
2.2.1 移除元素
2.2.2 删除有序数组中的重复项
2.2.3 归并两个有序数组
2.3 序次表存在的标题
3. 链表
3.1 概念及结构
3.2 链表的分类
编辑
3.2.1 常用链表
3.2.1.1 无头单向非循环链表
3.2.1.2 带头双向循环链表
3.3 无头单向非循环链表的实现
3.4 带头双向循环链表的实现
4. 对比序次表和链表
💬 :如果你在阅读过程中有任何疑问或想要进一步探究的内容,接待在批评区各抒己见!我们一起学习、共同发展~!
👍 :如果你以为这篇文章还不错,不妨顺手点个赞、到场收藏,并分享给更多的朋侪噢~!
1. 线性表
线性表(linear list)是 n 个具有雷同特性的数据元素的有限序列。它是一种在实际中广泛利用的数据结构。
常见的线性表包罗:序次表、链表、栈、队列、字符串等。
线性表在逻辑上是线性结构,即一条连续的直线。但是在物理结构上并不愿定是连续的,线性表在物理上存储时,通常以数组和链式结构的情势存储。
2. 序次表
2.1 概念及结构
序次表是用一段物理所在连续的存储单元依次存储数据元素的线性结构,一样寻常接纳数组存储。在数组上完成数据的增删查改。
序次表一样寻常可以分为:静态序次表,动态序次表。
2.1.1 静态序次表(不常用)
静态序次表利用定长数组存储元素。存储数据前,必要预先确定命组的长度,且在步伐运行过程中数组长度不能改变。
示例:
- #include <stdio.h>
- #define N 7
- // int 类型重命名为 SLDataType
- typedef int SLDataType;
- // 定义静态顺序表结构体
- typedef struct SeqList // 此结构体类型起了一个别名 SeqList
- {
- SLDataType array[N]; // 定长数组
- size_t size;
- // size_t 用于表示无符号整数类型,通常用于表示数组的索引、对象的大小等
- // size :当前有效数据的个数
- } SeqList;
- void SeqListInit(SeqList* psl)
- {
- psl->size = 0;
- }
- // 向静态顺序表尾部插入数据
- int SeqListPushBack(SeqList* psl, SLDataType x)
- {
- if (psl->size >= N)
- {
- printf("顺序表已满,无法插入\n");
- return 0;
- }
- psl->array[psl->size] = x;
- psl->size++; // 表示成功插入了一个数据
- return 1;
- }
- // 打印静态顺序表
- void SeqListPrint(SeqList* psl)
- {
- for (size_t i = 0; i < psl->size; i++)
- {
- printf("%d ", psl->array[i]);
- }
- printf("\n");
- }
- int main()
- {
- SeqList sl;
- // 声明一个SeqList类型的变量sl,用于表示一个静态顺序表
- SeqListInit(&sl);
- SeqListPushBack(&sl, 1);
- SeqListPushBack(&sl, 2);
- SeqListPushBack(&sl, 3);
- SeqListPrint(&sl);
- return 0;
- }
复制代码
2.1.2 动态序次表(常用)
动态序次表利用动态开辟的数组存储元素。可根据实际必要动态地增减数组的容量,以顺应数据量的变革。
示例:
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- typedef int SLDataType;
- // int 类型重命名为 SLDataType
- typedef struct SeqList // 此结构体类型起了一个别名 SeqList
- {
- SLDataType* array; // 指向动态开辟的数组
- size_t size; // 顺序表中有效数据的个数
- size_t capacity; // 顺序表当前容量大小
- } SeqList;
- // 顺序表初始化
- void SeqListInit(SeqList* psl)
- {
- // 检查传入的顺序表指针是否为空,若为空则终止程序
- assert(psl);
- // 初始化时先不分配空间,将数组指针置为 NULL
- psl->array = NULL;
-
- psl->size = 0;
- psl->capacity = 0;
- }
- // 检查空间,如果满了,进行增容
- void CheckCapacity(SeqList* psl)
- {
- assert(psl);
- // 当有效数据个数等于容量时,说明顺序表已满,需要增容
- if (psl->size == psl->capacity)
- {
- // 若当前容量为 0,先给一个初始容量,这里设为 4
- size_t newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
- // 重新分配内存空间,大小为新容量乘以每个数据元素的大小
- SLDataType* tmp = (SLDataType*)realloc(psl->array, newCapacity * sizeof(SLDataType));
- // 检查内存分配是否成功,若失败则终止程序
- assert(tmp);
- // 更新数组指针为新分配的内存地址
- psl->array = tmp;
-
- psl->capacity = newCapacity;
- }
- }
- // 顺序表尾插
- void SeqListPushBack(SeqList* psl, SLDataType x)
- {
- assert(psl);
- // 插入元素前先检查是否需要增容
- CheckCapacity(psl);
- // 在顺序表的末尾插入新元素
- psl->array[psl->size] = x;
-
- // 有效数据个数加 1,表示插入一个元素
- psl->size++;
- }
- // 顺序表尾删
- void SeqListPopBack(SeqList* psl)
- {
- assert(psl);
- // 检查顺序表中是否有数据,若没有则终止程序
- assert(psl->size > 0);
- // 表示删除一个元素
- psl->size--;
- }
- // 顺序表头插
- void SeqListPushFront(SeqList* psl, SLDataType x)
- {
- assert(psl);
- // 插入元素前先检查是否需要增容
- CheckCapacity(psl);
- // 将顺序表中所有元素依次向后移动一位
- for (int i = psl->size; i > 0; i--)
- {
- psl->array[i] = psl->array[i - 1];
- }
- psl->array[0] = x;
-
- psl->size++;
- }
- // 顺序表头删
- void SeqListPopFront(SeqList* psl)
- {
- assert(psl);
- // 检查顺序表中是否有数据,若没有则终止程序
- assert(psl->size > 0);
- // 将顺序表中所有元素依次向前移动一位
- for (int i = 0; i < psl->size - 1; i++)
- {
- psl->array[i] = psl->array[i + 1];
- }
-
- psl->size--;
- }
- // 顺序表查找
- int SeqListFind(SeqList* psl, SLDataType x)
- {
- assert(psl);
-
- for (size_t i = 0; i < psl->size; i++)
- {
- // 若找到与目标元素相等的元素,返回其下标
- if (psl->array[i] == x)
- {
- return (int)i;
- }
- }
-
- return -1;
- }
- // 顺序表在 pos 位置插入 x
- void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
- {
- assert(psl);
- // 检查插入位置是否合法,即不能小于 0 且不能大于当前有效数据个数
- assert(pos >= 0 && pos <= psl->size);
- // 插入元素前先检查是否需要增容
- CheckCapacity(psl);
- // 将 pos 位置及之后的元素依次向后移动一位
- for (size_t i = psl->size; i > pos; i--)
- {
- psl->array[i] = psl->array[i - 1];
- }
-
- psl->array[pos] = x;
-
- psl->size++;
- }
- // 顺序表删除 pos 位置的值
- void SeqListErase(SeqList* psl, size_t pos)
- {
- assert(psl);
- assert(pos >= 0 && pos < psl->size);
-
- for (size_t i = pos; i < psl->size - 1; i++)
- {
- psl->array[i] = psl->array[i + 1];
- }
-
- psl->size--;
- }
- // 顺序表销毁
- void SeqListDestory(SeqList* psl)
- {
- assert(psl);
- // 释放动态分配的数组内存
- free(psl->array);
- // 将数组指针置为 NULL,防止悬空指针
- psl->array = NULL;
-
- psl->size = 0;
-
- psl->capacity = 0;
- }
- void SeqListPrint(SeqList* psl)
- {
- assert(psl);
-
- for (size_t i = 0; i < psl->size; i++)
- {
- printf("%d ", psl->array[i]);
- }
- printf("\n");
- }
- int main()
- {
- SeqList sl;
- // 声明一个SeqList类型的变量sl,用于表示一个动态顺序表
- // 表尾插
- SeqListInit(&sl);
- SeqListPushBack(&sl, 1);
- SeqListPushBack(&sl, 2);
- SeqListPushBack(&sl, 3);
- SeqListPrint(&sl);
- // 表头插
- SeqListPushFront(&sl, 0);
- SeqListPrint(&sl);
- // 查找
- int index = SeqListFind(&sl, 2);
- if (index != -1)
- {
- printf("元素 2 在顺序表中的下标是: %d\n", index);
- }
- else
- {
- printf("未找到元素 2\n");
- }
- // 表尾删
- SeqListPopBack(&sl);
- SeqListPrint(&sl);
- // 表头删
- SeqListPopFront(&sl);
- SeqListPrint(&sl);
- // 指定位置插入值
- SeqListInsert(&sl, 1, 10);
- SeqListPrint(&sl);
- // 指定位置删除值
- SeqListErase(&sl, 1);
- SeqListPrint(&sl);
- // 顺序表销毁
- SeqListDestory(&sl);
- return 0;
- }
复制代码
2.2 训练
2.2.1 移除元素
有一个数组和一个值 val,要求:
- 原地移除全部便是 val 的元素
- 返回数组中未便是 val (剩余)的元素的数目 k
- 新数组前 k 个元素包罗未便是 val 的元素
“原地”:仅利用常数级别的额外空间,即空间复杂度为 O(1)。其算法直接在原始数据存储的内存位置上举行修改,而不是创建一个与原始数据规模相当的新数据结构来存储处置惩罚后的结果。
- #include <stdio.h>
- int removeElement(int* nums, int numsSize, int val)
- {
- int k = 0;
-
- for (int i = 0; i < numsSize; i++)
- {
- if (nums[i] != val)
- {
- // 如果不等于 val,则将该元素放到数组的前 k 个位置
- nums[k] = nums[i];
-
- k++;
- }
- }
-
- return k;
- }
- int main()
- {
- int nums[] = {3, 2, 2, 3};
- int numsSize = sizeof(nums) / sizeof(nums[0]);
- int val = 3;
- int newSize = removeElement(nums, numsSize, val);
- printf("剩余元素的数量: %d\n", newSize);
-
- printf("移除指定元素后的数组: ");
- for (int i = 0; i < newSize; i++)
- {
- printf("%d ", nums[i]);
- }
- printf("\n");
- return 0;
- }
复制代码
只遍历数组一次,以是时间复杂度 O(n) ,n 为数组长度。
2.2.2 删除有序数组中的重复项
原地删除一个非严格递增分列的数组 nums 中重复的元素,使每个元素只出现一次,保持元素的相对序次稳固,返回新数组的长度 k ,新数组前 k 个元素包罗唯一元素。
- #include <stdio.h>
- int removeDuplicates(int* nums, int numsSize)
- {
- if (numsSize == 0)
- {
- return 0;
- }
- int slow = 0;
- for (int fast = 1; fast < numsSize; fast++)
- {
- if (nums[fast] != nums[slow])
- {
- slow++;
- nums[slow] = nums[fast];
- }
- }
- return slow + 1;
- }
- int main()
- {
- int nums[] = {1, 1, 2, 2, 2, 3, 4, 4, 5};
- int numsSize = sizeof(nums) / sizeof(nums[0]);
- int newLength = removeDuplicates(nums, numsSize);
- printf("删除重复元素后数组的新长度: %d\n", newLength);
- printf("删除重复元素后的数组: ");
- for (int i = 0; i < newLength; i++)
- {
- printf("%d ", nums[i]);
- }
- printf("\n");
- return 0;
- }
复制代码 只遍历数组一次,以是时间复杂度 O(n) ,n 为数组长度。
2.2.3 归并两个有序数组
给定两个非递减排序的整数数组 nums1 和 nums2,及整数 m 和 n。
要求:
- nums1 初始长度是 m + n,前 m 个是待归并元素,后 n 个为 0 ;
- nums2 初始长度为 n;
- nums2 归并到 nums1 里,归并后数组保持非递减序次,归并后结果存于 nums1 中。
- #include <stdio.h>
- void merge(int* nums1, int m, int* nums2, int n)
- {
- int p1 = m - 1; // nums1 中最后一个“有效”元素的索引
- int p2 = n - 1; // nums2 中最后一个元素的索引
- int p = m + n - 1; // 合并后数组最后一个元素的索引
- // 从后往前比较两个数组的元素
- while (p1 >= 0 && p2 >= 0)
- {
- if (nums1[p1] > nums2[p2])
- {
- nums1[p] = nums1[p1];
- p1--;
- }
- else
- {
- nums1[p] = nums2[p2];
- p2--;
- }
- p--;
- }
- // 其中一个数组遍历完后,若 nums2 还有剩余元素,将其复制到 nums1 前面
- while (p2 >= 0)
- {
- nums1[p] = nums2[p2];
- p2--;
- p--;
- }
- }
- int main()
- {
- int nums1[] = { 1, 2, 3, 0, 0, 0, 0, 0 };
- int m = 3;
- int nums2[] = { 0, 2, 5, 6, 7 };
- int n = 5;
- merge(nums1, m, nums2, n);
- for (int i = 0; i < m + n; i++)
- {
- printf("%d ", nums1[i]);
- }
- printf("\n");
- return 0;
- }
复制代码
对比nums1[p]每次比力后的数组nums1[7] = 71,2,3,0,0,0,0,7nums1[6] = 61,2,3,0,0,0,6,7nums1[5] = 51,2,3,0,0,5,6,7nums1[4] = 31,2,3,0,3,5,6,7nums1[3] = 21,2,3,2,3,5,6,7nums1[2] = 21,2,2,2,3,5,6,7nums1[1] = 11,1,2,2,3,5,6,7 然后 p1 = -1,p2 = 0,p = 0,实行第二个 while 循环,nums1[0] = nums2[0] = 0 ,得到归并数组{0,1,2,2,3,5,6,7} 。
别的,只遍历数组 nums1 和 nums2 一次,以是时间复杂度为 O(m+n) 。
2.3 序次表存在的标题
序次表存在以下标题:
- 举行中心或表头插入、删除操纵时,时间复杂度达 O (N)。
- 增容要申请新空间、拷贝数据、开释旧空间,斲丧大。
- 扩容一样寻常扩大为原来的 2 倍,若插入数据少,造成空间浪费。
为办理这些标题,下面先容链表结构。
3. 链表
3.1 概念及结构
链表是一种物理存储结构上非连续、非序次的存储结构,数据元素的逻辑序次是通过链表中的指针链接序次实现的 。
链表在逻辑上是连续的,但是——链表节点一样寻常从堆上申请空间,而堆上空间的分配依据肯定计谋举行,导致两次申请的空间大概连续,也大概不连续——以是链表的物理存储所在不愿定连续。
3.2 链表的分类
3.2.1 常用链表
3.2.1.1 无头单向非循环链表
结构简朴,一样寻常不会单独用来存数据。实际中更多作为其他数据结构的子结构,如哈希桶、图的毗邻表等。别的这种结构在笔试口试中较常见。
3.2.1.2 带头双向循环链表
结构最复杂,一样寻常用来单独存储数据。实际中利用的链表数据结构多是带头双向循环链表。
3.3 无头单向非循环链表的实现
- #include <stdio.h>
- #include <stdlib.h>
- typedef int SLTDateType;
- // 定义链表节点结构体
- typedef struct SListNode
- {
- SLTDateType data; // 节点存储的数据
- struct SListNode* next; // 指向下一个节点的指针
- }SListNode;
- // 动态申请一个节点
- SListNode* BuySListNode(SLTDateType x)
- {
- // 为新节点分配内存空间
- SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
- if (newNode == NULL)
- {
- perror("malloc");
- return NULL;
- }
- // 初始化新节点的数据
- newNode->data = x;
- // 新节点的下一个节点初始化为空
- newNode->next = NULL;
- return newNode;
- }
- void SListPrint(SListNode* plist)
- {
- SListNode* cur = plist;
- // plist 是指向链表头节点的指针
- while (cur != NULL)
- // 无头单向非循环链表的末尾节点的 next 指针始终被设置为 NULL
- {
- // 打印当前节点的数据
- printf("%d -> ", cur->data);
- // 移动到下一个节点
- cur = cur->next;
- }
- // 打印链表结束标志
- printf("NULL\n");
- }
- // 尾插
- void SListPushBack(SListNode** pplist, SLTDateType x)
- {
- // 创建新节点
- SListNode* newNode = BuySListNode(x);
- if (*pplist == NULL)
- {
- // 如果链表为空,新节点就是链表的头节点
- *pplist = newNode;
- }
- else
- {
- // 找到链表的尾节点
- SListNode* tail = *pplist;
- while (tail->next != NULL)
- {
- tail = tail->next;
- }
-
- tail->next = newNode;
- }
- }
- // 头插
- void SListPushFront(SListNode** pplist, SLTDateType x)
- {
- SListNode* newNode = BuySListNode(x);
- // 新节点的下一个节点指向原来的头节点
- newNode->next = *pplist;
-
- *pplist = newNode;
- }
- // 尾删
- void SListPopBack(SListNode** pplist)
- {
- if (*pplist == NULL) // 若链表为空
- {
- return; // 函数立即终止执行
- }
- if ((*pplist)->next == NULL)
- {
- // 如果链表只有一个节点,释放该节点并将头指针置空
- free(*pplist);
- *pplist = NULL;
- }
- else
- {
- SListNode* prev = NULL;
- SListNode* tail = *pplist;
- while (tail->next != NULL)
- {
- prev = tail;
- tail = tail->next;
- }
- // 最后一次循环中,prev 被赋为 tail 的前一个节点,tail 移到了尾节点
-
- free(tail);
- // 倒数第二个节点的下一个节点置空
- prev->next = NULL;
- }
- }
- // 头删
- void SListPopFront(SListNode** pplist)
- {
- if (*pplist == NULL)
- {
- return;
- }
- // 保存原来的头节点
- SListNode* first = *pplist;
- // 更新头节点为原来头节点的下一个节点
- *pplist = first->next;
- // 释放原来的头节点
- free(first);
- }
- // 查找
- SListNode* SListFind(SListNode* plist, SLTDateType x)
- {
- // 遍历链表,查找值为x的节点
- SListNode* cur = plist;
- while (cur != NULL)
- {
- if (cur->data == x)
- {
- return cur;
- }
-
- cur = cur->next;
- }
-
- return NULL;
- }
- // 在pos的后一个位置插入值 x
- void SListInsertAfter(SListNode* pos, SLTDateType x)
- {
- if (pos == NULL)
- {
- return;
- }
-
- SListNode* newNode = BuySListNode(x);
-
- newNode->next = pos->next;
-
- pos->next = newNode;
- }
- // 删除pos后一个位置的值
- void SListEraseAfter(SListNode* pos)
- {
- if (pos == NULL || pos->next == NULL) // 如果pos为空或者pos是尾节点
- {
- return;
- }
- // 保存pos节点的下一个节点
- SListNode* nextNode = pos->next;
- // pos节点的下一个节点指向要删除节点的下一个节点
- pos->next = nextNode->next;
- free(nextNode);
- }
- int main()
- {
- SListNode* list = NULL;
- // 声明并初始化一个指向 SListNode 类型结构体的指针 list
- // 用于表示初始为空的链表
- // 尾插
- SListPushBack(&list, 1);
- SListPushBack(&list, 2);
- SListPushBack(&list, 3);
- SListPrint(list);
- // 头插
- SListPushFront(&list, 0);
- SListPrint(list);
- // 尾删
- SListPopBack(&list);
- SListPrint(list);
- // 头删
- SListPopFront(&list);
- SListPrint(list);
- // 查找
- SListNode* found = SListFind(list, 2);
- if (found)
- {
- printf("Found: %d\n", found->data);
- }
- else
- {
- printf("Not found\n");
- }
- // 在pos的后一个位置插入值
- SListInsertAfter(list, 4);
- SListPrint(list);
- // 删除pos后一个位置的值
- SListEraseAfter(list);
- SListPrint(list);
- return 0;
- }
复制代码
“无头单向非循环链表的末端节点的 next 指针始终被设置为 NULL”:
- 节点创建:新节点创建时,next 指针初始化为 NULL。
- 尾插操纵:尾插新节点时,若链表为空,新节点成头节点,next 为 NULL;若不为空,找到尾节点,将其 next 指向新节点(新节点 next 已初始化为 NULL)。
- 尾删操纵:尾删时,若链表仅一个节点,开释后链表为空;如有多个节点,开释尾节点,将倒数第二个节点的 next 置为 NULL。
思索:为什么在pos的后一个位置插入值,而非前一个位置?为什么不删除 pos 位置节点?
(1)单向链表只能从前向后遍历,没有指向前一节点的指针,无法直接获取 pos 节点的前一个节点,因此只能重新节点开始依次遍历链表,直到找到 pos 节点的前一个节点,时间复杂度达 O(n);而在 pos 后插入,只改两个指针,时间复杂度为 O(1)。
(2)删除 pos 位置节点需找到其前一节点,时间复杂度达 O(n);而删除 pos 后节点,只改一个指针,时间复杂度为 O(1)。
3.4 带头双向循环链表的实现
- #include <stdio.h>
- #include <stdlib.h>
- typedef int LTDataType;
- typedef struct ListNode
- {
- LTDataType _data; // 节点存储的数据
- struct ListNode* next; // 指向下一个节点的指针
- struct ListNode* prev; // 指向前一个节点的指针
- }ListNode;
- // 创建返回链表的头结点
- ListNode* ListCreate()
- {
- // 为头节点分配内存
- ListNode* phead = (ListNode*)malloc(sizeof(ListNode));
- if (phead == NULL)
- {
- perror("malloc fail");
- return NULL;
- }
- // 头节点的 next 和 prev 都指向自身,形成循环
- phead->next = phead;
- phead->prev = phead;
- return phead;
- }
- // 双向链表销毁
- void ListDestory(ListNode* plist)
- {
- // 从头节点的下一个节点开始遍历
- ListNode* cur = plist->next;
- while (cur != plist)
- {
- // 保存当前节点的下一个节点
- ListNode* next = cur->next;
-
- free(cur);
- cur = next;
- }
- // 最后释放头节点的内存
- free(plist);
- }
- void ListPrint(ListNode* plist)
- {
- ListNode* cur = plist->next;
- while (cur != plist)
- {
- printf("%d ", cur->_data);
- cur = cur->next;
- }
- printf("\n");
- }
- // 尾插
- void ListPushBack(ListNode* plist, LTDataType x)
- {
- // 创建新节点
- ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
- if (newnode == NULL)
- {
- perror("malloc fail");
- return;
- }
- newnode->_data = x;
- // 找到尾节点
- ListNode* tail = plist->prev;
- // 插入新节点
- tail->next = newnode;
- newnode->prev = tail;
- newnode->next = plist;
- plist->prev = newnode;
- }
- // 尾删
- void ListPopBack(ListNode* plist)
- {
- // 检查链表是否为空
- if (plist->next == plist)
- {
- return;
- }
- // 找到尾节点
- ListNode* tail = plist->prev;
- // 找到尾节点的前一个节点
- ListNode* prev = tail->prev;
-
- free(tail);
- // 更新指针
- prev->next = plist;
- plist->prev = prev;
- }
- // 头插
- void ListPushFront(ListNode* plist, LTDataType x)
- {
- ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
- if (newnode == NULL)
- {
- perror("malloc fail");
- return;
- }
- newnode->_data = x;
- // 保存头节点的下一个节点
- ListNode* first = plist->next;
- // 插入新节点
- plist->next = newnode;
- newnode->prev = plist;
- newnode->next = first;
- first->prev = newnode;
- }
- // 头删
- void ListPopFront(ListNode* plist)
- {
- // 检查链表是否为空
- if (plist->next == plist)
- {
- return;
- }
- // 保存头节点的下一个节点
- ListNode* first = plist->next;
- // 保存头节点的下下一个节点
- ListNode* second = first->next;
- // 释放头节点的下一个节点的内存
- free(first);
- // 更新指针
- plist->next = second;
- second->prev = plist;
- }
- // 查找
- ListNode* ListFind(ListNode* plist, LTDataType x)
- {
- // 从头节点的下一个节点开始遍历
- ListNode* cur = plist->next;
- while (cur != plist)
- {
- if (cur->_data == x)
- {
- return cur;
- }
- cur = cur->next;
- }
- return NULL;
- }
- // 在pos的前一个位置插入
- void ListInsert(ListNode* pos, LTDataType x)
- {
- ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
- if (newnode == NULL)
- {
- perror("malloc fail");
- return;
- }
- newnode->_data = x;
- // 找到 pos 节点的前一个节点
- ListNode* prev = pos->prev;
- // 插入新节点
- prev->next = newnode;
- newnode->prev = prev;
- newnode->next = pos;
- pos->prev = newnode;
- }
- // 删除pos位置的节点
- void ListErase(ListNode* pos)
- {
- ListNode* prev = pos->prev;
-
- ListNode* next = pos->next;
-
- free(pos);
- // 更新指针
- prev->next = next;
- next->prev = prev;
- }
- int main()
- {
- ListNode* plist = ListCreate();
- // 调用 ListCreate 创建带头双向循环链表,并将返回的头节点指针赋值给 plist
- ListPushBack(plist, 1);
- ListPushBack(plist, 2);
- ListPushBack(plist, 3);
- ListPrint(plist);
- ListPushFront(plist, 0);
- ListPrint(plist);
- ListPopBack(plist);
- ListPrint(plist);
- ListPopFront(plist);
- ListPrint(plist);
- ListNode* pos = ListFind(plist, 2);
- if (pos)
- {
- ListInsert(pos, 4);
- ListPrint(plist);
- }
- pos = ListFind(plist, 4);
- if (pos)
- {
- ListErase(pos);
- ListPrint(plist);
- }
- ListDestory(plist);
- return 0;
- }
复制代码
4. 对比序次表和链表
对比维度序次表链表存储空间物理存储上肯定连续逻辑上数据元素连续,但物理内存中不愿定连续,节点通过指针相互毗连随机访问支持随机访问,通过下标可直接定位元素,时间复杂度为 O (1)需从表头开始,逐个通过指针遍历查找元素,时间复杂度为 O (N)插入和删除操纵在恣意位置插入或删除元素时,大概需移动后续元素,服从较低,时间复杂度为 O (N)插入或删除元素时,仅需修改干系节点的指针指向,操纵相对轻巧插入特性动态序次表空间不敷时,需举行扩容,涉及重新分配内存、数据拷贝等步调没有容量限定,可随时动态创建和删除节点应用场景恰当元素高效存储且频仍访问的场景,如数组实现的线性表实用于数据插入和删除操纵频仍的场景,如实现队列、栈等数据结构缓存利用率物理存储连续,符合局部性原理,读取数据时相邻数据易一同加载到缓存中,缓存利用率高节点物理位置分散,难以充实利用缓存,缓存利用率较低
|