public DoubleLinkedList <T> Append(T value) { DoubleLinkedListNode <T> node = new DoubleLinkedListNode <T>(value); if (Tail != null) { Tail.Next = node; } node.Previous = Tail; Tail = node; Count++; return(this); }
public DoubleLinkedList <T> AddToStart(T value) { DoubleLinkedListNode <T> node = new DoubleLinkedListNode <T>(value); if (Head != null) { Head.Previous = node; } node.Next = Head; Head = node; Count++; return(this); }
public T[] ToArray() { T[] array = new T[Count]; DoubleLinkedListNode <T> iterator = Head; int counter = 0; while (iterator != null) { array[counter++] = iterator.Value; iterator = iterator.Next; } return(array); }
public DoubleLinkedList <T> Insert(T value, int index) { if (index < 0 || index > Count) { throw new ArgumentOutOfRangeException("Index couldn't be negative or greater than Count."); } int counter = 0; DoubleLinkedListNode <T> node = new DoubleLinkedListNode <T>(value); DoubleLinkedListNode <T> iterator = Head; DoubleLinkedListNode <T> prev = null; while (iterator != null && counter < index) { counter++; prev = iterator; iterator = iterator.Next; } // link new node in the list node.Previous = prev; node.Next = iterator; if (prev != null) { prev.Next = node; } if (iterator != null) { iterator.Previous = node; } // update Head and Tail if necessary if (counter == 0) { Head = node; } Count++; if (counter == Count - 1) { Tail = node; } return(this); }
public DoubleLinkedList() { Count = 0; Head = null; Tail = null; }
public DoubleLinkedListNode(T value, DoubleLinkedListNode <T> previous, DoubleLinkedListNode <T> next) { Value = value; Previous = previous; Next = next; }