Example #1
0
        /// <summary>
        /// 在链表中某一节点后插入一个新的节点。
        /// </summary>
        /// <param name="value"></param>
        /// <param name="node">必须是该链表中包含的节点</param>
        /// <returns></returns>
        public bool InsertAfter(T value, CircleListNode <T> node)
        {
            CircleListNode <T> newNode = new CircleListNode <T>(value);

            newNode.next = node.next;

            if (node.next == null)
            {
                return(false);
            }

            if (node == last)
            {
                last = newNode;
            }

            if (node.next != null)
            {
                node.next.pre = newNode;
            }
            node.next   = newNode;
            newNode.pre = node;

            length++;

            return(true);
        }
Example #2
0
        /// <summary>
        /// 在未首尾相连的条件下在链表的末尾加入一个元素。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddLast(T value)
        {
            if (linked)
            {
                return(false);
            }

            CircleListNode <T> node = new CircleListNode <T>(value);

            return(AddLast(node));
        }
Example #3
0
        /// <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);
        }
Example #4
0
        /// <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);
        }
Example #5
0
        /// <summary>
        /// 在未首尾相连的条件下在链表的末尾加入一个元素。
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool AddLast(CircleListNode <T> node)
        {
            if (linked)
            {
                return(false);
            }
            length++;
            if (first == null)
            {
                first = node;
                last  = node;
            }
            else
            {
                last.next = node;
                node.pre  = last;
                last      = node;
            }

            return(true);
        }
Example #6
0
        /// <summary>
        /// 在链表的第一个元素前插入一个元素。
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool AddFirst(CircleListNode <T> node)
        {
            if (first == null)
            {
                first = node;
                last  = node;
            }
            else
            {
                node.next = first;
                first.pre = node;
                first     = node;

                if (linked)
                {
                    first.pre = last;
                    last.next = first;
                }
            }
            length++;
            return(true);
        }
Example #7
0
            public bool MoveNext()
            {
                if (--EnumeLength < 0)
                {
                    return(false);
                }

                if (curNode == null)
                {
                    curNode = first;
                    return(true);
                }
                else if (curNode.next != null)
                {
                    curNode = curNode.next;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
Example #8
0
 public void Reset()
 {
     curNode = first;
 }
Example #9
0
 public CircleListEnumerator(CircleList <T> list)
 {
     this.first  = list.first;
     EnumeLength = list.length;
 }