Ejemplo n.º 1
0
        /// <summary>
        /// Splits the list into two.
        /// This list's end will be the node pointed by newLastPosition and the newly created list will begin with the next node.
        /// </summary>
        /// <param name="newLastPosition">Enumerator that points to the new last position in the list.</param>
        /// <param name="newCount">New number of elements in this list. If set to -1, it is calculated automatically,
        /// but that would make the split an O(N) operation. Beware: If you set this parameter, be sure to always set the
        /// correct number, otherwise, you'd cause both lists (this one and the returned one) to return a wrong number of
        /// elements in the future.</param>
        /// <returns>The newly created list</returns>
        public MySinglyLinkedList <V> Split(MySinglyLinkedList <V> .Enumerator newLastPosition, int newCount = -1)
        {
            Debug.Assert(newLastPosition.m_list == this, "Enumerator belongs to a different list");
            Debug.Assert(newLastPosition.m_currentNode != null, "Enumerator has to point at a valid list item");
            Debug.Assert(newCount > 0 && newCount <= m_count);

            if (newCount == -1)
            {
                newCount = 1;
                var node = m_rootNode;
                while (node != newLastPosition.m_currentNode)
                {
                    newCount++;
                    node = node.Next;
                }
            }

            MySinglyLinkedList <V> retList = new MySinglyLinkedList <V>();

            retList.m_rootNode = newLastPosition.m_currentNode.Next;
            retList.m_lastNode = retList.m_rootNode == null ? null : this.m_lastNode;
            retList.m_count    = this.m_count - newCount;

            this.m_lastNode      = newLastPosition.m_currentNode;
            this.m_lastNode.Next = null;
            this.m_count         = newCount;

            return(retList);
        }
Ejemplo n.º 2
0
        public void Merge(MySinglyLinkedList <V> otherList)
        {
            if (m_lastNode == null)
            {
                m_rootNode = otherList.m_rootNode;
                m_lastNode = otherList.m_lastNode;
            }
            else
            {
                if (otherList.m_lastNode != null)
                {
                    m_lastNode.Next = otherList.m_rootNode;
                    m_lastNode      = otherList.m_lastNode;
                }
            }
            m_count += otherList.m_count;

            // Erase other list to avoid node sharing
            otherList.m_count    = 0;
            otherList.m_lastNode = null;
            otherList.m_rootNode = null;
        }
Ejemplo n.º 3
0
 public Enumerator(MySinglyLinkedList <V> parentList)
 {
     m_list         = parentList;
     m_currentNode  = null;
     m_previousNode = null;
 }