/// <summary> /// Adds an item before the given item. /// </summary> /// <param name="pre"></param> /// <param name="value"></param> /// <returns></returns> public bool AddBefore(T pre, T value) { if (value == null) { throw new ArgumentException("Value cannot be null."); } IterableLinkedListNodeLink <T> node = this.FindNode(pre); if (node == null) { return(false); } IterableLinkedListNode <T> iterableLinkedListNode = new IterableLinkedListNode <T>(value, node.Child); if (node.Parent == null) { this.root = iterableLinkedListNode; } else { node.Parent.Next = iterableLinkedListNode; } return(true); }
/// <summary> /// Adds an item after the given item. /// </summary> /// <param name="pre"></param> /// <param name="value"></param> /// <returns></returns> public bool AddAfter(T pre, T value) { if (value == null) { throw new ArgumentException("Value cannot be null."); } IterableLinkedListNodeLink <T> node = this.FindNode(pre); if (node == null) { return(false); } IterableLinkedListNode <T> iterableLinkedListNode = new IterableLinkedListNode <T>(value, node.Child.Next); node.Child.Next = iterableLinkedListNode; if (node.Child == this.tail) { this.tail = iterableLinkedListNode; } return(true); }
/// <summary> /// Adds an item as the first element in this list. /// </summary> /// <param name="value"></param> public void AddFirst(T value) { if (value == null) { throw new ArgumentException("Value cannot be null."); } this.root = new IterableLinkedListNode <T>(value, this.root); if (this.tail == null) { this.tail = this.root; } }
/// <summary> /// Finds a node with the given value and returns the link between that node and its parent. /// </summary> /// <param name="value"></param> /// <returns></returns> private IterableLinkedListNodeLink <T> FindNode(T value) { IterableLinkedListNode <T> parent = null; for (IterableLinkedListNode <T> child = this.root; child != null; child = child.Next) { if (child.Value.Equals((object)value)) { return(new IterableLinkedListNodeLink <T>(parent, child)); } parent = child; } return(null); }
/// <summary> /// Removes the first item in this list. /// </summary> /// <param name="value"></param> /// <returns></returns> public bool RemoveFirst(out T value) { if (root == null) { value = default(T); return(false); } IterableLinkedListNode <T> oldRoot = root; value = oldRoot.Value; root = oldRoot.Next; if (oldRoot == tail) { tail = root; } return(true); }
/// <summary> /// Adds an item as the last element in this list. /// </summary> /// <param name="value"></param> public void AddLast(T value) { if (value == null) { throw new ArgumentException("Value cannot be null."); } IterableLinkedListNode <T> iterableLinkedListNode = new IterableLinkedListNode <T>(value, null); if (this.root == null) { this.root = iterableLinkedListNode; this.tail = iterableLinkedListNode; } else { this.tail.Next = iterableLinkedListNode; this.tail = iterableLinkedListNode; } }
/// <summary> /// Removes an item with the given value. /// </summary> /// <param name="value"></param> /// <returns></returns> public bool Remove(T value) { if (value == null) { throw new ArgumentException("Value cannot be null."); } IterableLinkedListNodeLink <T> node = this.FindNode(value); if (node == null) { return(false); } if (root == node.Child) { this.root = node.Child.Next; if (this.root == null) { this.tail = null; } } if (tail == node.Child) { this.tail = node.Parent; } if (node.Parent != null) { node.Parent.Next = node.Child.Next; } return(true); }
public void Dispose() { this.currentNode = null; this.sourceList = null; }
public void Reset() { this.currentNode = this.sourceList.root; }
public bool MoveNext() { this.currentNode = this.currentNode != null ? this.currentNode.Next : this.sourceList.root; return(this.currentNode != null); }
public IterableLinkedListNodeLink(IterableLinkedListNode <T> parent, IterableLinkedListNode <T> child) { this.Parent = parent; this.Child = child; }
public IterableLinkedListNode(T value, IterableLinkedListNode <T> next) { this.Value = value; this.Next = next; }