双向循环链表(附图解)

本文详细介绍了双向循环链表的特点,包括前指针和哨兵位,以及如何实现随机插入和删除节点的操作,包括Find、Insert和Eraser函数的代码示例。
摘要由CSDN通过智能技术生成

前言:双向循环链表与单向链表的区别是双向链表中有一个前指针,可以指向前面一个链表的地址,最后一个指针指向哨兵位的地址

哨兵位就是相当于一个头节点,但是它只是起到一个链接作用,只负责链接

这里我就画随机插入和删除节点的图,其他的图都是类似就不做过多说明

随机插入需要和寻找相配合(在pos节点的前面插入)

SL* Find(SL* phead,LTDatatype x)//寻找数据
{
	SL* tail = phead;
	while (tail->data!= x)
	{
		tail = tail->next;
	}
	return tail;
}



void Insert(SL* phead, SL* pos, LTDatatype x)//访问位置插入
{
	SL* tail = phead;
	SL* Newnode = Buynewnode(x);
	while (tail->next!= pos)
	{
		tail = tail->next;
	}
	Newnode->next = pos;
	Newnode->prev = pos->prev;
	pos->prev = Newnode;
	tail->next = Newnode;
}

随机删除需要和寻找相配合(在pos节点的前面删除)

void Eraser(SL* phead, SL* pos)//删除任意位置数据
{
	SL* tail = phead;
	while (tail->next != pos)
	{
		tail = tail->next;
	}
	SL* after = pos->next;
	after->prev = tail;
	tail->next = after;
	free(pos);
	pos->next = NULL;
	pos->prev = NULL;
}

下面就是全部源码,与单链表类似但是不完全相同

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int LTDatatype;
typedef struct SLTlist
{
	LTDatatype data;
	struct SLTlist* prev;
	struct SLTlist* next;
}SL;


SL* Initlist();//初始化
void PushBack(SL* phead,LTDatatype x);//尾插
void PopBack(SL* phead);//尾删
void PushFront(SL* phead, LTDatatype x);//头插
void PopFront(SL* phead);//头删
void Insert(SL* phead, SL* pos, LTDatatype x);//访问位置插入
void Eraser(SL* phead, SL* pos);//删除任意位置数据
SL* Find(SL* phead,LTDatatype x);//寻找数据
void Print(SL* phead);//打印数据
void Distroy(SL* phead);//销毁数据





#define _CRT_SECURE_NO_WARNINGS 1
#include"SldList.h"

int main()
{
	SL* guard = Initlist();
	PushBack(guard,6);
	PushBack(guard, 5);
	PushFront(guard,8);
	PushBack(guard, 7);
	Insert(guard, Find(guard,8),10);
	Eraser(guard, Find(guard, 5));
	PopFront(guard);
	Print(guard);

	return 0;
}



#define _CRT_SECURE_NO_WARNINGS 1
#include"SldList.h"


SL* Buynewnode(LTDatatype x)
{
	SL* Newnode = (SL*)malloc(sizeof(SL));
	Newnode->data = x;
	Newnode->prev = NULL;
	Newnode->next = NULL;

	return Newnode;
}




SL* Initlist()//初始化
{
	SL* guard = (SL*)malloc(sizeof(SL));
	guard->prev = guard;
	guard->next = guard;
	guard->data= -1;
	return guard;
}


void Print(SL* phead)//打印数据
{
	SL* tail = phead;
	while (tail->next != phead)
	{
		tail = tail->next;
		printf("%d->", tail->data);
	}

}

void Distroy(SL* phead)//销毁数据
{
	SL* tail = phead;
	SL* cur = phead;
	while (tail->next != phead)
	{
		cur = tail->next;
		free(tail);
		tail = cur;
	}
}




void PushBack(SL* phead, LTDatatype x)//尾插
{
	//SL* tail = phead;
	//SL* Newnode = Buynewnode(x);
	//while (tail->next!=phead)
	//{
	//	tail = tail->next;
	//}
	//Newnode->prev = tail;
	//Newnode->next = phead;
	//phead->prev = Newnode;
	//tail->next = Newnode;
	SL* end = phead->prev;
	Insert(phead, Find(phead,end->data),x);
}


void PopBack(SL* phead)//尾删
{
	assert(phead);
	assert(phead->next!=phead->prev);
	//SL* tail = phead;
	//while (tail->next!= phead)
	//{
	//	tail = tail->next;
	//}
	//SL* prevtail = tail->prev;
	//phead->prev = prevtail;
	//prevtail->next = tail->next;
	//free(tail);
	//tail->next = NULL;
	//tail->prev = NULL;
	SL* end = phead->prev;
	Eraser(phead,Find(phead, end->data));
}


void PushFront(SL* phead, LTDatatype x)//头插
{
	//assert(phead);
	//SL* tail = phead;
	//SL* Newnode = Buynewnode(x);
	//SL* first = tail->next;
	//tail->next = Newnode;
	//Newnode->prev = tail;
	//Newnode->next = first;
	//first->prev = Newnode;
	SL* first = phead->next;
	Insert(phead, Find(phead, first->data),x);
}

void PopFront(SL* phead)//头删
{
	//方法1:
	//assert(phead->next!=phead);
	//SL* tail = phead;
	//SL* first = phead->next;
	//SL* second = first->next;
	//tail->next =second;
	//second->prev = tail;
	//free(first);
	//first->next = NULL;
	//first->prev = NULL;
	//方法2:
	SL* first = phead->next;
	Eraser(phead,Find(phead,first->data));//删除任意位置数据
}


SL* Find(SL* phead,LTDatatype x)//寻找数据
{
	SL* tail = phead;
	while (tail->data!= x)
	{
		tail = tail->next;
	}
	return tail;
}


void Insert(SL* phead, SL* pos, LTDatatype x)//访问位置插入
{
	SL* tail = phead;
	SL* Newnode = Buynewnode(x);
	while (tail->next!= pos)
	{
		tail = tail->next;
	}
	Newnode->next = pos;
	Newnode->prev = pos->prev;
	pos->prev = Newnode;
	tail->next = Newnode;
}

void Eraser(SL* phead, SL* pos)//删除任意位置数据
{
	SL* tail = phead;
	while (tail->next != pos)
	{
		tail = tail->next;
	}
	SL* after = pos->next;
	after->prev = tail;
	tail->next = after;
	free(pos);
	pos->next = NULL;
	pos->prev = NULL;
}



  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙猫不是一只猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值