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

先定义一个泛型节点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

猜你喜欢

转载自www.cnblogs.com/myloadwls/p/9147732.html