之前的博文写过了,单链表的实现方式,今天补充一下双链表的实现方式吧!其实原理也很简单~
话不多说,直接撸码走起~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleLinkList
{
public class DoubleLinkNode<T> where T : class, new()
{
/// <summary>
/// 前一个节点
/// </summary>
public DoubleLinkNode<T> prev = null;
/// <summary>
/// 后一个节点
/// </summary>
public DoubleLinkNode<T> next = null;
/// <summary>
/// 当前节点
/// </summary>
public T t = null;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleLinkList
{
public class DoubleLinkedList<T> where T : class, new()
{
/// <summary>
/// 表头
/// </summary>
public DoubleLinkNode<T> Head = null;
/// <summary>
/// 表尾
/// </summary>
public DoubleLinkNode<T> Tail = null;
/// <summary>
/// 链表的元素个数
/// </summary>
private int m_Count = 0;
public int Count
{
get { return m_Count; }
}
/// <summary>
/// 添加元素到头结点
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public DoubleLinkNode<T> AddToHeader(T t)
{
DoubleLinkNode<T> node = new DoubleLinkNode<T>();
node.prev = null;
node.next = null;
node.t = t;
return AddToHeader(node);
}
/// <summary>
/// 添加节点到头节点
/// </summary>
/// <param name="pNode"></param>
/// <returns></returns>
public DoubleLinkNode<T> AddToHeader(DoubleLinkNode<T> pNode)
{
if (pNode == null) return null;
pNode.prev = null;
if (Head == null)
{
Head = pNode;
Tail = pNode;
}
else
{
Head.prev = pNode.next;
pNode.next = Head;
Head = pNode;
}
++m_Count;
return Head;
}
/// <summary>
/// 添加元素到尾节点
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public DoubleLinkNode<T> AddToTail(T t)
{
DoubleLinkNode<T> node = new DoubleLinkNode<T>();
node.prev = null;
node.next = null;
node.t = t;
return AddToTail(node);
}
/// <summary>
/// 添加元素到尾节点
/// </summary>
/// <param name="pNode"></param>
/// <returns></returns>
public DoubleLinkNode<T> AddToTail(DoubleLinkNode<T> pNode)
{
if (pNode == null) return null;
pNode.next = null;
if (Tail == null)
{
Tail = pNode;
Head = pNode;
}
else
{
Tail.next = pNode;
pNode.prev = Tail;
Tail = pNode;
}
++m_Count;
return Tail;
}
/// <summary>
/// 移除某个节点
/// </summary>
/// <param name="pNode"></param>
public void RemoveNode(DoubleLinkNode<T> pNode)
{
if (pNode == null) return;
if (pNode == Head)
{
Head = pNode.next;
}
if (pNode == Tail)
{
Tail = pNode.prev;
}
if (pNode.prev != null)
{
pNode.prev.next = pNode.next;
}
if (pNode.next != null)
{
pNode.next.prev = pNode.prev;
}
pNode.next = pNode.prev = null;
pNode.t = null;
--m_Count;
}
/// <summary>
/// 移动节点作为头结点
/// </summary>
/// <param name="pNode"></param>
public void MoveToHead(DoubleLinkNode<T> pNode)
{
if (pNode == null || Head == null) return;
if (pNode.prev == null && pNode.next == null) return;
if (pNode == Tail) Tail = pNode.next;
if (pNode.prev != null)
{
pNode.prev.next = pNode.next;
}
if (pNode.next != null)
{
pNode.next.prev = pNode.prev;
}
pNode.prev = null;
pNode.next = Head;
Head = pNode;
if (Tail == null)
{
Tail = Head;
}
}
}
}
好i啦,以上就是今天分享的双链表实现方式啦~ 代码里面有注释,基本可以看懂的哈~ 高手请绕道哦~