public void InsertAfter(int valueToFind, int valueToInsert) { IncrementCount(); var nodeToInsert = new DoubleLinkedListNode(valueToInsert); var node = Find(valueToFind); if (node == null) { throw new ArgumentException($"Unable to find a node with value '{valueToFind}' to insert after."); } nodeToInsert.Next = node.Next; nodeToInsert.Previous = node; node.Next = nodeToInsert; }
public void InsertLast(int value) { IncrementCount(); var node = new DoubleLinkedListNode(value); if (Head == null) { Tail = node; Head = Tail; } else { Tail.Next = node; node.Previous = Tail; Tail = node; } }
public void InsertFirst(int value) { IncrementCount(); var node = new DoubleLinkedListNode(value); if (Head == null) { Head = node; Tail = Head; } else { node.Next = Head; Head.Previous = node; Head = node; } }
public void InsertBefore(int valueToFind, int valueToInsert) { IncrementCount(); var nodeToInsert = new DoubleLinkedListNode(valueToInsert); var current = Head; var previous = current; while (current != null) { if (current.Value == valueToFind) { nodeToInsert.Next = current; current.Previous = nodeToInsert; previous.Next = nodeToInsert; nodeToInsert.Previous = previous; break; } previous = current; current = current.Next; } }