Esempio n. 1
0
        //----交换节点
        internal void Swap(DLinkNode <T> a, DLinkNode <T> b)
        {
            if (a.Next == b)
            {
                if (a.Previous == null && b.Next != null)
                {
                    a.Next          = b.Next;
                    b.Next.Previous = a;
                    b.Previous      = null;
                    b.Next          = a;
                    a.Previous      = b;
                    Head            = b;
                }
                else if (a.Previous != null && b.Next == null)
                {
                    b.Next          = a;
                    a.Previous.Next = b;

                    b.Previous = a.Previous;
                    a.Next     = null;
                    a.Previous = b;

                    Tail = a;
                }
                else if (a.Previous == null && b.Next == null)
                {
                    a.Previous = b;
                    a.Next     = null;
                    b.Next     = a;
                    b.Previous = null;
                    Head       = b;
                    Head       = a;
                }
                else
                {
                    a.Next          = b.Next;
                    b.Next.Previous = a;
                    DLinkNode <T> c = a.Previous;
                    a.Previous = b;
                    b.Next     = a;
                    b.Previous = c;
                    c.Next     = b;
                }
            }
            else if (a.Previous == b)
            {
                if (b.Previous == null && a.Next != null)
                {
                    Remove(b);
                    Insert(a, b);
                    Head = a;
                }
                else if (b.Previous != null && a.Next == null)
                {
                    Remove(a);
                    Insert(b.Previous, a);
                    Tail = b;
                }
                else if (b.Previous == null && a.Next == null)
                {
                    Remove(b);
                    AddOnTail(b);
                    Head = a;
                    Tail = b;
                }
                else
                {
                    Remove(b);
                    Insert(a, b);
                }
            }
            else
            {
                if (a.Previous == null)
                {
                    Remove(a);
                    Insert(b, a);
                    Remove(b);
                    AddOnHead(b);
                    Head = b;
                    if (a.Next == null)
                    {
                        Tail = a;
                    }
                    else
                    {
                    }
                }
                else if (a.Previous != null && b.Next == null)
                {
                    DLinkNode <T> c = a.Previous;
                    Remove(a);
                    AddOnTail(a);
                    Remove(b);
                    Insert(c, b);
                    Tail = a;
                }

                else if (b.Previous == null)
                {
                    Remove(b);
                    Insert(a, b);
                    Remove(a);
                    AddOnHead(a);
                    Head = a;
                    if (a.Next == null)
                    {
                        Tail = b;
                    }
                }
                else if (b.Previous != null && a.Next == null)
                {
                    DLinkNode <T> c = b.Previous;
                    Remove(b);
                    AddOnTail(b);
                    Remove(a);
                    Insert(c, a);
                    Tail = b;
                }
                else
                {
                    DLinkNode <T> Previous = a.Previous;
                    Remove(a);
                    Insert(b, a);
                    Remove(b);
                    Insert(Previous, b);
                }
            }
        }