/// <summary> /// Removes item at specified index /// </summary> /// <param name="index">index of item to remove</param> /// <returns>item which was removed</returns> public T RemoveAt(int index) { if (index > _length - 1) { throw new IndexOutOfRangeException(); } T item; lock (_lock) { int i = 0; ParentAwareNode <T> previousNode = null; ParentAwareNode <T> currentNode = _head; while (i < index) { previousNode = currentNode; currentNode = currentNode.Next() as ParentAwareNode <T>; i++; } item = currentNode.Value; if (previousNode == null) { _head = currentNode.Next() as ParentAwareNode <T>; } else { previousNode.SetChild(currentNode.Next()); } _length--; } return(item); }
/// <summary> /// Remove item from list /// </summary> /// <param name="item">item to remove</param> /// <returns>Item, if it was successfully removed</returns> public T Remove(T item) { lock (_lock) { int i = 0; ParentAwareNode <T> previousNode = null; ParentAwareNode <T> currentNode = _head; while (i < _length && !item.Equals(currentNode.Value)) { previousNode = currentNode; currentNode = currentNode.Next() as ParentAwareNode <T>; i++; } if (i == _length) { throw new IndexOutOfRangeException(); } item = currentNode.Value; if (previousNode == null) { _head = currentNode.Next() as ParentAwareNode <T>; } else { previousNode.SetChild(currentNode.Next()); } _length--; } return(item); }
/// <summary> /// Prepends an item onto the list /// </summary> /// <param name="item">Item to prepend</param> /// <returns>Item that was successfully prepended.</returns> public T AddFirst(T item) { lock (_lock) { ParentAwareNode <T> newNode = new ParentAwareNode <T>(item); if (_head == null) { _head = _last = newNode; } else { newNode.SetChild(_head); _head = newNode; } _length++; } return(item); }
/// <summary> /// Adds an item to the end of a list /// </summary> /// <param name="item">Item to add</param> /// <returns>The item that was successfully added</returns> public T Add(T item) { lock (_lock) { ParentAwareNode <T> newNode = new ParentAwareNode <T>(item); if (_head == null) { _head = _last = newNode; } else { _last.SetChild(newNode); newNode.SetParent(_last); _last = newNode; } _length++; } return(item); }