/// <summary> /// Finds the first node with the value that matches the input value and returns the node /// </summary> /// <param name="value"></param> /// <returns></returns> public DoublyLinkedListNode <T> Find([NotNull] T value) { DoublyLinkedListNode <T> getCurrentHead = Head; while (getCurrentHead != null) { if (getCurrentHead.nodeValue.Equals(value)) { return(getCurrentHead); } else { getCurrentHead = getCurrentHead.nextNode; } } return(null); }
/// <summary> /// Removes the first node with the value equal to the input value /// </summary> /// <param name="item"></param> /// <returns></returns> public bool Remove([NotNull] T item) { DoublyLinkedListNode <T> findValue = Find(item); if (findValue is null) { return(false); } else { var getNextNode = findValue.nextNode; var getPreviousNode = findValue.previousNode; if (getPreviousNode is null && getNextNode is null) { //Only one value exists so remove and make list empty Head = Tail = null; return(true); }
/// <summary> /// Adds a node value to become the head of the list (Beginning of the list) /// </summary> /// <param name="value"></param> public void AddHead([NotNull] T value) { //Create a new node with a null previousNode and the current Head as nextNode if it exists DoublyLinkedListNode <T> newNode = new DoublyLinkedListNode <T>(value, null, Head); if (Head != null) { Head.previousNode = newNode; Head = newNode; } else { if (Tail == null) { Head = newNode; Tail = Head; } } Count++; }
public DoublyLinkedListNode(T nodeValue, DoublyLinkedListNode <T> previousNode, DoublyLinkedListNode <T> nextNode) { this.nodeValue = nodeValue; this.previousNode = previousNode; this.nextNode = nextNode; }