public void DeleteLast() { if (Tail != null) { if (Tail.Previous != Head) { Tail = Tail.Previous; Tail.Next = null; } else { Tail = null; } } else { Head = null; } }
/// <summary> /// Insert the specified value at the end of the list. Runtime O(1) /// </summary> /// <param name="val">Value.</param> public void Insert(T val) { if (Head == null) { Head = new DoublyLinkedListNode <T>(val); } else { var node = new DoublyLinkedListNode <T>(val); if (Tail == null) //this will only be null if there's one element in the list { Tail = node; Tail.Previous = Head; //set head as prev Head.Next = Tail; } else { Tail.Next = node; node.Previous = Tail; //set old tail as previous to new tail Tail = node; //set the new tail } } }
public DoublyLinkedList(DoublyLinkedListNode <T> head, DoublyLinkedListNode <T> tail) { Head = head; Tail = tail; }
/// <summary> /// Clear the entire list. Runtime O(1) /// </summary> public void Clear() { Head = null; Tail = null; }