/// <summary> /// 对链表中的每个元素执行一次操作 /// </summary> /// <param name="func"></param> public void ForEach(ForEachFunc <T> func) { CircleListEnumerator iter = new CircleListEnumerator(this); while (iter.MoveNext()) { func(ref iter.curNode.value); } }
/// <summary> /// 用遍历的方式查找链表,返回符合要求的第一个节点 /// </summary> /// <param name="func"></param> /// <returns></returns> public CircleListNode <T> FindFirst(FindFunc <T> func) { CircleListEnumerator iter = new CircleListEnumerator(this); while (iter.MoveNext()) { if (func(iter.curNode.value)) { return(iter.curNode); } } return(null); }
/// <summary> /// /// </summary> /// <returns></returns> public CircleList <T> Clone() { CircleList <T> result = new CircleList <T>(); CircleListEnumerator iter = new CircleListEnumerator(this); while (iter.MoveNext()) { result.AddLast(iter.curNode.Clone()); } if (this.isLinked) { result.LinkLastAndFirst(); } return(result); }
/// <summary> /// 用遍历的方式查找链表,返回所以符合要求的节点 /// </summary> /// <param name="func"></param> /// <returns></returns> public CircleListNode <T>[] FindAll(FindFunc <T> func) { List <CircleListNode <T> > result = new List <CircleListNode <T> >(); CircleListEnumerator iter = new CircleListEnumerator(this); while (iter.MoveNext()) { if (func(iter.curNode.value)) { result.Add(iter.curNode); } } return(result.ToArray()); }
/// <summary> /// 用遍历的方式获得该节点在链表中的位置。 /// 如果节点不在链表中,返回-1 /// </summary> /// <param name="node"></param> /// <returns></returns> public int IndexOf(CircleListNode <T> node) { CircleListEnumerator iter = new CircleListEnumerator(this); int result = -1; while (iter.MoveNext()) { result++; if (iter.curNode == node) { return(result); } } return(-1); }
/// <summary> /// 将链表从起始节点开始,向前或向后的顺序转换到一个数组中。 /// </summary> /// <param name="startNode">起始节点,必须是链表中的节点</param> /// <param name="forward"></param> /// <returns></returns> public T[] ToArray(CircleListNode <T> startNode, bool forward) { if (startNode == null) { throw new Exception("The startNode is null!"); } CircleListEnumerator iter = new CircleListEnumerator(this); bool startNodeInList = false; while (iter.MoveNext()) { if (iter.curNode == startNode) { startNodeInList = true; } } if (!startNodeInList) { throw new Exception("the startNode should in current List!"); } if (!this.isLinked) { this.LinkLastAndFirst(); } T[] result = new T[this.Length]; for (int i = 0; i < this.Length; i++) { result[i] = startNode.value; if (forward) { startNode = startNode.next; } else { startNode = startNode.pre; } } return(result); }