浅析数据结构系列(五)

5 篇文章 0 订阅
1 篇文章 0 订阅

之前的博文写过了,单链表的实现方式,今天补充一下双链表的实现方式吧!其实原理也很简单~
话不多说,直接撸码走起~

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啦,以上就是今天分享的双链表实现方式啦~ 代码里面有注释,基本可以看懂的哈~ 高手请绕道哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值