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

先定义一个节点类Node<T>

public class Node<T>
    {
        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; }
        public void Add(Node<T> node)
        {
            if (Head == null)
            {
                Head = node;
                Count++;
                return;
            }
            var mod = Head;
            while (mod.Next != null)
            {
                mod = mod.Next;
            }
            mod.Next = node;
            Count++;
        }

        public void Delete(int index)
        {
            CheckIndex(index);
            var mod = Head;
            while (index > 1)
            {
                index--;
                mod = mod.Next;
            }
            mod.Next = mod.Next.Next;
            Count--;
        }

        public Node<T> Get(int index)
        {
            CheckIndex(index);
            var mod = Head;
            while (index > 0)
            {
                index--;
                mod = mod.Next;
            }
            return mod;
        }

        public void Set(Node<T> node, int index)
        {
            CheckIndex(index);
            var mod = Head;
            while (index > 1)
            {
                index--;
                mod = mod.Next;
            }
            node.Next = mod.Next.Next;
            mod.Next = node;
        }

        public void Insert(Node<T> node, int index)
        {
            CheckIndex(index);
            var mod = Head;
            while (index > 1)
            {
                index--;
                mod = mod.Next;
            }
            node.Next = mod.Next;
            mod.Next = node;
            Count++;
        }

        public void Foreach(Action<Node<T>> act)
        {
            var mod = Head;
            while (mod != null)
            {
                act(mod);
                mod = mod.Next;
            }
        }

        void CheckIndex(int index)
        {
            if (Count < index + 1)
            {
                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>("---"), 3);
            list.Set(new Node<string>("+++"), 4);
            Console.WriteLine(list.Get(3).Current);
            list.Delete(4);
            list.Foreach(act);
            Console.ReadKey();
View Code

 

posted on 2018-06-06 21:28  anywls  阅读(134)  评论(0)    收藏  举报