public void Swap(DoubleLinked another) { //只有两个值的时候 old 1 交换为 1 old //this.Next = null; //this.Previous = another; //another.Previous = null; //another.Next = this; //三个值的时候 2 3 4头部交换为3 2 4 //doublelinked temp ; //temp = another.next; //another.next = this; //this.next = temp; //this.next.previous=this ; //this.previous = another; //another.previous = null; ////5 6 7 尾部交换为 5 7 6 //DoubleLinked temp; //temp = this.Previous; //this.Previous = another; //another.Previous = temp; //temp.Next = another; //another.Next = this; //this.Next = null; //8 9 10 11 中间交换为 8 10 9 11 //DoubleLinked before; //DoubleLinked after; //before = this.Previous; //after = another.Next; //another.Next = this; //this.Next = after; //before.Next = another; ////after.Next = null; //this.Next.Previous=this; //another = this.Previous; //before = another.Previous; ////before.Previous = null; DoubleLinked temp; temp = another.Previous; this.Delete(); another.Delete(); another.InserBefore(temp); this.InserAfter(temp); temp.Delete(); }
/// <summary> /// 插入当前节点前面 /// </summary> /// <param name="node"></param> public void InserBefore(DoubleLinked node) { if (node.Previous == null) { node.Previous = this; this.Next = node; } else { this.Previous = node.Previous; node.Previous = this; this.Next = node; this.Previous.Next = this; } }
/// <summary> /// 插入当前节点后面 /// </summary> /// <param name="node"></param> public void InserAfter(DoubleLinked node) { if (node.Next == null) { //current1.InserAfter(old); node.Next = this; //this指代current1 this.Previous = node; } else { //current2.InserAfter(old); this.Next = node.Next; node.Next = this; this.Next.Previous = this; this.Previous = node; //还可以重构代码,使用单元测试检测功能是否损坏 } }