コード例 #1
0
        /// <summary>
        /// 按照索引进行插入
        /// </summary>
        /// <param name="dataItem">节点Data类型实例</param>
        /// <param name="index">链表的索引</param>
        public void InsertAt(T dataItem, int index)
        {
            if (index == 0)
            {
                Prepend(dataItem);
            }
            else if (index == Count)
            {
                Append(dataItem);
            }
            else if (index > 0 && index < Count)
            {
                var currentNode = m_FirstNode;
                var newNode     = new SLinkedListNode <T>(dataItem);

                for (int i = 1; i < index; ++i)
                {
                    currentNode = currentNode.Next;
                }

                newNode.Next     = currentNode.Next;
                currentNode.Next = newNode;

                m_Count++;
            }
            else
            {
                throw new IndexOutOfRangeException();
            }
        }
コード例 #2
0
 /// <summary>
 /// 比较接口的实现
 /// </summary>
 /// <param name="other">比较类型的实例</param>
 /// <returns>比较后的返回值</returns>
 public int CompareTo(SLinkedListNode <T> other)
 {
     if (other == null)
     {
         return(-1);               //空值一般比非空的优先,因为可以再次赋值新的数据
     }
     return(this.Data.CompareTo(other.Data));
 }
コード例 #3
0
        /// <summary>
        /// 从链表的首部进行插入
        /// </summary>
        /// <param name="dataItem">插入链表的节点Data的类型实例</param>
        public void Prepend(T dataItem)
        {
            SLinkedListNode <T> newNode = new SLinkedListNode <T>(dataItem);

            if (m_FirstNode == null)
            {
                m_FirstNode = m_LastNode = newNode;
            }
            else
            {
                var currentNode = m_FirstNode;
                newNode.Next = currentNode;
                m_FirstNode  = newNode;
            }
            m_Count++;
        }
コード例 #4
0
        /// <summary>
        /// 从链表的尾部进行插入
        /// </summary>
        /// <param name="dataItem">插入链表的节点Data的类型实例</param>
        public void Append(T dataItem)
        {
            SLinkedListNode <T> newNode = new SLinkedListNode <T>(dataItem);

            if (m_FirstNode == null)
            {
                m_FirstNode = m_LastNode = newNode;
            }
            else
            {
                var currentNode = m_LastNode;
                currentNode.Next = newNode;
                m_LastNode       = newNode;
            }

            // Increment the count.
            m_Count++;
        }
コード例 #5
0
 public void Dispose()
 {
     m_current          = null;
     m_doublyLinkedList = null;
 }
コード例 #6
0
 public void Reset()
 {
     m_current = m_doublyLinkedList.Head;
 }
コード例 #7
0
            public bool MoveNext()
            {
                m_current = m_current.Next;

                return(this.m_current != null);
            }
コード例 #8
0
 public SLinkedListEnumerator(SLinkedList <T> list)
 {
     this.m_doublyLinkedList = list;
     this.m_current          = list.Head;
 }