// ==================================================================================== // Interfaces // ==================================================================================== #region IComparable public int CompareTo(DoubleLink <T> other) { if (other == null) { return(-1); } return(Data.CompareTo(other.Data)); }
// Add last element public void AddFirst(DoubleLink <T> link) { Debug.Assert(link != null); if (link.IsLinked) { link.Unlink(); } link.LinkBefore(First); }
// Add first element public void AddLast(DoubleLink <T> link) { Debug.Assert(link != null); if (link.IsLinked) { link.Unlink(); } link.LinkAfter(Last); }
// Adds @link element to the list, with replacing the specified element. // This is equivalent to calling the sequence: LinkBefore(Replace); replace.Unlink(); public void LinkReplace(DoubleLink <T> link, DoubleLink <T> replace) { Debug.Assert(link != null); Debug.Assert(replace != null); if (link == replace) { return; } link.LinkReplace(replace); }
// Adds this element to a list, before the given element. public void LinkBefore(DoubleLink <T> before) { Debug.Assert(before != null); Previous = before.Previous; before.Previous = this; Next = before; if (Previous != null) { Previous.Next = this; } }
// ==================================================================================== // Methods // ==================================================================================== // Remove link from list public void Unlink() { if (Next != null) { Next.Previous = Previous; } if (Previous != null) { Previous.Next = Next; } Next = Previous = null; }
// Adds this element to the list, after the specified element public void LinkAfter(DoubleLink <T> after) { Debug.Assert(after != null); Next = after.Next; after.Next = this; Previous = after; if (Next != null) { Next.Previous = this; } }
// Adds this element to the list, with replacing the specified element. // This is equivalent to calling the sequence: LinkBefore(Replace); replace.Unlink(); public void LinkReplace(DoubleLink <T> replace) { Debug.Assert(replace != null); var replacePrev = replace.Previous; var replaceNext = replace.Next; Previous = replacePrev; Next = replaceNext; if (Previous != null) { Previous.Next = this; } if (Next != null) { Next.Previous = this; } replacePrev = null; replaceNext = null; }
// Create link without connection to the list public DoubleLink(T data, DoubleLink <T> next, DoubleLink <T> previous) { Data = data; Next = next; Previous = previous; }
// Adds this element to the list, after the specified element public void LinkAfter(DoubleLink <T> link, DoubleLink <T> after) { Debug.Assert(link != null); Debug.Assert(after != null); link.LinkAfter(after); }
// Adds this element to a list, before the given element. public void LinkBefore(DoubleLink <T> link, DoubleLink <T> before) { Debug.Assert(link != null); Debug.Assert(before != null); link.LinkBefore(before); }
// Unlink @link from the list public void Unlink(DoubleLink <T> link) { Debug.Assert(link != null); link.Unlink(); }
public DoubleLinkedList() { First = Last = Root; }