双向循环链表的创建,删除,插入,摧毁等操作(超详细)…… 〃•ω‹〃 (⌯꒪꒫꒪)੭

本文详细介绍了双向循环链表的实现,包括头文件定义、节点创建、打印输出、头尾插入与删除、查找节点以及链表的初始化、销毁和插入/删除操作。
摘要由CSDN通过智能技术生成

目录

一.头文件:List.h

 二.函数实现(引入头文件):

1.创建节点

2.打印输出

3.头插与尾插

4.头删与尾删

5.查找节点

6.插入与删除

7.删除链表


引入:带头双向循环链表:结构比较复杂,一般用在单链表存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然复杂,但是使用代码实现后就会发现结构会带来很多优势,实现起来反而简单了。 那让我们开始吧~

一.头文件:List.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int LTDataType;//对双向循环链表创建的声明
typedef struct ListNode
{
	struct ListNode* next;
	struct ListNode* prev;
	LTDataType data;
}ListNode;
//创建头结点
ListNode* ListInit();
//打印输出
void ListPrint(ListNode* phead);
//摧毁双向链表
void ListDestory(ListNode* phead);
//头插尾插
void ListPushFront(ListNode* phead, LTDataType x);
void ListPushBack(ListNode* phead, LTDataType x);
//头删尾删
void ListPopFront(ListNode* phead);
void ListPopBack(ListNode* phead);
//查找
ListNode* ListFind(ListNode* phead, LTDataType x);
//插入与删除节点
void ListInsert(ListNode* pos, LTDataType x);
void ListErase(ListNode* pos);


 二.函数实现(引入头文件):

1.创建节点

#include"List.h"

//创建节点
ListNode* ListBuyNode(LTDataType x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	newnode->data = x;
	newnode->prev = NULL;
	newnode->next = NULL;
	return newnode;
}
//创建头结点
ListNode* ListInit()
{
	ListNode* phead = ListBuyNode(0);
	phead->next = phead;//使节点指向本身,创建双向循环链表的头节点
	phead->prev = phead;
	return phead;
}

2.打印输出

//打印输出
void ListPrint(ListNode* phead)
{
	assert(phead);
	ListNode* cur = phead->next;
	while (cur != phead)//注意双向循环链表的结束标志不是NULL
	{
		pirntf("%d", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

3.头插与尾插

尾插

头插

//尾插
void ListPushBack(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* tail = phead->prev;
	ListNode* newnode = ListBuyNode(x);

	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
}
//头插
void ListPushFront(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* first = phead->next;
	ListNode* newnode = ListBuyNode(x);
	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = first;
	first->prev = newnode;
}

4.头删与尾删

头删

 尾删

//头删
void ListPopFront(ListNode* phead)
{
    assert(phead);
    ListNode* first = phead->next;
    ListNode* second = first->next;
    phead->next = second;
    second->prev = phead;
    free(first);//防止空指针的出现
    first->next = NULL;
}
//尾删
void ListpopBack(ListNode* phead)
{
    assert(phead);
    assert(phead->next != phead);//防止头结点被删除
    ListNode* tail = phead->prev;
    ListNode* prev = tail->prev;
    prev->next = phead;
    phead->prev = prev;
    free(tail);//防止空指针的出现
    tail->next = NULL;
}

5.查找节点

//查找节点
ListNode* ListFind(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data = x) 
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

6.插入与删除

 插入

删除

//插入节点
void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* prev = pos->prev;
	ListNode* newnode = ListBuyNode(x);
	prev->next = newnode;
	newnode->prev = prev;
	newnode->next = pos;
	pos->prev = newnode;
}
//删除节点
void ListErase(ListNode* pos)
{
	assert(pos);
	ListNode* prev = pos->prev;
	ListNode* next = pos->next;
	prev->next = next;
	next->prev = prev;
	free(pos);
	pos->next = NULL;
}

7.删除链表

//删除链表
void LsitDestory(ListNode* phead)
{
	assert(phead);
	ListNode* cur = phead->next;
	while (cur != phead)
	{
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;
}

博客到这里也是结束了,喜欢的小伙伴可以点赞加关注支持下博主,这对我真的很重要~~ 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值