public override string ToString() { StringBuilder sb = new StringBuilder(); DoubleNode <T> node = Head; while (node != null) { sb.Append(" " + node.Value + ","); node = node.Next; } return(sb.ToString().Trim().TrimEnd(',')); }
/// <summary> /// Adds a new node with the specified value at the end of /// this DoubleLinkedList /// </summary> /// <param name="val"></param> public new void Add(T val) { DoubleNode <T> nodeToAdd = new DoubleNode <T>(val); if (Head != null) { nodeToAdd.Next = Tail.Next; Tail.Next = nodeToAdd; nodeToAdd.Previous = Tail; Tail = nodeToAdd; } else { // first time adding a node Head = nodeToAdd; Tail = Head; } ++Count; }
/// <summary> /// Searches for the specified value starting at both the head /// and tail of this DoubleLinkedList /// </summary> /// <param name="val">Value to search for</param> /// <returns>index of first node match, otherwise -1</returns> public new int Search(T val) { int result = -1; // nothing in list, can't search! if (IsEmpty()) { return(result); } DoubleNode <T> forwardNode = Head; DoubleNode <T> backwardNode = Tail; int targetIndex = (Count / 2); for (int j = 0; j <= targetIndex; ++j) { int forwardIndex = j; int backwardIndex = (Count - 1 - j); if (forwardNode.Value.Equals(val)) { result = forwardIndex; break; } else if (backwardNode.Value.Equals(val)) { result = backwardIndex; break; } forwardNode = forwardNode.Next; backwardNode = backwardNode.Previous; } return(result); }
public new void Clear() { Head = null; Tail = null; Count = 0; }
/// <summary> /// Gets a node at the specified index /// </summary> /// <param name="index"></param> /// <returns></returns> public new T Get(int index) { DoubleNode <T> node = NodeAt(index); return(node.Value); }