首页 > Windows开发 > 详细

数据结构笔记-双链表(C#)

时间:2018-06-06 23:31:18      阅读:275      评论:0      收藏:0      [点我收藏+]

先定义一个泛型节点Node<T>

技术分享图片
    public class Node<T>
    {
        public Node<T> Prev { set; get; }
        public T Current { set; get; }
        public Node<T> Next { set; get; }
        public Node()
        {
            Current = default(T);
        }
        public Node(T node)
        {
            Current = node;
        }
    }
View Code

再定义一个泛型链表NodeList<T>

技术分享图片
    public class NodeList<T>
    {
        public Node<T> Head { set; get; }
        public int Count { private set; get; }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="node"></param>
        public void Add(Node<T> node)
        {
            if (Head == null)
            {
                Head = node;
                Count++;
                return;
            }
            if (Head.Prev == null)
            {
                Head.Prev = node;
                Head.Next = node;
                node.Next = Head;
                node.Prev = Head;
                Count++;
                return;
            }
            var mod = Head.Prev;
            mod.Next = node;
            node.Prev = mod;
            node.Next = Head;
            Head.Prev = node;
            Count++;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="index"></param>
        public void Delete(int index)
        {
            CheckIndex(index);
            var mod = Get(index);
            mod.Prev.Next = mod.Next;
            mod.Next.Prev = mod.Prev;
            Count--;
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Node<T> Get(int index)
        {
            CheckIndex(index);
            //正向查找
            if (index < Count / 2)
            {
                var mod1 = Head;
                for (int i = 0; i < index; i++)
                {
                    mod1 = mod1.Next;
                }
                return mod1;
            }
            //反向查找
            var mod2 = Head;
            var rindex = Count - index;
            for (int i = 0; i < rindex; i++)
            {
                mod2 = mod2.Prev;
            }
            return mod2;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="node"></param>
        /// <param name="index"></param>
        public void Set(Node<T> node, int index)
        {
            CheckIndex(index);
            var mod = Get(index);
            mod.Prev.Next = node;
            mod.Next.Prev = mod;

            node.Prev = mod.Prev;
            node.Next = mod.Next;
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="node"></param>
        /// <param name="index"></param>
        public void Insert(Node<T> node, int index)
        {
            CheckIndex(index);
            var mod = Get(index);
            node.Prev = mod.Prev;
            node.Next = mod;
            node.Prev.Next = node;
            mod.Prev = node;
            Count++;
        }

        /// <summary>
        /// 遍历元素
        /// </summary>
        /// <param name="act"></param>
        public void Foreach(Action<Node<T>> act)
        {
            var mod = Head;
            for (int i = 0; i < Count; i++)
            {
                if (i == 0)
                {
                    act(Head);
                    mod = Head.Next;
                    continue;
                }
                act(mod);
                mod = mod.Next;
            }
        }

        void CheckIndex(int index)
        {
            if (index+1 > Count)
            {
                throw new IndexOutOfRangeException("Index out of range");
            }
        }
    }
View Code

最后是Console的测试代码

技术分享图片
            var list = new NodeList<string>();
            list.Add(new Node<string>("111"));
            list.Add(new Node<string>("222"));
            list.Add(new Node<string>("333"));
            list.Add(new Node<string>("444"));
            var act = new Action<Node<string>>(it => {
                Console.WriteLine(it.Current);
            });


            list.Insert(new Node<string>("---"), 1);
            list.Set(new Node<string>("+++"), 3);
            Console.WriteLine(list.Get(3).Current);
            list.Delete(2);
            list.Foreach(act);
            Console.ReadKey();        
View Code

 

数据结构笔记-双链表(C#)

原文:https://www.cnblogs.com/myloadwls/p/9147732.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!