private void AddFirst(DoublyLinkedListNode <T> node) { DoublyLinkedListNode <T> temp = Head; Head = node; Head.Next = temp; Count++; if (Count == 1) { Tail = Head; } else { temp.Previous = Head; } }
/// <summary> /// Removes the first occurance of the item from the list(searching from head to tail) /// </summary> /// <param name="item"></param> /// <returns>True if the item was found and removed, false otherwise</returns> public bool Remove(T item) { DoublyLinkedListNode <T> previous = null; DoublyLinkedListNode <T> current = Head; /* * 1: Empty List: Do nothing * 2: Single Node: (previous is null) * 3: Many nodes: * a. node to remove is the first node * b. node to remove is the middle or last */ while (current != null) { if (current.Value.Equals(item)) { if (previous != null) { previous.Next = current.Next; //it was the end - so update Tail if (current.Next == null) { Tail = previous; } else { current.Next.Previous = previous; } Count--; } else { RemoveFirst(); } return(true); } previous = current; current = current.Next; } return(false); }