private TwoWayNode <T> GetNodeFromIndex(int index) { TwoWayNode <T> storageNode = firstNode; for (int i = 0; i < index; i++) { storageNode = storageNode.NextNode; } return(storageNode); }
public bool Remove(T value) { TwoWayNode <T> storageNode = firstNode; for (int i = 0; i < Count; i++) { if (storageNode.Value.Equals(value)) { RemoveAt(i); return(true); } storageNode = storageNode.NextNode; } return(false); }
public void Insert(T value, int index) { if (index < 0 || index > Count) { throw new Exception("Index of the List not valid!"); } TwoWayNode <T> nodeToAdd = new TwoWayNode <T>(value, null, null); if (firstNode == null) { firstNode = nodeToAdd; lastNode = nodeToAdd; } else if (firstNode == lastNode) { lastNode = nodeToAdd; lastNode.PreviousNode = firstNode; firstNode.NextNode = lastNode; } else { if (index == 0) { firstNode.PreviousNode = nodeToAdd; firstNode.PreviousNode.NextNode = firstNode; firstNode = firstNode.PreviousNode; } else if (index == Count) { lastNode.NextNode = nodeToAdd; lastNode.NextNode.PreviousNode = lastNode; lastNode = lastNode.NextNode; } else { TwoWayNode <T> previousNode = GetNodeFromIndex(index - 1); TwoWayNode <T> nextNode = previousNode.NextNode; nextNode.PreviousNode = nodeToAdd; nodeToAdd.NextNode = nextNode; nodeToAdd.PreviousNode = previousNode; previousNode.NextNode = nodeToAdd; } } Count++; }
public string DisplayBackward() { if (Count == 0) { return("Null list"); } string storageString = ""; TwoWayNode <T> node = lastNode; for (int i = Count - 1; i >= 0; i--) { storageString += node.Value.ToString(); node = node.PreviousNode; } return(storageString); }
// THOSE METHODS ARE ONLY TO SHOW THAT ARE DIFFERENT MANNERS OF SHOWING DATA IN DOUBLY LINKED LISTS. // CON -> REQUIRE MORE MEMORY THAN THE SINGLY LINKED LIST // It could also be written as a CircularDoublyLinkedList, that, first node previous // ref points to the last one, and it's next points to the first one. public string DisplayForward() { if (Count == 0) { return("Null list"); } string storageString = ""; TwoWayNode <T> node = firstNode; for (int i = 0; i < Count; i++) { storageString += node.Value.ToString(); node = node.NextNode; } return(storageString); }
/// <summary> /// Clear all list data. O(N) /// Here I am not making any data Dispose, though it should be done. I'm just deleting all refs, /// like disposing, but making them null. /// </summary> public void Clear() { /* * TwoWayNode<T> previousNode = firstNode; * TwoWayNode<T> nextNode = firstNode.NextNode; * * // It should be disposed as this example, although it is not doing anything: * for (int i = 0; i < Count - 1; i++) * { * previousNode = null; * previousNode = nextNode; * nextNode = nextNode.NextNode; * } */ // Only to simplify, I'll clear both first and top nodes, and make the Count 0. Garbage Collector will do the rest firstNode = null; lastNode = null; Count = 0; }
/// <summary> /// Remove a data from the List at a specified index. /// </summary> /// <param name="index">Index delete the data.</param> public void RemoveAt(int index) { if (index < 0 || index >= Count) { throw new Exception("Invalid index!"); } if (Count == 1) { firstNode = null; lastNode = null; } else if (index == 0) { TwoWayNode <T> nodeToRemove = firstNode; firstNode = nodeToRemove.NextNode; nodeToRemove = null; } else if (index == Count - 1) { TwoWayNode <T> nodeToRemove = lastNode; lastNode = nodeToRemove.PreviousNode; nodeToRemove = null; } else if (index != 0) { TwoWayNode <T> nodeToRemove = GetNodeFromIndex(index); TwoWayNode <T> previousNode = nodeToRemove.PreviousNode; TwoWayNode <T> nextNode = nodeToRemove.NextNode; previousNode.NextNode = nextNode; nextNode.PreviousNode = previousNode; nodeToRemove = null; } Count--; }
public TwoWayNode(T value, TwoWayNode <T> previousNode, TwoWayNode <T> nextNode) { this.NextNode = nextNode; this.PreviousNode = previousNode; this.Value = value; }
public DoublyLinkedList() { Count = 0; firstNode = null; lastNode = null; }