链表
链表的概念及结构
概念:
链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
注意:
1.链式结构在逻辑上是连续的,但是在物理上不一定连续
2.现实中的节点一般都是从堆上申请出来的
3.从堆上申请的空间,是按照一定的策略来分配的,两次申请的空间可能连续,也可能不连续
链表的分类
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
单向或者双向
带头或者不带头(是否有哨兵位)
哨兵位
哨兵位要malloc一个新节点,哨兵里面不要存具体的值。也不能存链表的长度,万一链表里面的数据类型是char,链表长度超过128,就会溢出。
哨兵位的主要方便链表插入,插入时改变头结点不需要传二级指针,尾插不需要判断空链表
循环或者非循环
最常用的链表还是单向无头不循环链表和双向带头循环链表。
1. 单向无头非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希图、图的邻接表等等。另外这种结构在笔试面试中出现很多。
2. 双向带头循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了。
单链表
单链表的实现
SList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
}SLTNode;
void SLTPrint(SLTNode* phead);
void SLPushFront(SLTNode** pphead,SLTDataType x);
void SLPushBack(SLTNode** pphead, SLTDataType x);
void SLPopBack(SLTNode** pphead);
void SLPopFront(SLTNode** pphead);
//单链表查找
SLTNode* STFind(SLTNode* plist, SLTDataType x);
//在pos前插入
void SLInsert1(SLTNode** pphead, SLTNode* pos, SLTDataType x);
void SLInsert2(SLTNode* pos, SLTDataType x);
//pos后插
void SLInsertAfter(SLTNode* pos, SLTDataType x);
//在pos前面删除
void SLErase(SLTNode** pphead, SLTNode* pos);
//pos后删
void SLEraseAfter(SLTNode* pos);
//删除
void SLDestroy(SLTNode** pphead);
SList.c
#define _CRT_SECURE_NO_WARNINGS
#include "SList.h"
SLTNode* BuyLTNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
perror("malloc fail");
return NULL;
}
newnode->data = x;
newnode->next = NULL;
}
void SLTPrint(SLTNode* phead)
{
//assert(phead); 不需要断言,空链表可以打印
SLTNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
void SLPushFront(SLTNode** pphead, SLTDataType x)
{
assert(pphead);//pphead需要断言,pphead是头指针plist的地址如果是空则解引用野指针。
//assert(*pphead); *pphead不能断言,链表为空也可以插入
SLTNode* newnode = BuyLTNode(x);
newnode->next = *pphead;
*pphead = newnode;
}//头插不需要判断是否为空链表
void SLPushBack(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
//assert(*pphead); *pphead为空也可以尾插
SLTNode* newnode = BuyLTNode(x);
//1.空链表
if (*pphead == NULL)
{
*pphead = newnode;
}
//2、非空链表
else
{
SLTNode* tail = *pphead;
while (tail->next != NULL) //此行决定了需要单独判断空链表
{
tail = tail->next;
}
tail->next = newnode;
/*SLTNode* tail = *pphead;
while (tail != NULL)
{
tail = tail->next;
}
tail = newnode;*/ //错误代码,尾结点不仅没有与新结点连接,还造成了内存泄漏!
}
}//尾插需要判断是否为空链表,要利用*phead找尾结点
void SLPopBack(SLTNode** pphead)
{
assert(pphead);
//没有节点
assert(*pphead);//暴力检查,链表为空不能尾删,需要断言
//if (*pphead == NULL)
//{
// return;//温柔的检查
//}
SLTNode* prev = NULL;
SLTNode* tail = *pphead;
//一个节点
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
//多个节点
else
{
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
prev->next == NULL;
//写法二
/*SLTNode* tail = *pphead;
while (tail->next->next != NULL)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL; */
}
}
void SLPopFront(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);//链表为空不能头删,需要断言
一个节点
//if ((*pphead)->next == NULL)
//{
// free(*pphead);
// *pphead = NULL;
//}
多个节点
//else
//{
// SLTNode* del = *pphead;
// *pphead=del->next;
// free(del);
//}
//一个节点和多个节点合二为一,因为单节点时(*pphead)->next为空,不影响
SLTNode* del = *pphead;
*pphead = (*pphead)->next;
free(del);
}
SLTNode* STFind(SLTNode* phead, SLTDataType x)
{
//assert(phead); // 此处不需要断言,空链表可以查找
SLTNode* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void SLInsert1(SLTNode** pphead, SLTNode* pos, SLTDataType x)//默认是在当前位置前插入,单链表不太适合前插,因为要找前一个节点
{
assert(pphead);//pphead需要断言
assert(pos);//插入的节点不能为空
if (*pphead == pos)
{
SLPushFront(pphead, x);
}
else
{
SLTNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
SLTNode* newnode = BuyLTNode(x);
prev->next = newnode;
newnode->next = pos;
}
}
void SLInsert2(SLTNode* pos, SLTDataType x)//利用后插交换新结点和pos结点的数据,达成前插效果
{
assert(pos);
SLInsertAfter(pos, pos->data);
pos->data = x;
}
void SLInsertAfter(SLTNode* pos, SLTDataType x)
{
assert(pos);
SLTNode* newnode = BuyLTNode(x);
newnode->next = pos->next;
pos->next = newnode;
}//在此基础上可以完成不给头结点的前插
void SLErase(SLTNode** pphead, SLTNode* pos)
{
assert(pphead);
assert(pos);
if (pos == *pphead)
{
SLPopFront(pphead);
}
else
{
SLTNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
void SLEraseAfter(SLTNode* pos)
{
assert(pos);
assert(pos->next);
SLTNode* next = pos->next;
pos->next = next->next;
free(next);
}
void SLDestroy(SLTNode** pphead)
{
assert(pphead);
SLTNode* cur = *pphead;
while (cur)
{
SLTNode* next = cur;
free(cur);
cur = next;
}
*pphead = NULL;
}
双向带头循环链表的实现
头文件List.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int LTDatatype;
typedef struct ListNode
{
struct ListNode* next;
struct ListNode* prev;
LTDatatype data;
}LTNode;
LTNode* LTInit();
LTNode* BuyLTNode(LTDatatype x);
bool LTEmpty(LTNode* head);
void LTPushBack(LTNode* phead, LTDatatype x);
void LTPushFront1(LTNode* phead, LTDatatype x);
void LTPushFront2(LTNode* phead, LTDatatype x);
void PrintLTNode(LTNode* phead);
void LTPopBack(LTNode* phead);
void LTPopFront(LTNode* phead);
LTNode* FindLTNode(LTNode* phead, LTDatatype x);
void LTInsert(LTNode* pos, LTDatatype x);//在pos之前插入
void Erase(LTNode* pos);//删除pos的值
void DestroyLTNode(LTNode* phead);
函数具体实现
#define _CRT_SECURE_NO_WARNINGS
#include"List.h"
LTNode* BuyLTNode(LTDatatype x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
if (newnode == NULL) {
perror("malloc fail");
exit(1);
}
newnode->data = x;
newnode->prev = newnode->next = NULL;
return newnode;
}//放在第一个定义,因为编译器不会向下调用函数,或者在使用前声明
LTNode* LTInit()
{
LTNode* phead = BuyLTNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
bool LTEmpty(LTNode* phead)
{
assert(phead);
return phead->next == phead;
}
void LTPushBack(LTNode* phead, LTDatatype x)
{
assert(phead);
LTNode* tail = phead->prev;
LTNode* newnode = BuyLTNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
//链表是否为空都通用
}//先找尾结点,再插入
void LTPushFront1(LTNode* phead, LTDatatype x)
{
assert(phead);
LTNode* newnode = BuyLTNode(x);
newnode->next = phead->next;
phead->next->prev = newnode;
phead->next = newnode;
newnode->prev = phead;
//链表是否为空都通用
}//不建立新节点,关注顺序,先将新节点指向原来的头结点(哨兵位的next),再将哨兵位指向新节点
void LTPushFront2(LTNode* phead, LTDatatype x)
{
assert(phead);
LTNode* newnode = BuyLTNode(x);
LTNode* first = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = first;
first->prev = newnode;
//链表是否为空都通用
}//法二不用管顺序,先用first变量记录第一个节点的地址,再插入
void LTPopBack(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
LTNode* curtail = phead->prev;
LTNode* newtail= curtail->prev;
free(curtail);
newtail->next = phead;
phead->prev = newtail;
}//只有一个节点也适用(链表结构好),但只剩哨兵位不能继续删,添加一个LTEmpty函数增加可读性
void LTPopFront(LTNode* phead)
{
assert(phead);
assert(!LTEmpty(phead));
LTNode* start = phead->next;
LTNode* newstart = start->next;
phead->next = newstart;
newstart->prev = phead;
free(start);
}
void PrintLTNode(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
printf("guard<-->");
while (cur != phead)
{
printf("%d<-->", cur->data);
cur = cur->next;
}
printf("\n");
}
LTNode* FindLTNode(LTNode* phead, LTDatatype x)
{
LTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x) return cur;
cur = cur->next;
}
return NULL;
}
void LTInsert(LTNode* pos, LTDatatype x)
{
assert(pos);
LTNode* newnode = BuyLTNode(x);
LTNode* pospre = pos->prev;
newnode->next = pos;
newnode->prev = pospre;
pos->prev = newnode;
pospre->next = newnode;
}
/*
利用Insert实现头插尾插
void LTPushFront(LTNode* phead, LTDatatype x)
{
LTInsert(phead->next,x);
}
void LTPushBack(LTNode* phead, LTDatatype x)
{
LTInsert(phead,x);
}
*/
void LTErase(LTNode* pos)
{
assert(pos);
LTNode* posPrev = pos->prev;
LTNode* posNext = pos->next;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
}//有一点缺陷,不能传入一个哨兵位,可以加入phead进行判断是否是哨兵位,但是这个参数不是功能的核心逻辑。
/*
利用Erase实现头删尾删
void LTPopFront(LTNode* pos)
{
assert(phead);
assert(!LTEmpty(phead));
LTErase(phead->next);
}
void LTPopFront(LTNode* pos)
{
assert(phead);
assert(!LTEmpty(phead));
LTErase(phead->prev);
}
*/
void DestroyLTNode(LTNode* phead)
{
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* Next = cur->next;
free(cur);
cur = Next;
}
free(phead);
//phead = NULL;改变指针需要二级,不需要置空
}
顺序表和链表的区别
单纯的存储数据用的最多的就是顺序表和链表。
比较时使用带头双向循环链表,单链表相比于顺序表的优势不大。
链表:
优点:
1、任意位置插入删除O(1)
2.按需申请释放空间
缺点:
1、不支持下标随机访问
2、CPU高速缓存命中率低
顺序表:
优点:
1、尾插尾删效率不错
2、下标的随机访问
3、CPU高速缓存命中率高
缺点:
1、前面部分插入删除数据,效率是O(N),需要挪动数据
2、空间不够,需要扩容。 a、扩容单次性能消耗高(realloc异地扩容)b、一定程度的空间浪费
例题
移除链表元素
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* prev = NULL,*cur = head;
while(cur)
{
if(cur->val == val)
{
if(cur == head)
{
head = cur->next;
free(cur);
cur = head;
}
else
{
prev->next = cur->next;
free(cur);
cur = prev->next;
}
}
else
{
prev = cur;
cur = cur->next;
}
}
return head;
}
反转一个单链表
头插
struct ListNode* reverseList(struct ListNode* head){
if(head == NULL) return NULL;
struct ListNode* n1 = NULL;
struct ListNode* n2 = head;
struct ListNode* n3 = head->next;
while(n2)
{
n2->next = n1;
n1 = n2;
n2 = n3;
if(n3)
n3 = n3->next;
}
return n1;
}
struct ListNode* reverseList(struct ListNode* head){
struct ListNode* cur = head,*rhead = NULL,*next = NULL;
while(cur)
{
next = cur->next;
cur->next = rhead;
rhead = cur;
cur = next;
}
return rhead;
}
快慢指针
链表的中间结点
快慢指针,fast一次走两步,slow一次走一步
struct ListNode* middleNode(struct ListNode* head){ struct ListNode* slow,*fast; slow = fast = head; while(fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; } return slow; }
链表中倒数第k个结点
快指针先走k步
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
struct ListNode* slow,*fast;
slow = fast = pListHead;
if(k<=0 || pListHead == NULL) return NULL;
while(k--)
{
fast = fast->next;
if(fast == NULL && k!=0) return NULL;
}
while(fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
return slow;
}
合并两个有序链表
list1和list2挑小的尾插
1.不带哨兵位
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
struct ListNode* tail = NULL, * head = NULL;
if(list1 == NULL)//list1为空直接返回list2
{
return list2;
}
if(list2 == NULL)//list2为空直接返回list1
{
return list1;
}
while(list1 && list2)//有一个为空就终止
{
//取小的尾插
if(list1->val <= list2->val)
{
if(tail == NULL)
{
tail = head = list1;
}
else
{
tail->next = list1;
tail = tail->next;
}
list1 = list1->next;
}
else
{
if(tail == NULL)
{
tail = head = list2;
}
else
{
tail->next = list2;
tail = tail->next;
}
list2 = list2->next;
}
}
//终止后谁还有结点直接连接上
if(list1)
{
tail->next = list1;
}
if(list2)
{
tail->next = list2;
}
return head;
}
2.带哨兵位
为了插入方便可以增加一个哨兵位,可以简化插入的代码,但是需要额外开辟空间和释放
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
if(list1 == NULL)
{
return list2;
}
if(list2 == NULL)
{
return list1;
}
struct ListNode* tail = NULL, * head = NULL;
head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
while(list1 && list2)
{
if(list1->val <= list2->val)
{
tail->next = list1;
tail = tail->next;
list1 = list1->next;
}
else
{
tail->next = list2;
tail = tail->next;
list2 = list2->next;
}
}
if(list1)
{
tail->next = list1;
}
if(list2)
{
tail->next = list2;
}
struct ListNode* del = head;
head = head->next;
free(del);
return head;
}
链表分割
比x小的值插入到less链表,比x大的值插入到more链表,最后less链表与more链表连接,最好使用哨兵位,避免讨论空指针的情况。
ListNode* partition(ListNode* pHead, int x) {
struct ListNode* lesshead, *lesstail, *morehead, *moretail;
struct ListNode* cur = pHead;
lesshead = lesstail = (struct ListNode*)malloc(sizeof(struct ListNode));
morehead = moretail = (struct ListNode*)malloc(sizeof(struct ListNode));
while(cur)
{
if(cur->val < x)
{
lesstail->next = cur;
lesstail = lesstail->next;
}
else
{
moretail->next = cur;
moretail = moretail->next;
}
cur = cur->next;
}
lesstail->next = morehead->next;
moretail->next = NULL;
struct ListNode* head = lesshead->next;
free(lesshead);
free(morehead);
return head;
}
链表的回文
找到中间结点后逆置,然后一一比对。
class PalindromeList {
public:
bool chkPalindrome(ListNode* head) {
struct ListNode* mid = middleNode(head);
struct ListNode* rmid = reverseList(mid);
while(rmid)
{
if(head->val != rmid->val)
{
return false;
}
else
{
rmid = rmid->next;
head = head->next;
}
}
return true;
}
struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode* cur = head,*rhead = NULL,*next = NULL;
while(cur)
{
next = cur->next;
cur->next = rhead;
rhead = cur;
cur = next;
}
return rhead;
}
struct ListNode* middleNode(struct ListNode* head)
{
struct ListNode* slow,*fast;
slow = fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};
两个链表的公共结点
链表的相交只有一种情况,因为链表的next只有一个,不会再出现分叉
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode* n1 = headA;
struct ListNode* n2 = headB;
int len1 = 0,len2 = 0;
//判断是否相交
while(n1->next)
{
n1 = n1->next;
len1++;
}
while(n2->next)
{
n2 = n2->next;
len2++;
}
//len1++;
//len2++;自增是准确的,也可以不自增,因为求的是差值
if(n1!=n2) return NULL;
//求交叉结点
n1 = headA;
n2 = headB;
int gap = fabs(len1 - len2);
while(gap--)
{
if(len1>len2)
{
n1 = n1->next;
}
else
{
n2 = n2->next;
}
}
while(n1 != n2)
{
n1 = n1->next;
n2 = n2->next;
}
return n1;
}
环链表
判断链表是否有环
快慢指针,即慢指针一次走一步,快指针一次走两步,两个指针从链表起始位置开始运行,
如果链表带环则一定会在环中相遇,否则快指针率先走到链表的末尾。
bool hasCycle(struct ListNode *head) {
struct ListNode* slow = head;
struct ListNode* fast = head;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
{
return true;
}
}
return false;
}
Q1:slow和fast一定会相遇吗?A1:一定会。fast会先进环,slow后进环,假设slow进环时,fast追到slow的距离为N,slow每走一步,fast每走两步,他们之间的距离缩小1,距离N一直减到0结束,一定会追上。
Q2:fast一次走3步、4步……一定会相遇吗? A2:不一定。1.如果快指针走3步,慢指针走一步,如果N是偶数,则会追上;如果N为奇数,则会错过,进行新一轮追击,此时fast追到slow的距离为R-1(设R是环的长度),如果R-1是偶数那么可以追上,如果R-1是奇数那么永远追不上。
快指针走4步、5步……推导类似
判断链表是否有环并返还入口结点
法一:让一个指针从链表起始位置开始遍历链表,同时让一个指针从判环时相遇点的位置开始绕环
运行,两个指针都是每次均走一步,最终肯定会在入口点的位置相遇。
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* slow = head, * fast = head,*meet = NULL;
while(fast && fast->next)//寻找相遇结点
{
slow = slow->next;
fast = fast->next->next;
while(slow == fast)//找到相遇结点
{
struct ListNode* meet = slow;
slow = head;
while(meet != slow)//寻找入口结点
{
slow = slow->next;
meet = meet->next;
}
return meet;
}
}
return NULL;//非带环链表
}
法二: 取相遇结点处下一个结点为head1,相遇结点的前一个结点为head2,转化为寻找两个链表的交点.
复制带随机指针的链表(难)
struct Node* copyRandomList(struct Node* head) {
//1.插入
struct Node* cur = head;
while(cur)
{
struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
copy->val = cur->val;
copy->next = cur->next;
cur->next = copy;
cur = copy->next;
}
//2.控制拷贝结点的random
cur = head;
while(cur)
{
struct Node* copy = cur->next;
if(cur->random == NULL)
{
copy->random = NULL;
}
else
{
copy->random = cur->random->next;
}
cur = copy->next;
}
//3.尾插copy
struct Node* copyHead = NULL,*copyTail = NULL;
cur = head;
while(cur)
{
struct Node* copy = cur->next;
struct Node* next = copy->next;
if(copyTail == NULL)
{
copyHead = copyTail = copy;
}
else
{
copyTail->next = copy;
copyTail = copyTail->next;
}
cur->next = next;
cur = cur->next;
}
return copyHead;
}