/// <summary> /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </summary> /// <returns> /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </returns> /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception> public bool Remove(T item) { var x = Root; while (x.Next != Root) { if (item.CompareTo(x.Value) == 0) { x.Previous.Next = x.Next; x.Next.Previous = x.Previous; if (Count == 1) { Root = null; } else if (x == Root) { Root = x.Next; } Count--; return(true); } x = x.Next; } return(false); }
/// <summary> /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </summary> /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception> public void Add(T item) { if (Count == 0) { Root = new CircularLinkedNode <T>(item); Root.Next = Root; Root.Previous = Root; } else { CircularLinkedNode <T> x = new CircularLinkedNode <T>(item); x.Next = Root.Next; Root.Next = x; x.Next.Previous = x; x.Previous = Root; } Count++; }
public CircularLinkedNode(T value) { Value = value; Next = Previous = null; }
/// <summary> /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </summary> /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception> public void Clear() { Root = null; Count = 0; }