码文不易,给个双击~
码文不易,给个双击~
码文不易,给个双击~
目录
线性表是n个具有相同特性的数据元素的有限序列。
常见的线性表:顺序表、链表、栈、队列、字符串……
线性表在逻辑上是线性结构,但在物理上存储时,通常以数组和链式结构的形式存储。
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。
- //顺序表的静态存储
- #define N 12
- typedef int SLDataType;
- typedef struct SeqList
- {
- SLDataType arr[N];//定长数组
- size_t size;//有效数据的个数
- }SeqList;
- //顺序表的动态存储
- typedef struct SeqList
- {
- SLDataType* arr;//指向动态开辟的数组
- size_t size;//有效数据的个数
- size_t capicity;//容量空间的大小
- }SeqList;
实际应用中基本上使用动态顺序表,根据需要动态地分配空间大小。
基本增删查改接口有多种,如下:
顺序表初始化
顺序表检查空间,如果满了,进行增容
顺序表尾插
顺序表尾删
顺序表头插
顺序表头删
顺序表查找
顺序表在pos位置插入x
顺序表删除pos位置的值
顺序表改变pos位置的值
顺序表销毁
顺序表打印
test.c
- #include"OrderTable.h"
- void TestOT()
- {
- OT s;//定义一个结构体变量,即顺序表
- //顺序表初始化
- OTInit(&s);
- //顺序表检查空间,如果满了,进行增容
- OTCheckCapacity(&s);
- //顺序表尾插
- OTPushBack(&s, 1);
- OTPushBack(&s, 2);
- OTPushBack(&s, 3);
- OTPushBack(&s, 4);
- OTPushBack(&s, 5);
- //顺序表打印
- OTPrint(&s);
- //顺序表尾删
- OTPopBack(&s);
- //顺序表头插
- OTPushFront(&s, 1);
- OTPushFront(&s, 2);
- OTPushFront(&s, 3);
- OTPushFront(&s, 4);
- OTPushFront(&s, 5);
- OTPrint(&s);
- //顺序表头删
- OTPopFront(&s);
- //顺序表查找
- OTFind(&s, 2);
- //顺序表在pos位置插入x,pos是指下标
- OTInsert(&s, 0, 100);
- //顺序表删除pos位置的值
- OTErase(&s, 1);
- //顺序表改变pos位置的值
- OTChange(&s, 2,99);
- //顺序表销毁
- OTDestroy(&s);
- }
- int main()
- {
- TestOT();//测试顺序表基本增删查改接口功能
- return 0;
- }
OrderTable.h
- #include
- #include
- #pragma once
- #include
- //顺序表的动态存储
- typedef int OTDataType;
- typedef struct OrderTable
- {
- OTDataType* a;//指针指向动态开辟的数组
- size_t size;//有效数据的个数
- size_t capacity;//存储空间的大小
- }OT;
-
- //顺序表初始化
- void OTInit(OT* s);
- //顺序表检查空间,如果满了,进行增容
- void OTCheckCapacity(OT* s);
- //顺序表尾插
- void OTPushBack(OT* s, OTDataType x);
- //顺序表打印
- void OTPrint(OT* s);
- //顺序表尾删
- void OTPopBack(OT* s);
- //顺序表头插
- void OTPushFront(OT* s, OTDataType x);
- //顺序表头删
- void OTPopFront(OT* s);
- //顺序表查找
- void OTFind(OT* s, OTDataType x);
- //顺序表在pos位置插入x
- void OTInsert(OT* s, size_t pos, OTDataType x);
- //顺序表删除pos位置的值
- void OTErase(OT* s, size_t pos);
- //顺序表改变pos位置的值
- void OTChange(OT* s, size_t pos, OTDataType x);
- //顺序表销毁
- void OTDestroy(OT* s);
OrderTable.c
- #include"OrderTable.h"
-
- void OTInit(OT* s)
- {
- assert(s);
- s->a = NULL;
- s->size = 0;
- s->capacity = 0;
- }
-
- void OTCheckCapacity(OT* s)
- {
- assert(s);
- //检查容量
- if (s->size == s->capacity)
- {
- size_t newcapacity = s->capacity == 0 ? 4 : s->capacity * 2;
- OTDataType* tmp = (OTDataType*)realloc(s->a, newcapacity * sizeof(OTDataType));
- if (tmp == NULL)
- {
- perror("realloc fail\n");
- return;
- }
- s->a = tmp;
- s->capacity = newcapacity;
- }
- }
-
- void OTPushBack(OT* s, OTDataType x)
- {
- assert(s);
- OTCheckCapacity(s);
- s->a[s->size] = x;
- s->size++;
- }
-
- void OTPrint(const OT* s)
- {
- assert(s);
- int i = 0;
- for (i = 0; i < s->size; ++i)
- {
- printf("%d ", s->a[i]);
- }
- printf("\n");
- }
-
- void OTPopBack(OT* s)
- {
- assert(s);
- if (s->size == 0)
- {
- return;
- }
- s->size--;
- }
-
- void OTPushFront(OT* s, OTDataType x)
- {
- assert(s);
- OTCheckCapacity(s);
- //挪动数据
- int end = s->size - 1;
- while (end >= 0)
- {
- s->a[end + 1] = s->a[end];
- --end;
- }
- s->a[0] = x;
- s->size++;
- }
-
- void OTPopFront(OT* s)
- {
- assert(s);
- assert(s->size > 0);
- int begin = 1;
- while (begin < s->size)
- {
- s->a[begin - 1] = s->a[begin];
- ++begin;
- }
- s->size--;
- }
-
- void OTFind(OT* s, OTDataType x)
- {
- assert(s);
- int i = 0;
- for (i = 0; i < s->size; ++i)
- {
- if (s->a[i] == x)
- {
- return i;
- }
- }
- return -1;
- }
-
- void OTInsert(OT* s, size_t pos, OTDataType x)
- {
- assert(s);
- assert(pos <= s->size);
- OTCheckCapacity(s);
- //挪动数据
- size_t end = s->size - 1;
- while (end >= pos)
- {
- s->a[end + 1] = s->a[end];
- --end;
- }
- s->a[pos] = x;
- s->size++;
- }
-
- void OTErase(OT* s, size_t pos)
- {
- assert(s);
- assert(pos < s->size);
- size_t begin = pos;
- while (begin < s->size - 1)
- {
- s->a[begin] = s->a[begin + 1];
- ++begin;
- }
- s->size--;
- }
-
- void OTChange(OT* s, size_t pos, OTDataType x)
- {
- assert(s);
- assert(pos <= s->size - 1);
- s->a[pos] = x;
- }
-
- void OTDestroy(OT* s)
- {
- assert(s);
- free(s->a);
- s->a = NULL;
- s->size = 0;
- s->capacity = 0;
- }
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
https://leetcode.cn/problems/remove-element/
给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。
由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果。
将最终结果插入 nums 的前 k 个位置后返回 k 。
不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
https://leetcode.cn/problems/remove-duplicates-from-sorted-array/
给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。
请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。
注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。
https://leetcode.cn/problems/merge-sorted-array/
1. 中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到
200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
因此,我们便有了链表~
链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
假设在32位系统上,结点中值域为int类型,则一个节点的大小为8个字节,则可能有下述链表:
从上图可知:
a.链式结构在逻辑上是连续的,但是在物理上不一定连续
b.现实中的结点一般都是从堆上申请出来的
c. 从堆上申请的空间,是按照一定的策略来分配的,两次申请的空间可能连续,也可能不连续
实际中最常用的是无头单向非循环链表和带头双向循环链表:
a.无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结
构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
b.带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都
是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带
来很多优势,实现反而简单了,后面我们代码实现了就知道了。
定义一个结构体类型的变量,即链表的结点:
- typedef int SLDataType;
- //定义一个结构体类型的变量,即链表的结点
- typedef struct SListNode
- {
- SLDataType data;
- struct SListNode* next;
- }SLN;
注意:
- typedef int SLDataType;
- //定义一个结构体类型的变量,即链表的结点
- typedef struct SListNode
- {
- SLDataType data;
- struct SListNode* next;
- }SLN, * PSLN;
- //以下三种结构体指针等价
- //SLN*
- //PSLN PSLN是指针而不是指针变量
- //struct SListNode
无头单向非循环链表的实现包括:
单链表打印
- //此处SLN不需要断言,因为结点(结构体类型)是允许为空的,即没有结点
- void SListPrint(SLN* phead)
- {
- SLN* current = phead;
- while (current != NULL)
- {
- printf("%d->", current->data);
- current = current->next;
- }
- printf("NULL\n");
- }
动态申请一个节点
- SLN* SLBuyNode(SLDataType x)
- {
- //为什么不直接用静态空间呢?因为静态空间是在栈区上创建的,使用完后立即销毁,
- //而动态空间只有free之后才会被回收
- SLN* nownode = (SLN*)malloc(sizeof(SLN));
- if (nownode == NULL)
- {
- perror("malloc fail\n");
- exit(-1);
- }
- nownode->data = x;
- nownode->next = NULL;
- return nownode;
- }
单链表尾插
- //此处结点传参的时候用二级指针,是因为我们将要改变的就是指针,就好比交换两个数,
- //我们将数的地址作为实参才能改变数,同理,我们将指针的地址作为实参才能改变指针
- void SListPushBack(SLN** pphead, SLDataType x)
- {
- assert(pphead);//此时断言是因为指针的地址一定不能为空,否则程序错误
- SLN* nownode = SLBuyNode(x);
- if (*pphead == NULL)
- {
- *pphead = nownode;
- }
- else
- {
- SLN* tail = *pphead;
- while (tail->next != NULL)
- {
- tail = tail->next;
- }
- tail->next = nownode;
- }
- }
单链表头插
- void SListPushFront(SLN** pphead, SLDataType x)
- {
- assert(pphead);
- SLN* newnode = SLBuyNode(x);
- newnode->next = *pphead;
- *pphead = newnode;
- }
单链表尾删
- void SListPopBack(SLN** pphead)
- {
- assert(pphead);
- assert(*pphead != NULL);//这里断言是因为要保持链表至少有一个结点,才有删除的余地
- //一个结点
- if ((*pphead)->next == NULL)
- {
- free(*pphead);
- *pphead = NULL;
- }
- //多个结点
- else
- {
- //找尾
- SLN* previous = NULL;//这里的previous用来配合tail找到最后一个结点和倒数第二个结点,自有妙用
- SLN* tail = *pphead;
- while (tail->next != NULL)
- {
- previous = tail;
- tail = tail->next;
- }
- previous->next = NULL;
- free(tail);
- tail = NULL;
- //或者
- /* SLN* tail = *pphead;
- while (tail->next->next != NULL)
- {
- tail = tail->next;
- }
- free(tail->next);
- tail->next = NULL;*/
- }
- }
单链表头删
- void SListPopFront(SLN** pphead)
- {
- assert(pphead);
- assert(pphead != NULL);
- SLN* delete = *pphead;
- *pphead = (*pphead)->next;
- free(delete);
- delete = NULL;
- }
单链表查找
- SLN* SListFind(SLN* phead, SLDataType x)
- {
- SLN* current = phead;
- while (current != NULL)
- {
- if (current->data == x)
- {
- return current;
- }
- current = current->next;
- }
- return NULL;
- }
单链表在position位置之前插入
- position = SListFind(pList, 2);
- if (position != NULL)
- {
- SListInsert(&pList, position, 20);
- }
-
-
-
- void SListInsert(SLN** pphead, SLN* position, SLDataType x)
- {
- assert(pphead);
- assert(position);
- if (position == *pphead)
- {
- SListPushFront(pphead, 20);
- }
- else
- {
- SLN* previous = *pphead;
- while (previous->next!=position)
- {
- previous = previous->next;
- assert(previous);//暴力检查,如果position不指向链表,prev为空,还没有找到pos,说明pos传错了
- }
- SLN* newnode = SLBuyNode(x);
- previous->next = newnode;
- newnode->next = position;
- }
- }
单链表在position位置之后插入
- position = SListFind(pList, 1);
- if (position != NULL)
- {
- SListInsertAfter(position, 10);
- }
-
-
-
- void SListInsertAfter(SLN* position, SLDataType x)
- {
- assert(position);
- SLN* newnode = SLBuyNode(x);
- newnode->next = position->next;
- position->next = newnode;
- }
单链表删除position处的位置
- position = SListFind(pList, 2);
- if (position != NULL)
- {
- SListErase(&pList, position);
- position = NULL;
- }
-
-
-
- void SListErase(SLN** pphead, SLN* position)
- {
- assert(pphead);
- assert(position);
- if (position == *pphead)
- {
- SListPopFront(pphead);
- }
- else
- {
- SLN* previous = *pphead;
- if (previous->next != position)
- {
- previous = previous->next;
- assert(previous);
- }
- previous->next = position->next;
- free(position);
- position = NULL;
- }
- }
单链表删除position之后的位置
- position = SListFind(pList, 2);
- if (position != NULL)
- {
- SListEraseAfter(position);
- position = NULL;
- }
-
-
-
- void SListEraseAfter(SLN* position)
- {
- assert(position);
- if (position->next == NULL)
- {
- return;
- }
- else
- {
- SLN* next = position->next;
- position->next = next->next;
- free(next);
- next = NULL;
- }
- }
单链表销毁
- void SListDestroy(SLN** pphead)
- {
- assert(pphead);
- SLN* current = *pphead;
- while (current != NULL)
- {
- SLN* next = current->next;
- free(current);
- current = next;
- }
- *pphead = NULL;
- }
增删改查各功能图解:
源代码:
Test.c:
- #include"SList.h"
- void TestSList()
- {
- SLN* pList = NULL;
- SListPushBack(&pList, 1);//单链表尾插
- SListPushBack(&pList, 2);
- SListPushBack(&pList, 3);
- SListPushBack(&pList, 4);
- SListPrint(pList);//单链表打印
- SListPushFront(&pList, 1);//单链表头插
- SListPushFront(&pList, 2);
- SListPushFront(&pList, 3);
- SListPushFront(&pList, 4);
- SListPrint(pList);//单链表打印
- SListPopBack(&pList);//单链表尾删
- SListPrint(pList);//单链表打印
- SListPopFront(&pList);//单链表头删
- SListPrint(pList);//单链表打印
- SLN* position = SListFind(pList, 3);//单链表查找
- if (position != NULL)
- {
- position->data *= 10;
- printf("找到了\n");
- }
- else
- {
- printf("没找到\n");
- }
- SListPrint(pList);//单链表打印
- //单链表在position之前插入
- position = SListFind(pList, 2);
- if (position != NULL)
- {
- SListInsert(&pList, position, 20);
- }
- SListPrint(pList);//单链表打印
- position = SListFind(pList, 1);
- if (position != NULL)
- {
- SListInsertAfter(position, 10);
- }
- SListPrint(pList);//单链表打印
- //单链表删除position处的位置
- position = SListFind(pList, 2);
- if (position != NULL)
- {
- SListErase(&pList, position);
- position = NULL;
- }
- SListPrint(pList);//单链表打印
- //单链表删除position之后的位置
- position = SListFind(pList, 2);
- if (position != NULL)
- {
- SListEraseAfter(position);
- position = NULL;
- }
- SListPrint(pList);//单链表打印
- SListDestroy(&pList);//单链表销毁
- }
- int main()
- {
- TestSList();
- return 0;
- }
SList.h:
- #pragma once//避免头文件的重复引入
- #include
- #include
- #include
-
-
- typedef int SLDataType;
- //定义一个结构体类型的变量,即链表的结点
- typedef struct SListNode
- {
- SLDataType data;
- struct SListNode* next;
- }SLN;
-
- //单链表打印
- void SListPrint(SLN* phead);
- //动态申请一个结点
- SLN* SLBuyNode(SLDataType x);
- //单链表尾插
- void SListPushBack(SLN** pphead, SLDataType x);
- //单链表头插
- void SListPushFront(SLN** pphead, SLDataType x);
- //单链表尾删
- void SListPopBack(SLN** pphead);
- //单链表头删
- void SListPopFront(SLN** pphead);
- //单链表查找
- SLN* SListFind(SLN* phead, SLDataType x);
- //在position位置之前插入
- void SListInsert(SLN** pphead, SLN* position, SLDataType x);
- //在position位置之后插入
- void SListInsertAfter(SLN* position, SLDataType x);
- //删除position位置的值
- void SListErase(SLN** pphead, SLN* position);
- //删除position之后的位置
- void SListEraseAfter(SLN* position);
- //单链表销毁
- void SListDestroy(SLN** pphead);
SList.c:
- #include"SList.h"
-
- //此处SLN不需要断言,因为结点(结构体类型)是允许为空的,即没有结点
- void SListPrint(SLN* phead)
- {
- SLN* current = phead;
- while (current != NULL)
- {
- printf("%d->", current->data);
- current = current->next;
- }
- printf("NULL\n");
- }
-
- SLN* SLBuyNode(SLDataType x)
- {
- //为什么不直接用静态空间呢?因为静态空间是在栈区上创建的,使用完后立即销毁,
- //而动态空间只有free之后才会被回收
- SLN* newnode = (SLN*)malloc(sizeof(SLN));
- if (newnode == NULL)
- {
- perror("malloc fail\n");
- exit(-1);
- }
- newnode->data = x;
- newnode->next = NULL;
- return newnode;
- }
-
- //此处结点传参的时候用二级指针,是因为我们将要改变的就是指针,就好比交换两个数,
- //我们将数的地址作为实参才能改变数,同理,我们将指针的地址作为实参才能改变指针
- void SListPushBack(SLN** pphead, SLDataType x)
- {
- assert(pphead);//此时断言是因为指针的地址一定不能为空,否则程序错误
- SLN* newnode = SLBuyNode(x);
- if (*pphead == NULL)
- {
- *pphead = newnode;
- }
- else
- {
- //找尾
- SLN* tail = *pphead;
- while (tail->next != NULL)
- {
- tail = tail->next;
- }
- tail->next = newnode;
- }
- }
-
- void SListPushFront(SLN** pphead, SLDataType x)
- {
- assert(pphead);
- SLN* newnode = SLBuyNode(x);
- newnode->next = *pphead;
- *pphead = newnode;
- }
-
- void SListPopBack(SLN** pphead)
- {
- assert(pphead);
- assert(*pphead != NULL);//这里断言是因为要保持链表至少有一个结点,才有删除的余地
- //一个结点
- if ((*pphead)->next == NULL)
- {
- free(*pphead);
- *pphead = NULL;
- }
- //多个结点
- else
- {
- //找尾
- SLN* previous = NULL;//这里的previous用来配合tail找到最后一个结点和倒数第二个结点,自有妙用
- SLN* tail = *pphead;
- while (tail->next != NULL)
- {
- previous = tail;
- tail = tail->next;
- }
- previous->next = NULL;
- free(tail);
- tail = NULL;
- //或者
- /* SLN* tail = *pphead;
- while (tail->next->next != NULL)
- {
- tail = tail->next;
- }
- free(tail->next);
- tail->next = NULL;*/
- }
- }
-
- void SListPopFront(SLN** pphead)
- {
- assert(pphead);
- assert(pphead != NULL);
- SLN* delete = *pphead;
- *pphead = (*pphead)->next;
- free(delete);
- delete = NULL;
- }
-
- SLN* SListFind(SLN* phead, SLDataType x)
- {
- SLN* current = phead;
- while (current != NULL)
- {
- if (current->data == x)
- {
- return current;
- }
- current = current->next;
- }
- return NULL;
- }
-
- void SListInsert(SLN** pphead, SLN* position, SLDataType x)
- {
- assert(pphead);
- assert(position);
- if (position == *pphead)
- {
- SListPushFront(pphead, 20);
- }
- else
- {
- SLN* previous = *pphead;
- while (previous->next!=position)
- {
- previous = previous->next;
- assert(previous);//暴力检查,如果position不指向链表,prev为空,还没有找到pos,说明pos传错了
- }
- SLN* newnode = SLBuyNode(x);
- previous->next = newnode;
- newnode->next = position;
- }
- }
-
- void SListInsertAfter(SLN* position, SLDataType x)
- {
- assert(position);
- SLN* newnode = SLBuyNode(x);
- newnode->next = position->next;
- position->next = newnode;
- }
-
- void SListErase(SLN** pphead, SLN* position)
- {
- assert(pphead);
- assert(position);
- if (position == *pphead)
- {
- SListPopFront(pphead);
- }
- else
- {
- SLN* previous = *pphead;
- if (previous->next != position)
- {
- previous = previous->next;
- assert(previous);
- }
- previous->next = position->next;
- free(position);
- position == NULL;
- }
- }
-
- void SListEraseAfter(SLN* position)
- {
- assert(position);
- if (position->next == NULL)
- {
- return;
- }
- else
- {
- SLN* next = position->next;
- position->next = next->next;
- free(next);
- next == NULL;
- }
- }
-
- void SListDestroy(SLN** pphead)
- {
- assert(pphead);
- SLN* current = *pphead;
- while (current != NULL)
- {
- SLN* next = current->next;
- free(current);
- current = next;
- }
- *pphead = NULL;
- }
带哨兵头(guard)和不带哨兵头(head)
找工作正式笔试中没有测试用例,也不能进行调试,所以我们就应该学会自己猜测试用例。
测试主要分为常规场景和极端场景(边界)。
测试代码(自己备份好,平时练习时遇到可以用):
- int main()
- {
- struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
- assert(n1);
- struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
- assert(n2);
- struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
- assert(n3);
- struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
- assert(n4);
- struct ListNode* n5 = (struct ListNode*)malloc(sizeof(struct ListNode));
- assert(n5);
- struct ListNode* n6 = (struct ListNode*)malloc(sizeof(struct ListNode));
- assert(n6);
- struct ListNode* n7 = (struct ListNode*)malloc(sizeof(struct ListNode));
- assert(n7);
-
- n1->next = n2;
- n2->next = n3;
- n3->next = n4;
- n4->next = n5;
- n5->next = n6;
- n6->next = n7;
- n7->next = NULL;
-
- n1->val = 1;
- n2->val = 2;
- n3->val = 6;
- n4->val = 3;
- n5->val = 4;
- n6->val = 5;
- n7->val = 6;
- struct ListNode* head = removeElements(n1, 6);
-
-
- return 0;
- }
给你一个链表的头节点 head
和一个整数 val
,请你删除链表中所有满足 Node.val == val
的节点,并返回 新的头节点 。
https://leetcode.cn/problems/remove-linked-list-elements/
给你单链表的头节点 head
,请你反转链表,并返回反转后的链表。
https://leetcode.cn/problems/reverse-linked-list/description/
给定一个头结点为 head
的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
https://leetcode.cn/problems/middle-of-the-linked-list/description/
输入一个链表,输出该链表中倒数第k个结点。
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
https://leetcode.cn/problems/merge-two-sorted-lists/description/
现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。
给你两个单链表的头节点 headA
和 headB
,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null
。
https://leetcode.cn/problems/intersection-of-two-linked-lists/description/
给你一个链表的头节点 head ,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。
如果链表中存在环 ,则返回 true 。 否则,返回 false 。
https://leetcode.cn/problems/linked-list-cycle/description/
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
https://leetcode.cn/problems/linked-list-cycle-ii/description/
给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。
构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。
例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。
返回复制链表的头节点。
https://leetcode.cn/problems/copy-list-with-random-pointer/description/
首先在头文件中声明结构体类型
- //声明一个结构体类型,即双链表的结点
- typedef int LTDataType;
- typedef struct ListNode
- {
- struct ListNode* prev;
- struct ListNode* next;
- LTDataType data;
- }LTNode;
带头双向循环链表增删改查各功能的实现:
创建返回链表的头结点(即初始化)
- //以下方法是使用带哨兵头的方法,当然也可以使用二级指针传参的方式
- LTNode* ListInit()
- {
- LTNode* guard = (LTNode*)malloc(sizeof(LTNode));
- if (guard == NULL)
- {
- printf("malloc fail\n");
- exit(-1);
- }
- guard->next = guard;
- guard->prev = guard;
- return guard;
- }
双向链表打印
- void ListPrint(LTNode* phead)
- {
- assert(phead);
- LTNode* cur = phead->next;
- printf("head<=>");
- while (cur != phead)
- {
- printf("%d<=>", cur->data);
- cur = cur->next;
- }
- printf("\n");
- }
新建一个结点
- LTNode* BuyListNode(LTDataType x)
- {
- LTNode* node = (LTNode*)malloc(sizeof(LTNode));
- if (node == NULL)
- {
- printf("malloc fail\n");
- exit(-1);
- }
- node->next = NULL;
- node->prev = NULL;
- node->data = x;
- return node;
- }
双向链表尾插
- void ListPushBack(LTNode* phead, LTDataType x)
- {
- LTNode* newnode = BuyListNode(x);
- LTNode* tail = phead->prev;
- tail->next = newnode;
- newnode->prev = tail;
- newnode->next = phead;
- phead->prev=newnode;
- }
双向链表尾删
- void ListPopBack(LTNode* phead)
- {
- assert(phead);
- assert(!ListEmpty(phead));
- LTNode* tail = phead->prev;
- LTNode* prev = tail->prev;
- prev->next = phead;
- phead->prev = prev;
- free(tail);
- }
双向链表头插
- void ListPushFront(LTNode* phead, LTDataType x)
- {
- assert(phead);
- LTNode* newnode = BuyListNode(x);
- //newnode->next = phead->next;
- //phead->next->prev = newnode;
- //phead->next = newnode;
- //newnode->prev = phead;
- //不关心顺序的方法
- LTNode* first = phead->next;
- phead->next = newnode;
- newnode->prev = phead;
- newnode->next = first;
- first->prev = newnode;
- }
双向链表头删
- void ListPopFront(LTNode* phead)
- {
- assert(phead);
- assert(!ListEmpty(phead));
- LTNode* first = phead->next;
- LTNode* second = first->next;
- phead->next = second;
- second->prev = phead;
- free(first);
- }
双向链表查找
- LTNode* ListFind(LTNode* phead, LTDataType x)
- {
- assert(phead);
- LTNode* cur = phead->next;
- while (cur != phead)
- {
- if (cur->data == x);
- {
- return cur;
- }
- cur = cur->next;
- }
- return NULL;
- }
双向链表在pos位置前插入
- void ListInsert(LTNode* pos, LTDataType x)
- {
- assert(pos);
- LTNode* prev = pos->prev;
- LTNode* newnode = BuyListNode(x);
- prev->next = newnode;
- newnode->prev = prev;
- newnode->next = pos;
- pos->prev = newnode;
- }
双向链表删除pos位置的结点
- void ListErase(LTNode* pos)
- {
- assert(pos);
- LTNode* prev = pos->prev;
- LTNode* next = pos->next;
- prev->next = next;
- next->prev = prev;
- free(pos);
- pos = NULL;
- }
双向链表销毁
- void ListDestroy(LTNode* phead)
- {
- assert(phead);
- LTNode* cur = phead->next;
- while (cur != phead)
- {
- LTNode* next = cur->next;
- free(cur);
- cur = next;
- }
- free(phead);
- phead = NULL;
- }
源代码:
List.h:
- #pragma once
- #include
- #include
- #include
- #include
-
- //声明一个结构体类型,即双链表的结点
- typedef int LTDataType;
- typedef struct ListNode
- {
- struct ListNode* prev;
- struct ListNode* next;
- LTDataType data;
- }LTNode;
-
- //创建返回链表的头结点
- LTNode* ListInit();
- //双向链表打印
- void ListPrint(LTNode* phead);
- //双向链表尾插
- void ListPushBack(LTNode* phead, LTDataType x);
- //如果是链表是空则返回真,否则返回假
- bool ListEmpty(LTNode* phead);
- //双向链表尾删
- void ListPopBack(LTNode* phead);
- //双向链表头插
- void ListPushFront(LTNode* phead, LTDataType);
- //双向链表头删
- void ListPopFront(LTNode* phead);
- //统计链表结点个数(不包括哨兵头)
- size_t ListSize(LTNode* phead);
- //双向链表查找
- LTNode* ListFind(LTNode* phead, LTDataType x);
- //双向链表在pos位置之前插入
- void ListInsert(LTNode* phead, LTDataType x);
- //双向链表删除pos位置的结点
- void ListErase(LTNode* pos);
- //双向链表销毁
- void ListDestroy(LTNode* phead);
List.c:
- #include "List.h"
-
- #define _CRT_SECURE_NO_WARNINGS
-
- LTNode* ListInit()
- {
- LTNode* guard = (LTNode*)malloc(sizeof(LTNode));
- if (guard == NULL)
- {
- printf("malloc fail\n");
- exit(-1);
- }
- guard->next = guard;
- guard->prev = guard;
- return guard;
- }
-
- void ListPrint(LTNode* phead)
- {
- assert(phead);
- LTNode* cur = phead->next;
- printf("head<=>");
- while (cur != phead)
- {
- printf("%d<=>", cur->data);
- cur = cur->next;
- }
- printf("\n");
- }
-
- //新建一个结点
- LTNode* BuyListNode(LTDataType x)
- {
- LTNode* node = (LTNode*)malloc(sizeof(LTNode));
- if (node == NULL)
- {
- printf("malloc fail\n");
- exit(-1);
- }
- node->next = NULL;
- node->prev = NULL;
- node->data = x;
- return node;
- }
-
- void ListPushBack(LTNode* phead, LTDataType x)
- {
- LTNode* newnode = BuyListNode(x);
- LTNode* tail = phead->prev;
- tail->next = newnode;
- newnode->prev = tail;
- newnode->next = phead;
- phead->prev=newnode;
- }
-
- bool ListEmpty(LTNode* phead)
- {
- assert(phead);
- //if (phead->next == phead)
- // return true;
- //else
- // return false;
- return phead->next == phead;
- }
-
- void ListPopBack(LTNode* phead)
- {
- assert(phead);
- assert(!ListEmpty(phead));
- LTNode* tail = phead->prev;
- LTNode* prev = tail->prev;
- prev->next = phead;
- phead->prev = prev;
- free(tail);
- }
-
- void ListPushFront(LTNode* phead, LTDataType x)
- {
- assert(phead);
- LTNode* newnode = BuyListNode(x);
- //newnode->next = phead->next;
- //phead->next->prev = newnode;
- //phead->next = newnode;
- //newnode->prev = phead;
- //不关心顺序的方法
- LTNode* first = phead->next;
- phead->next = newnode;
- newnode->prev = phead;
- newnode->next = first;
- first->prev = newnode;
- }
-
- void ListPopFront(LTNode* phead)
- {
- assert(phead);
- assert(!ListEmpty(phead));
- LTNode* first = phead->next;
- LTNode* second = first->next;
- phead->next = second;
- second->prev = phead;
- free(first);
- }
-
- size_t ListSize(LTNode* phead)
- {
- assert(phead);
- size_t n = 0;
- LTNode* cur = phead->next;
- while (cur != phead)
- {
- ++n;
- cur = cur->next;
- }
- return n;
- }
-
- LTNode* ListFind(LTNode* phead, LTDataType x)
- {
- assert(phead);
- size_t n = 0;
- LTNode* cur = phead->next;
- while (cur != phead)
- {
- if (cur->data == x);
- {
- return cur;
- }
- cur = cur->next;
- }
- return NULL;
- }
-
- void ListInsert(LTNode* pos, LTDataType x)
- {
- assert(pos);
- LTNode* prev = pos->prev;
- LTNode* newnode = BuyListNode(x);
- prev->next = newnode;
- newnode->prev = prev;
- newnode->next = pos;
- pos->prev = newnode;
- }
-
- void ListErase(LTNode* pos)
- {
- assert(pos);
- LTNode* prev = pos->prev;
- LTNode* next = pos->next;
- prev->next = next;
- next->prev = prev;
- free(pos);
- pos = NULL;
- }
-
- void ListDestroy(LTNode* phead)
- {
- assert(phead);
- LTNode* cur = phead->next;
- while (cur != phead)
- {
- LTNode* next = cur->next;
- free(cur);
- cur = next;
- }
- free(phead);
- phead = NULL;
- }
Test.c:
- #include"List.h"
- void TestList()
- {
- LTNode* pList = ListInit();//创建一个哨兵头
- ListPushBack(pList, 1);//双向链表尾插
- ListPushBack(pList, 2);
- ListPushBack(pList, 3);
- ListPushBack(pList, 4);
- ListPrint(pList);//双向链表打印
- ListPopBack(pList);//双向链表尾删
- ListPopBack(pList);
- ListPrint(pList);//双向链表打印
- ListPushFront(pList, 10);//双向链表头插
- ListPushFront(pList, 20);
- ListPushFront(pList, 30);
- ListPushFront(pList, 40);
- ListPrint(pList);//双向链表打印
- ListPopFront(pList);//双向链表头删
- ListPopFront(pList);
- ListPrint(pList);//双向链表打印
- size_t n = ListSize(pList);//统计链表中结点的个数
- printf("结点个数:%d\n", n);
- LTNode* pos = ListFind(pList, 1);//双向链表查找
- ListInsert(pos, 100);//双向链表插入
- ListPrint(pList);//双向链表打印
- LTNode* pos = ListFind(pList, 2);//双向链表查找
- ListErase(pos);//删除pos位置的结点
- ListPrint(pList);//双向链表打印
- ListDestroy(phead);//双向链表销毁
- }
- int main()
- {
- TestList();
- return 0;
- }
不同点 | 顺序表 | 链表 |
存储空间上 | 物理上一定连续 | 逻辑上连续,但物理上不一定 连续 |
随机访问 | 支持O(1) | 不支持:O(N) |
任意位置插入或者删除 元素 | 可能需要搬移元素,效率低O(N) | 只需修改指针指向 |
插入 | 动态顺序表,空间不够时需要扩容 | 没有容量的概念 |
应用场景 | 元素高效存储+频繁访问 | 任意位置插入和删除频繁 |
缓存利用率 | 高 | 低 |
本篇文章全网最细,没有更细,小编花了一周多的时间总结,麻烦给个双击,后续不断更新优质内容~