예제 #1
0
        /// <summary>
        ///     获取链表长度
        /// </summary>
        /// <returns></returns>
        public int GetLength()
        {
            LinkListNode <T> p = this._headLinkListNode;
            int len            = 0;

            while (p != null)
            {
                ++len;
                p = p.Next;
            }
            return(len);
        }
예제 #2
0
        /// <summary>
        ///     显示链表数据
        /// </summary>
        public void ShowtheLinkList()
        {
            var p = new LinkListNode <T>();

            p = this._headLinkListNode;
            int index  = 0;
            int index2 = 0;

            while (p != null)
            {
                Console.Write("{0},{1}\n", p.Data, index);
                p = p.Next;

                ++index;
            }
        }
예제 #3
0
        /// <summary>
        ///     在End Node之后追加Node
        /// </summary>
        /// <param name="item"></param>
        public void AppendToLinkListEndNode(T item)
        {
            var q = new LinkListNode <T>(item);
            var p = new LinkListNode <T>();

            if (this._headLinkListNode == null)
            {
                this._headLinkListNode = q;
                return;
            }

            p = this._headLinkListNode;
            while (p.Next != null)
            {
                p = p.Next;
            }
            p.Next = q;
        }
예제 #4
0
        /// <summary>
        ///     根据Value值查找Element
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int GetElementForValue(T value)
        {
            if (this.IsEmpty())
            {
                Console.WriteLine("LinkList is Empty!");
                return(-1);
            }
            var p = new LinkListNode <T>();

            p = this._headLinkListNode;
            int i = 1;

            while (!p.Data.Equals(value) && p.Next != null)
            {
                p = p.Next;
                ++i;
            }
            return(i);
        }
예제 #5
0
        /// <summary>
        ///     获取第i个Node的Element
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public T GetElementForIndex(int i)
        {
            if (this.IsEmpty() || i < 0)
            {
                Console.WriteLine("LinkList is empty or position is error! ");
                return(default(T));
            }
            var p = new LinkListNode <T>();

            p = this._headLinkListNode;
            int j = 1;

            while (p.Next != null && j < i)
            {
                ++j;
                p = p.Next;
            }
            if (j == i)
            {
                return(p.Data);
            }
            Console.WriteLine("The " + i + "th doubleLinkedListNode is not exist!");
            return(default(T));
        }
예제 #6
0
 /// <summary>
 ///     清空链表
 /// </summary>
 public void ClearLinkList()
 {
     this._headLinkListNode = null;
 }
예제 #7
0
 /// <summary>
 ///     构造函数
 /// </summary>
 public LinkList()
 {
     this._headLinkListNode = null;
 }