用C#实现链表的一些功能

 用C#实现链表的一些功能 

    public class Node<T>

    {

        public T data;

 

        public Node()

        {

        }

        public Node(T data)

        {

            this.data = data;

        }

 

        public Node<T> next = null;

    }

 

    public class Link<T>

    {

        //定义链表的头节点

        private Node<T> head;

 

        //链表初始化

        public Link()

        {

            head = null;

        }

 

        /// <summary>

        /// 链表的长度,遍历整个链表,直到链表结尾

        /// </summary>

        /// <returns>链表的长度</returns>

        public int Count()

        {

            //定义一个节点

            Node<T> p = head;

 

            int count = 0;//链表计数器

 

            //遍历链表

            while (p != null)

            {

                count++;

                p = p.next;//移到下一个节点

            }

 

            return count;

        }

 

        /// <summary>

        /// 取链表的第i个节点的值

        /// </summary>

        /// <param name="i">i个节点</param>

        /// <returns>i个节点的值</returns>

        public T GetElem(int i)

        {

            //定义一个节点

            Node<T> p = head;

 

            int k = 0;//计数器

 

            //如果i大于链表的长度或i小于0,则报出异常

            if (i >= Count() || i < 0)

            {

                throw new Exception("Error");

            }

 

            //如果i大于0且小于链表长度,则遍历链表直到第i个节点为止

            while (k < i)

            {

                k++;

                p = p.next;

            }

 

            return p.data;

        }

 

 

        /// <summary>

        /// 在链表的第i个位置上插入新的节点

        /// </summary>

        /// <param name="e">要插入节点的值</param>

        /// <param name="i">链表的第i个节点</param>

        public void Insert(T e, int i)

        {

            Node<T> p = new Node<T>(e);//要插入的节点

 

            Node<T> q = head;

 

            int k = 0;//计数器

 

            //如果i大于链表的长度或i小于0,则报出异常

            if (i >= Count() || i < 0)

            {

                System.Console.WriteLine("Error");

                return;

            }

 

            //如果在链表头插入,移动头指针

            if (i == 0)

            {

                p.next = head;

                head = p;

                return;

            }

 

 

            //遍历链表直到第i-1个节点为止

            while (k < i - 1)

            {

                k++;

                q = q.next;

            }

 

            //把新节点插入在第i个节点的位置

            p.next = q.next;

            q.next = p;

 

        }

 

 

        /// <summary>

        /// 删除第i个节点

        /// </summary>

        /// <param name="i">链表的第i个节点</param>

        public void RemoveAt(int i)

        {

            Node<T> p = head;

 

            int k = 0;//计数器

 

            //如果i大于链表的长度或i小于0,则报出异常

            if (i < 0 || i >= Count())

            {

                System.Console.WriteLine("Error");

                return;

            }

 

            //如果删除链表头,移动头指针

            if (i == 0)

            {

                head.next = head.next.next;

                return;

            }

 

            //遍历链表直到第i-1个节点为止

            while (k < i - 1)

            {

                k++;

                p = p.next;

            }

 

            //删除第i个节点

            p.next = p.next.next;

        }

 

 

        /// <summary>

        /// 在链表尾加入一个新的节点

        /// </summary>

        /// <param name="e">新的节点的值</param>

        public void Add(T e)

        {

            Node<T> p = new Node<T>(e);//创建一个新的节点

 

            Node<T> q = new Node<T>();

 

            //如果链表为空,则将新节点赋给头指针

            if (head == null)

            {

                head = p;

                return;

            }

 

            /*---------------------------------------

             * 如果链表不为空,则在链表尾加入新的节点

             ----------------------------------------*/

           

            //从头指针开始遍历,直至链表尾

            q = head;

 

            while (q.next != null)

            {

                q = q.next;

            }

            //在链表尾插入新节点

            q.next = p;

        }

 

 

 

        /// <summary>

        /// 查找某个元素在链表中第一次出现的位置

        /// </summary>

        /// <param name="e">要查找的元素</param>

        /// <returns>这个元素在链表中第一次出现的位置的索引</returns>

        public int IndexOf(T e)

        {

            Node<T> p = head;

           

            int k = 0;//计数器

 

            /*------------------------------------

             * 遍历整个链表,直到找到第一个节点的值

             * 与该元素相等退出,并返回相应的位置索

             * 引。如果没有找到,则返回-1

             -------------------------------------*/

           

            //从头指针开始遍历,找到退出,并返回相应的位置索引

            while (p.next != null)

            {

                if (p.data.Equals(e))

                {

                    return k;

                }

                k++;

                p = p.next;

            }

            if (!p.data.Equals(e))

            {

                k++;

            }

 

            //如果没有找到,则返回-1

            return k >= Count() ? -1 : k;

        }

 

 

        /// <summary>

        /// 查找某个元素在链表中最后一次出现的位置

        /// </summary>

        /// <param name="e">要查找的元素</param>

        /// <returns>这个元素在链表中最后一次出现的位置的索引</returns>

        public int LastIndexOf(T e)

        {

            Node<T> p = head;

           

            int index = -1;//最后一次出现的位置索引

           

            int k = 0;//计数器

 

            /*------------------------------------------------

             * 遍历整个链表,直到链表结束,每发现相应节点的值

             * 与该元素相等,则将该节点的位置索引赋给index

             * 这样index的值就是最后一次的值。如果没有,则返

             * -1

             -------------------------------------------------*/

          

            while (p.next != null)

            {

                if (p.data.Equals(e))

                {

                    index = k;

                }

                k++;

                p = p.next;

            }

 

            if (p.data.Equals(e))

            {

                index = k;

            }

 

            return index;

        }

 

 

        /// <summary>

        /// 判断链表是否为空

        /// </summary>

        /// <returns></returns>

        public bool Empty()

        {

            return head == null ? true : false;

        }

 

 

 

        /// <summary>

        /// 清空链表

        /// </summary>

        public void Clear()

        {

            head = null;

        }

 

       

       

       

        /// <summary>

        /// 将链表转成数组

        /// </summary>

        /// <returns>转换后的数组</returns>

        public T[] ToArray()

        {

            T[] array = new T[Count()];//定义一个与链表长度相同的数组

           

            /*------------------------------------------------

             * 遍历链表,将链表的每个节点的值放到相应的数组里

             ------------------------------------------------*/

            Node<T> p = head;

 

            int i = 0;//数组下标计数器

 

            while (p.next != null)

            {

                array[i++] = p.data;

                p = p.next;

            }

            array[Count() - 1] = p.data;

           

            return array;

        }

 

        /// <summary>

        /// 将一个数组加到链表中

        /// </summary>

        /// <param name="a">要加入链表的数组</param>

        public void AddRange(T[] a)

        {

            //遍历整个数组,将数组中的每个元素作为一个新的节点加入到链表中

            for (int i = 0; i < a.Length; i++)

            {

                Add(a[i]);

            }

        }

 

     

        /// <summary>

        /// 删除链表中值为某个元素的所有节点

        /// </summary>

        /// <param name="e">要删除节点的值</param>

        public void Remove(T e)

        {

           

            //如果头指针的值等于这个元素,则删这个结点,并将头指针后移

            while (head.data.Equals(e))

            {

                head = head.next;

            }

            //如果不是头指针,则删除该节点

            Node<T> p = head;

 

            while (p.next.next != null)

            {

                if (p.next.data.Equals(e))

                {

                    p.next = p.next.next;

                    continue;

                }

                p = p.next;

            }

 

            if (p.next.data.Equals(e))

            {

                p.next = null;

            }

 

        }

 

 

 

        /// <summary>

        /// 将链表中所有为某个值的节点替换为另一个值

        /// </summary>

        /// <param name="first">被替换的值</param>

        /// <param name="second">替换的值</param>

        public void Replace(T first, T second)

        {

            Node<T> p = head;

 

            while (p.next != null)

            {

                if (p.data.Equals(first))

                {

                    p.data = second;

                }

                p = p.next;

            }

            if (p.data.Equals(first))

            {

                p.data = second;

            }

        }

 

       

       

        /// <summary>

        /// 链表反转

        /// </summary>

        public void Reverse()

        {

            Node<T> p = head;

            Node<T> newhead = head;

            Node<T> q = p;

            p = p.next;

            newhead.next = null;

            while (p.next != null)

            {

                q = p;

                p = p.next;

                q.next = newhead;

                newhead = q;

            }

            q = p;

            q.next = newhead;

            newhead = p;

 

            head = newhead;

        }

 

 

 

没有更多推荐了,返回首页