public override string ToString() { DoubleLink currentLink = _first; StringBuilder builder = new StringBuilder(); while (currentLink != null) { builder.Append(currentLink); currentLink = currentLink.NextLink; } return(builder.ToString()); }
public DoubleLink Insert(int val) { // Creates a link, sets its link to the first item and then makes this the first item in the list. DoubleLink link = new DoubleLink(val); link.NextLink = _first; if (_first != null) { _first.PreviousLink = link; } _first = link; return(link); }
public DoubleLink Delete() { // Gets the first item, and sets it to be the one it is linked to DoubleLink temp = _first; if (_first != null) { _first = _first.NextLink; if (_first != null) { _first.PreviousLink = null; } } return(temp); }
///// New operations public void InsertAfter(DoubleLink link, int val) { if (link == null) { return; } DoubleLink newLink = new DoubleLink(val); newLink.PreviousLink = link; // Update the 'after' link's next reference, so its previous points to the new one if (link.NextLink != null) { link.NextLink.PreviousLink = newLink; } // Steal the next link of the node, and set the after so it links to our new one newLink.NextLink = link.NextLink; link.NextLink = newLink; }
public DoublyLinkedList() { _first = null; }