C# 链表 --增 -删-反转-删除最小值

1.

Node.cs

namespace 链表
{
  public class Node<T>
  {
        public T Data;
        //这个就是地址
        public Node<T> Next;


        //构造函数用来初始化
        public Node()
        {
            Data = default(T);
            Next = null;
        }
        public Node(T value)
        {
            Data = value;
            Next = null;
        }
  }
}

2.LinkList.cs

namespace 链表
{   //一般链表都是有头部节点的,简称头结点,头结点不参与运算
   public class LinkList<T>
   {
        private Node<T> _head;
        private int _count;

        public LinkList()
        {
            _head = new Node<T>();
            _count = 0;
        }

        public void AddItem(Node<T> newNode)
        {
            Node<T> tmpNode = _head; //找到头结点
            while (tmpNode.Next != null)  //循环找到最后节点
            {
                tmpNode = tmpNode.Next; //一直下移
            }
            tmpNode.Next = newNode;  //将最后节点和即将插入的节点连接
            _count++;
        }

        public int GetLength()
        {
            return _count;
        }

        public void Insert(int index, Node<T> newNode)  //
        {
            if(index<0||index>_count)
            {
                Console.WriteLine("Over");
                return;
            }
            Node<T> tmpNode = _head;
            for (int i = 0; i < index; i++)
            {
                tmpNode = tmpNode.Next;
            }
            //tmpNode (index的前一个节点)
            newNode.Next = tmpNode.Next;
            tmpNode.Next = newNode;
            _count++;
        }
         //第一个为index,第二个为value
        public void ShowItem(Action<int,T> ac)
        {
            if (_count ==0)
            { Console.WriteLine("");
                return;
            }
            Node<T> tmpNode = _head.Next;
            for (int i = 0; i < _count; i++)
            {
                ac(i, tmpNode.Data);
                tmpNode = tmpNode.Next;
            }
        }

        public T RemoveAt(int index)
        {
            T returnValue = default(T); //  定义一个data的返回值
            if (index < 0 || index >= _count)//判断是否出界
            {
                Console.WriteLine("error");
                goto returnTip;
            }
            Node<T> tmpNode = _head; //删除节点的前一个节点
            for (int i = 0; i < index; i++)//循环走
            {
                tmpNode = tmpNode.Next;
            }
            Node<T> deleteNode = tmpNode.Next; //要删除的节点
            tmpNode.Next = tmpNode.Next.Next;//牵手删除节点的后一个节点
            deleteNode.Next = null;//不让其连接
            _count--;
            returnValue = deleteNode.Data;//返回删除节点的数据data
        returnTip:
            return returnValue;
        }

        public void Reverse()  //链表反转
        {
            if (_count < 1)
            {
                Console.WriteLine("链表长度不足");
                return;
            }
            Node<T> x1, x2;

            x2 = _head.Next;
            _head.Next = null;
            while (x2 != null)
            {
                x1 = x2.Next;
                x2.Next = _head.Next;
                _head.Next = x2;
                x2 = x1;
            }
        }
       
        public T RemoveMinDemo(Func<Node<T>,Node<T>,Boolean> _func)  //删除最小值
        {
            Node<T> deletePreMin, deleteMin, PreMin, Min;

            deletePreMin = PreMin = _head;
            deleteMin = Min = _head.Next;
            while (Min != null)
            {
                if (_func(Min,deleteMin))
                {
                    deletePreMin = PreMin;
                    deleteMin = Min;
                }
                PreMin = PreMin.Next;
                Min = Min.Next;
            }
            deletePreMin.Next = deletePreMin.Next.Next;
            deleteMin.Next = null;
            _count--;

            return deleteMin.Data;
        }

        public void Clear()
        {
            _head.Next = null;
            _count = 0;
        }

   }
}

3.Program.cs

namespace 链表
{
    class Program
    {
     public static bool SH(Node<int> a, Node<int> b)
     {
            if (a.Data > b.Data)
            {
                return false;
            }
            return true;
     }

        public static void Show(int index, int value)
        {
            Console.WriteLine("第{0}个元素是{1}",index+1,value);
        }

        static void Main(string[] args)
        {
            LinkList<int> linklist = new LinkList<int>();
            linklist.AddItem(new Node<int>(9));
            linklist.AddItem(new Node<int>(3));
            linklist.AddItem(new Node<int>(10));
            linklist.AddItem(new Node<int>(4));
            linklist.AddItem(new Node<int>(21));
            linklist.AddItem(new Node<int>(100));
       
            linklist.Reverse();//反转
            //9  3  10 4  21 100
            linklist.RemoveMinDemo(SH); //删除最小值
            linklist.ShowItem(Show);
            Console.WriteLine(linklist.GetLength());
            Console.ReadLine();
        }
    }
}

输出结果: 

5为链表的长度

猜你喜欢

转载自www.cnblogs.com/lk95/p/9909604.html