C# 创建链表及对链表的相关操作

 class Node<T>
    {
        public T data;
        public Node<T> next=null;
        public Node()
        { }
        public Node(T i)
        {
            this.data = i;
        }
    }
    class LinkList<T>
    {
        private Node<T> head;
        public LinkList()
        {
            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>
        /// 
        /// </summary>
        /// <param name="i">第i个节点</param>
        /// <returns></returns>
        public T GetItem(int i)
        {
            Node<T> p = head;
            int k = 0;
            if (i>count()||i<0)
            {
                Console.WriteLine("i不存在");
            }
            while (k<i)
            {
                k++;
                p = p.next;
            }
            return p.data;
        }
        public void Insert(T e, int i)
        {
            Node<T> p = new Node<T>(e);
            Node<T> q = head;
            int k = 0;
            if (i==0)
            {
                p.next = head;
                head = p;
                return;
            }
            while (k<i-1)
            {
                k++;
                q = q.next;
            }
            p.next = q.next;
            q.next = p;
        }
        /// <summary>
        /// 在链表末尾添加新节点
        /// </summary>
        public void Add(T i)
        {
            Node<T> p = new Node<T>(i);//要插入的节点
            Node<T> q = new Node<T>();
            //如果链表为空则把新节点赋给head节点
            if (head == null)
            {
                head = p;
                return;
            }
            #region 如果节点不为空,遍历该链表
            q = head;
            while (q.next != null)
            {
                q = q.next;
            }
            q.next = p;
            #endregion 如果节点不为空,遍历该链表

        }
        /// <summary>
        /// 移除链表上某个节点
        /// </summary>
        /// <param name="i"></param>
        public void RemoveAt(int i)
        {
            Node<T> p = head;
            int k = 0;
            if (i>count()||i<0)
            {
                Console.WriteLine("Error");
            }
            if (i==0)
            {
                head.next = head.next.next;
                return;
            }
            while (k<i-1)
            {
                k++;
                p = p.next;
            }
            p.next = p.next.next;
        }
        public int IndexOf(T e)
        {
            Node<T> p = head;
            int k = 0;
            while (p.next!=null)
            {
                if (p.data.Equals(e))
                {
                    return k;
                }
                k++;
                p = p.next;
            }
            if (!p.data.Equals(e))
            {
                k++;
            }
            return k >= count() ? -1 : k;
        }
        /// 查找某个元素在链表中最后一次出现的位置
        /// </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;
        }

        public void ToArrary()
        {
            T[] arr = new T[count()];
            Node<T> p = head;
            int i = 0;
            while (p!=null)
            {
                arr[i++] = p.data;
                p = p.next;
            }
            for (int j = 0; j < count(); j++)
            {
                Console.Write(arr[j] + " ");
            }
        }

        /// <summary>
        /// 删除链表中值为某个元素的所有节点
        /// </summary>
        /// <param name="e"></param>
        public void Remove(T e)
        {
            if (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>
        public void Reverse()
        {
            Node<T> p = head;
            Node<T> q = p;
            Node<T> newHead = head;
            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 = q;
            head = newHead;
        }
        /// <summary>
        /// 打印链表
        /// </summary>
        public void PrintLinkList()
        {
            Node<T> p = head;
            while(p!=null)
            {
                Console.Write(p.data+" ");
                p = p.next;
            }
        }
    }
    class Program
    {

        static void Main(string[] args)
        {
            //Node node = new Node();
            LinkList<int> linkList = new LinkList<int>();
            for (int i = 0; i < 7; i++)
            {
                linkList.Add(i);
			}
            //linkList.ToArrary();
            //linkList.RemoveAt(4);
            //linkList.Insert(10, 3);
            //Console.WriteLine(linkList.IndexOf(7));
            linkList.PrintLinkList();
            linkList.Reverse();
            linkList.PrintLinkList();
            //Console.WriteLine(linkList.GetItem(4));
            Console.Read();
        }
   }

  

原文地址:https://www.cnblogs.com/Griffin/p/2760634.html