Exemple #1
0
        private DoubleLink <T> GetNodeAt(int pos)
        {
            if (pos >= m_Count || pos < 0)
            {
                throw new IndexOutOfRangeException("pos");
            }

            DoubleLink <T> currentNode = null;

            // 最近的途径找到pos位置的节点
            if (pos > m_Count / 2)
            {
                currentNode = m_Tail;
                pos         = m_Count - pos - 1;

                while (pos-- > 0)
                {
                    currentNode = currentNode.Prior;
                }
            }
            else
            {
                currentNode = m_Head.Next;

                while (pos-- > 0)
                {
                    currentNode = currentNode.Next;
                }
            }

            return(currentNode);
        }
Exemple #2
0
 public void Clear()
 {
     m_Count      = 0;
     m_Tail       = m_Head;
     m_Head.Next  = null;
     m_Head.Prior = null;
 }
Exemple #3
0
        public void InsertAt(T t, int pos)
        {
            if (pos > m_Count || pos < 0)
            {
                throw new IndexOutOfRangeException("pos");
            }

            if (m_Count == int.MaxValue)
            {
                throw new ArithmeticException();
            }

            DoubleLink <T> insertNode = new DoubleLink <T>(t);

            if (pos == m_Count)
            {
                insertNode.Prior = m_Tail;
                m_Tail.Next      = insertNode;
                m_Tail           = insertNode;
                m_Count++;
                return;
            }

            DoubleLink <T> currentNode = GetNodeAt(pos);

            insertNode.Prior       = currentNode.Prior;
            insertNode.Next        = currentNode;
            currentNode.Prior.Next = insertNode;
            currentNode.Prior      = insertNode;
            m_Count++;
        }
Exemple #4
0
 public DoubleLinkedList(T t)
     : this()
 {
     m_Count      = 1;
     m_Head.Next  = new DoubleLink <T>(t);
     m_Tail       = m_Head.Next;
     m_Tail.Prior = m_Head;
 }
Exemple #5
0
        public IEnumerator <T> GetEnumerator()
        {
            DoubleLink <T> currentNode = m_Head;

            while ((currentNode = currentNode.Next) != null)
            {
                yield return(currentNode.Value);
            }
        }
Exemple #6
0
        public void RemoveAt(int pos)
        {
            if (pos == m_Count - 1)
            {
                m_Tail      = m_Tail.Prior;
                m_Tail.Next = null;
                m_Count--;
                return;
            }

            DoubleLink <T> currentNode = GetNodeAt(pos);

            currentNode.Prior.Next = currentNode.Next;
            currentNode.Next.Prior = currentNode.Prior;
            m_Count--;
        }
Exemple #7
0
        public int Find(T t)
        {
            DoubleLink <T> currentNode = m_Head;
            int            pos         = 0;

            while ((currentNode = currentNode.Next) != null)
            {
                if (currentNode.Value.Equals(t))
                {
                    return(pos);
                }

                pos++;
            }

            return(-1);
        }
Exemple #8
0
        public T GetPrevious()
        {
            if (m_Count == 0)
            {
                throw new IndexOutOfRangeException();
            }

            if (m_CurrentNode != null)
            {
                m_CurrentNode = m_CurrentNode.Prior;
                m_CurrentIndex--;
            }

            if (m_CurrentNode == null || m_CurrentNode == m_Head)
            {
                m_CurrentNode  = m_Tail;
                m_CurrentIndex = m_Count - 1;
            }

            return(m_CurrentNode.Value);
        }
Exemple #9
0
        public T GetNext()
        {
            if (m_Count == 0)
            {
                throw new IndexOutOfRangeException();
            }

            if (m_CurrentNode != null)
            {
                m_CurrentNode = m_CurrentNode.Next;
                m_CurrentIndex++;
            }

            if (m_CurrentNode == null)
            {
                m_CurrentNode  = m_Head.Next;
                m_CurrentIndex = 0;
            }

            return(m_CurrentNode.Value);
        }
Exemple #10
0
 public LoopLink(T t) : base(t)
 {
     m_CurrentNode  = m_Head.Next;
     m_CurrentIndex = 0;
 }
Exemple #11
0
 public DoubleLinkedList()
 {
     m_Count = 0;
     m_Head  = new DoubleLink <T>();
     m_Tail  = m_Head;
 }