数据结构入门——链表(SeqList)详解(初始化、增、删、查、改)

一. 链表的介绍

1.1 链表的概念及结构

概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表
中的指针链接次序实现的 。
在这里插入图片描述
注意三点:

  1. 由图可以看出链表的结构在逻辑上是连续的,但是在内存中不一定连续。
  2. 链表的结点是在堆上开辟的。
  3. 在堆上开辟的两段空间,可能连续,也可能不连续。

1.2 链表的分类

单向与双向
单向链表
在这里插入图片描述

双向链表
在这里插入图片描述
带头与不带头
带头
在这里插入图片描述
不带头
在这里插入图片描述
循环与不循环

循环
在这里插入图片描述
不循环
在这里插入图片描述
将这三类排列组合可以形成8种链表
我们主要讲解两类
1.单向不带头不循环链表
2.双向带头循环链表

二. 单向不带头不循环链表

在这里插入图片描述
无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结
构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。

2.1 单链表的实现代码

大概的结构可以用以下代码展示:
在这里插入图片描述
data代表的是每一个节点存储的数据,next代表的是指向下一个结点的指针,如果没有下一个结点,那么next就为空。

2.1.1 头文件——SList.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDateType;
typedef struct SListNode
{
    
    
	SLTDateType data;
	struct SListNode* next;
}SListNode;

// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x);
//创建一个有n个节点的单链表 
SListNode* CreatSListNode(int n);
// 单链表打印
void SListPrint(SListNode* plist);
// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x);
// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x);
// 单链表的尾删
void SListPopBack(SListNode** pplist);
// 单链表头删
void SListPopFront(SListNode** pplist);
// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x);
// 单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDateType x);
// 单链表在pos位置之前插入x
void SListInsertBefore(SListNode** plist, SListNode* pos, SLTDateType x);
// 单链表删除pos之后位置
void SListEraseAfter(SListNode* pos);
//单链表删除pos位置
void SListErase(SListNode** pplist, SListNode* pos);
// 单链表的销毁
void SListDestroy(SListNode** plist);

2.1.2 具体函数实现文件——SList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x)
{
    
    
	SListNode* phead = (SListNode*)malloc(sizeof(SListNode));
	if (phead == NULL)
	{
    
    
		perror("malloc failed");
		exit(-1);
	}
	else
	{
    
    
		phead->data = x;
		phead->next = NULL;
	}
	return phead;
}
//创建一个有n个节点的单链表 
SListNode* CreatSListNode(int n)
{
    
    
	SListNode* phead = NULL, * ptail = NULL;
	for (int i = 0; i < n; i++)
	{
    
    
		SListNode* NewNode = BuySListNode(i);
		if (phead == NULL)
		{
    
    
			ptail = phead = NewNode;
		}
		else
		{
    
    
			ptail->next = NewNode;
			ptail = NewNode;
		}
	}
	return phead;
}

// 单链表打印
void SListPrint(SListNode* plist)
{
    
    
	SListNode* cur = plist;
	while (cur)
	{
    
    
		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)
		{
    
    
			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)
{
    
    
	//链表为空就删不了了
	assert(*pplist);
	SListNode* tail = *pplist;
	//一上来就已经是尾
	if (tail->next == NULL)
	{
    
    
		free(tail);
		*pplist = NULL;
	}
	else
	{
    
    
		方法1
		//while (tail->next->next)
		//{
    
    
		//	tail = tail->next;
		//}
		//free(tail->next);
		//tail->next = NULL;

		//方法2
		SListNode* pre = NULL;
		while (tail->next)
		{
    
    
			pre = tail;
			tail = tail->next;
		}
		free(tail);
		pre->next = NULL;
	}
}

// 单链表头删
void SListPopFront(SListNode** pplist)
{
    
    
	assert(*pplist);
	SListNode* tmp = (*pplist)->next;
	free(*pplist);
	*pplist = tmp;
}

// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
    
    
	SListNode* cur = plist;
	while (cur)
	{
    
    
		if (cur->data == x)
		{
    
    
			return cur;
		}
		else
		{
    
    
			cur = cur->next;
		}
	}
}
// 单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
    
    
	if (pos == NULL)
	{
    
    
		return;
	}
	else
	{
    
    
		SListNode* newnode = BuySListNode(x);
		newnode->next = pos->next;
		pos->next = newnode;
	}
}
// 单链表在pos位置之前插入x
void SListInsertBefore(SListNode** pplist, SListNode* pos, SLTDateType x)
{
    
    
	if (*pplist == pos)
	{
    
    
		SListPushFront(pplist, x);
	}
	else
	{
    
    
		SListNode* newnode = BuySListNode(x);
		SListNode* pre = *pplist;
		while (pre->next != pos)
		{
    
    
			pre = pre->next;
		}
		newnode->next = pre->next;
		pre->next = newnode;

	}
}
// 单链表删除pos之后位置
void SListEraseAfter(SListNode* pos)
{
    
    
	assert(pos);
	if (pos->next == NULL)
	{
    
    
		return;
	}
	else
	{
    
    
		SListNode* tmp = pos->next;
		pos->next = pos->next->next;
		free(tmp);
	}
}
// 单链表删除pos当前位置
void SListErase(SListNode** pplist, SListNode* pos)
{
    
    
	assert(pos && *pplist);
	if (*pplist == pos)
	{
    
    
		SListPopFront(pplist);
	}
	else
	{
    
    
		SListNode* cur = *pplist;
		while (cur->next != pos)
		{
    
    
			cur = cur->next;
		}
		cur->next = pos->next;
		free(pos);
	}
}
// 单链表的销毁
void SListDestroy(SListNode** pplist)
{
    
    
	SListNode* pre = *pplist;
	SListNode* cur = *pplist;
	while (cur->next)
	{
    
    
		pre = cur->next;
		free(cur);
		cur = pre;
	}
	*pplist = NULL;
}

关于程序的测试,大家可以自己写一个主函数进行测试。

三. 双向带头循环链表

在这里插入图片描述
带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都
是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带
来很多优势,实现反而简单了,后面我们代码实现了就知道了。

3.1 双向带头循环链表的实现代码

结构声明的代码:
在这里插入图片描述
双向带头循环链表的结构相对复杂,但是各个功能的实现其实是很简单的。接下来请看代码

3.1.1 头文件——DList.h

#pragma once
//头文件包含
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

//结构声明
typedef int DLTDataType;//声明存储的数据类型
typedef struct DoubleListNode
{
    
    
	DLTDataType data;
	struct DoubleListNode* prev;
	struct DoubleListNode* next;
}DLTNode;//声明链表的节点

//函数声明

//创建新节点
DLTNode* BuyNode(DLTDataType x);
//初始化链表
DLTNode* DLTInit();
//打印链表
void DLTPrint(DLTNode* phead);
//链表头插
void DLTPushFront(DLTNode* phead, DLTDataType x);
//链表头删
void DLTPopFront(DLTNode* phead);
//链表尾插
void DLTPushBack(DLTNode* phead, DLTDataType x);
//链表尾删
void DLTPopBack(DLTNode* phead);
//链表查找
DLTNode* DLTFind(DLTNode* phead, DLTDataType x);
//在链表的pos节点前插入数据
void DLTInsertBefore(DLTNode* phead, DLTNode* pos, DLTDataType x);
//删除链表中pos节点的数据
void DLTErase(DLTNode* phead, DLTNode* pos);

3.1.2 具体函数实现——DList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "DList.h"
//创建新节点
DLTNode* BuyNode(DLTDataType x)
{
    
    
	DLTNode* newNode = (DLTNode*)malloc(sizeof(DLTNode));
	if (newNode == NULL)
	{
    
    
		perror("malloc failed");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;
	newNode->prev = NULL;
	return newNode;
}
//初始化链表
DLTNode* DLTInit()
{
    
    
	DLTNode* phead = BuyNode(-1);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}
//打印链表
void DLTPrint(DLTNode* phead)
{
    
    
	assert(phead);
	DLTNode* cur = phead->next;
	while (cur != phead)
	{
    
    
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
//链表头插
void DLTPushFront(DLTNode* phead, DLTDataType x)
{
    
    
	assert(phead);
	//开辟新节点
	DLTNode* newNode = BuyNode(x);
	//保存原本的第一个节点
	DLTNode* tmp = phead->next;
	//更新链表
	phead->next = newNode;
	newNode->prev = phead;
	newNode->next = tmp;
	tmp->prev = newNode;
}
//链表头删
void DLTPopFront(DLTNode* phead)
{
    
    
	assert(phead);
	assert(phead->next != phead);
	//保存要删除的节点
	DLTNode* tmp = phead->next;
	//更新链表
	phead->next = tmp->next;
	tmp->next->prev = phead;
	//删除保存的节点
	free(tmp);
}
//链表尾插
void DLTPushBack(DLTNode* phead, DLTDataType x)
{
    
    
	assert(phead);
	//申请新节点
	DLTNode* newNode = BuyNode(x);
	//保存原本的尾节点
	DLTNode* tmp = phead->prev;
	//更新链表
	tmp->next = newNode;
	newNode->prev = tmp;
	phead->prev = newNode;
	newNode->next = phead;
}
//链表尾删
void DLTPopBack(DLTNode* phead)
{
    
    
	assert(phead);
	assert(phead->next != phead);
	//保存尾结点
	DLTNode* tmp = phead->prev;
	//将尾结点的上一个节点更新为新的尾结点
	tmp->prev->next = phead;
	phead->prev = tmp->prev;
	//释放原本的尾结点
	free(tmp);
}
//链表查找
DLTNode* DLTFind(DLTNode* phead, DLTDataType x)
{
    
    
	assert(phead);
	DLTNode* cur = phead->next;
	while (cur != phead)
	{
    
    
		if (cur->data == x)
		{
    
    
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

//在链表的pos节点前插入数据
void DLTInsertBefore(DLTNode* phead, DLTNode* pos, DLTDataType x)
{
    
    
	assert(phead);
	assert(pos);
	//申请一个新节点
	DLTNode* newNode = BuyNode(x);
	//更新链表
	pos->prev->next = newNode;
	newNode->prev = pos->prev;
	newNode->next = pos;
	pos->prev = newNode;

}
//删除链表中pos节点的数据
void DLTErase(DLTNode* phead, DLTNode* pos)
{
    
    
	assert(phead);
	assert(pos);
	assert(phead->next != phead);
	DLTNode* tmp = pos->next;
	pos->prev->next = tmp;
	tmp->prev = pos->prev;
	free(pos);
}

四. 顺序表与链表的对比

在这里插入图片描述

五. 总结

其实主要的思想还是提现在代码上,感谢大家耐心看完。之后的一篇博客会讲一下关于链表的OJ题。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_72482689/article/details/127779966
今日推荐