示例#1
0
        public DoubleLinked <T> FindBy(int value)
        {
            DoubleLinked <T> after  = this.Next;
            DoubleLinked <T> before = this.Previous;

            if (this.Value.Equals(value))
            {
                return(this);
            }
            while (!(after == null))
            {
                if (after.Value.Equals(value))
                {
                    return(after);
                }
                else
                {
                    after = after.Next;
                }
            }
            while (!(before == null))
            {
                if (before.Value.Equals(value))
                {
                    return(before);
                }
                else
                {
                    before = before.Previous;
                }
            }
            return(null);
        }
示例#2
0
        public static void Swap(DoubleLinked <T> a, DoubleLinked <T> b)
        {
            DoubleLinked <T> temp  = b.Next;
            DoubleLinked <T> Temp  = b.Previous;
            DoubleLinked <T> store = a.Next;
            DoubleLinked <T> link  = a.Previous;

            if (a == b)
            {
                throw new Exception("");
            }
            Delete(b);
            b.InsertAfter(a);
            Delete(a);
            if (link == null)
            {
                a.IsertBefore(temp);
            }
            else
            {
                if (store == b)
                {
                    a.InsertAfter(b);
                }
                else
                {
                    a.InsertAfter(Temp);
                }
            }
        }
示例#3
0
            public bool MoveNext()
            {
                bool result = _node.Next != null;

                _node = _node.Next;
                return(result);
            }
示例#4
0
 /// <summary>
 /// 在node之前插入当前节点
 /// </summary>
 /// <param name="node"></param>
 public void InsertBefor(DoubleLinked node)
 {
     this.Next = node;
     if (node.Previous != null)
     {
         this.Previous      = node.Previous;
         this.Previous.Next = this;
     }
     node.Previous = this;
 }
示例#5
0
 public void IsertBefore(DoubleLinked <T> node)
 {
     this.Next = node;
     if (node.Previous == null)
     {
         node.Previous = this;
     }
     else
     {
         this.Previous      = node.Previous;
         node.Previous      = this;
         this.Previous.Next = this;
     }
 }
示例#6
0
 /// <summary>
 /// 在node之后插入当前节点
 /// </summary>
 /// <param name="node"></param>
 public void InsertAfter(DoubleLinked node)
 {
     this.Previous = node;
     if (node.Next != null)
     {
         this.Next          = node.Next;
         this.Next.Previous = this;
     }
     else
     {
         //nothing
     }
     node.Next = this;
 }
示例#7
0
 public void InsertAfter(DoubleLinked <T> node)
 {
     if (node.Next == null)
     {
         node.Next     = this;
         this.Previous = node;
     }
     else
     {
         this.Next          = node.Next;
         this.Previous      = node;
         node.Next          = this;
         this.Next.Previous = this;
     }
 }
示例#8
0
 public static void Delete(DoubleLinked <T> node)
 {
     if (node.IsHead && node.IsTail)
     {
         throw new Exception("最后一个无法删除");
     }
     if (node.Previous != null)
     {
         node.Previous.Next = node.Next;
     }
     if (node.Next != null)
     {
         node.Next.Previous = node.Previous;
     }
     node.Previous = null;
     node.Next     = null;
 }
示例#9
0
 public DLEnumerable(DoubleLinked <T> node)
 {
     _node = node;
 }
示例#10
0
        /// <summary>
        /// 将当前节点和swapped节点交换
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public void Swap(DoubleLinked swapped)
        {
            if (this == swapped)
            {
                throw new Exception("无法用节点自己与自己调换");
            }

            if (this.IsHead && this.Next == swapped)
            {
                this.Previous    = swapped;
                this.Next        = null;
                swapped.Next     = this;
                swapped.Previous = null;
                return;
            }

            if (this.IsTail && this.Previous == swapped)
            {
                this.Next        = swapped;
                this.Previous    = null;
                swapped.Previous = this;
                swapped.Next     = null;
                return;
            }

            DoubleLinked thisP    = this.Previous;
            DoubleLinked thisN    = this.Next;
            DoubleLinked swappedP = swapped.Previous;
            DoubleLinked swappedN = swapped.Next;

            this.Delet();
            this.InsertAfter(swapped);
            swapped.Delet();

            if (thisP == null)
            {
                swapped.InsertBefor(thisN);
            }
            else if (this.Next == swapped)
            {
                swapped.InsertBefor(this);
            }
            else
            {
                swapped.InsertAfter(thisP);
            }



            //if (this.IsHead && swapped.IsTail)
            //{
            //    DoubleLinked thisN = this.Next;
            //    DoubleLinked swappedP = swapped.Previous;
            //    this.Previous = swappedP;
            //    swappedP.Next = this;
            //    this.Next = null;

            //    swapped.Next = thisN;
            //    thisN.Previous = swapped;
            //    swapped.Previous = null;
            //    return;
            //}


            //if (this.IsTail && swapped.IsHead)
            //{
            //    DoubleLinked thisNP = this.Previous;
            //    DoubleLinked swappedN = swapped.Next;
            //    this.Next = swappedN;
            //    swappedN.Previous = this;
            //    this.Previous = null;

            //    swapped.Previous = thisNP;
            //    thisNP.Next = swapped;
            //    swapped.Next = null;
            //    return;
            //}

            //if (!this.IsHead && !this.IsTail && !swapped.IsTail && !swapped.IsHead)
            //{
            //    DoubleLinked thisP = this.Previous;
            //    DoubleLinked thisN = this.Next;
            //    DoubleLinked swappedP = swapped.Previous;
            //    DoubleLinked swappedN = swapped.Next;

            //    this.Next = swappedN;
            //    this.Previous = swappedP;
            //    swappedN.Previous = this;
            //    swappedP.Next = this;

            //    swapped.Next = thisN;
            //    swapped.Previous = thisP;
            //    thisN.Previous = swapped;
            //    thisP.Next = swapped;
            //}
        }