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); }
public void Clear() { m_Count = 0; m_Tail = m_Head; m_Head.Next = null; m_Head.Prior = null; }
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++; }
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; }
public IEnumerator <T> GetEnumerator() { DoubleLink <T> currentNode = m_Head; while ((currentNode = currentNode.Next) != null) { yield return(currentNode.Value); } }
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--; }
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); }
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); }
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); }
public LoopLink(T t) : base(t) { m_CurrentNode = m_Head.Next; m_CurrentIndex = 0; }
public DoubleLinkedList() { m_Count = 0; m_Head = new DoubleLink <T>(); m_Tail = m_Head; }