ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

浅析数据结构系列(五)

2020-02-28 18:45:18  阅读:258  来源: 互联网

标签:数据结构 系列 next pNode Tail prev null DoubleLinkNode 浅析


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

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

@LYZY 发布了47 篇原创文章 · 获赞 55 · 访问量 3万+ 私信 关注

标签:数据结构,系列,next,pNode,Tail,prev,null,DoubleLinkNode,浅析
来源: https://blog.csdn.net/qq_37601496/article/details/104561324

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有