一发入魂双链表(十字链表)

前言

本篇主要介绍双链表,简易的讲就是你知道你前面的地址和你后面的地址,本篇涉及知识和上篇单链表一样,但是双链表在实现上比单链表要简单,抽象度不高,容易掌握。


一、双链表是什么?

1.双链表的引入是为了来弥补单链表的部分缺陷,在单链表插入删除时,总是要进行遍历,时间复杂度相对较高。对此,我们采取以空间换时间的策略引入双链表。
在这里插入图片描述

  1. 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都
    是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带
    来很多优势,实现反而简单了。

在这里插入图片描述


二、双链表的具体实现?

1、函数接口介绍

#pragma once

#include <iostream>

#include<cassert>

using namespace std;

typedef int DateType;

struct Node
{
	DateType value;
	struct Node* prev;
	struct Node* next;
};

Node* Init();

Node* BuyNode(DateType x);

void Push_back(Node* phead,DateType x);

void Push_front(Node* phead,DateType x);

void Pop_back(Node* phead);

void Pop_front(Node* phead);

Node* Find(Node* phead, DateType x);

void Erase(Node* pos);

void Insert(Node* pos, DateType x);

void Print(Node* phead);


2、函数具体实现

#include "Double_Linklist.h"

Node* Init()
{
	Node* phead = BuyNode(-1);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

void Push_back(Node* phead, DateType x)
{
	//Node* newnode = BuyNode(x);

	//Node* tail = phead->prev;

	//tail->next = newnode;

	//newnode->prev = tail;

	//newnode->next = phead;

	//phead->prev = newnode;
	Insert(phead->prev,x);
}

void Push_front(Node* phead, DateType x)
{
	//Node* newnode = BuyNode(x);

	//newnode->next = phead->next;

	//phead->next->prev = newnode;

	//newnode->prev = phead;

	//phead->next = newnode;
	Insert(phead, x);

}


Node* BuyNode(DateType x)
{
	Node* node = (Node*)malloc(sizeof(Node));

	if (node == NULL)
	{
		return NULL;
	}
	node->value = x;
	node->next = NULL;
	node->prev = NULL;
	return node;
}

void Pop_back(Node* phead)
{
	assert(phead->next != phead);
	//Node* tmp = phead->prev;
	//tmp->prev->next = phead;
	//phead->prev = tmp->prev;
	//free(tmp);
	Erase(phead->prev);
}

void Pop_front(Node* phead)
{
	//assert(phead->next!=phead);
	//Node* tmp = phead->next;
	//phead->next = tmp->next;
	//tmp->next->prev = phead;
	//free(tmp);
	Erase(phead->next);
}

void Insert(Node* pos, DateType x)//在pos后面插入
{
	assert(pos);
	Node* newnode = BuyNode(x);
	pos->next->prev = newnode;
	newnode->next = pos->next;
	newnode->prev = pos;
	pos->next = newnode;
}

void Erase(Node* pos)
{
	assert(pos);
	Node* tmp = pos->prev;
	tmp->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
}

Node* Find(Node* phead, DateType x)
{
	Node* cur = phead->next;

	while (cur!=phead)
	{
		if (cur->value == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}

void Print(Node* phead)
{
	assert(phead);
	Node* cur = phead->next;
	while (cur != phead)
	{
		cout << cur->value << " ";
		cur = cur->next;
	}
}

三、总结

双链表在具体实现的时最主要的是要搞清四条链接线,修改时注意顺序(在没有另设变量保存时),其他的和单链表相比还稍微简单一丢丢哦!


注:作者水平有限,如有错误,敬请指正!!!

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值