/// <summary> /// Adds a new node containing the specified value before the specified existing node in the <see cref="DoublyLinkedList{T}"/>. /// </summary> /// <param name="node">The <see cref="DoublyLinkedListNode{T}"/> before which to insert a new <see cref="DoublyLinkedListNode{T}"/> containing the value.</param> /// <param name="value">The value to add to the <see cref="DoublyLinkedList{T}"/>.</param> /// <returns>The new <see cref="DoublyLinkedListNode{T}"/> containing the value.</returns> public DoublyLinkedListNode <T> AddBefore(DoublyLinkedListNode <T> node, T value) { if (node == null) { throw new ArgumentNullException(nameof(node)); } if (node.List != this) { throw new InvalidOperationException("node doesn't belong to this list"); } if (node == First) { return(AddFirst(value)); } var newNode = new DoublyLinkedListNode <T>(value); AddBefore(node, newNode); return(newNode); }
/// <summary> /// Adds the specified new node at the end of the <see cref="DoublyLinkedList{T}"/>. /// </summary> /// <param name="node">The new <see cref="DoublyLinkedListNode{T}"/> to add at the end of the <see cref="DoublyLinkedList{T}"/>.</param> public void AddLast(DoublyLinkedListNode <T> node) { if (node == null) { throw new ArgumentNullException(nameof(node)); } if (node.List != null) { throw new InvalidOperationException("node belongs to another list"); } if (Count == 0) { AddFirst(node); return; } node.List = this; node.Next = null; node.Previous = Last; Last.Next = node; Last = node; Count++; }