using System; using System.Collections.Generic; using JD.Collections.Immutable; namespace JD.Collections { /// /// A stack implemented using a singly-linked list, allowing for easy and fast shallow cloning. /// public sealed class LinkedListStack : IEnumerable, ICloneable { #region Variables private ImmutableStack m_top = new ImmutableStack(); #endregion #region Constructors /// /// Constructor. /// public LinkedListStack() { } private LinkedListStack(LinkedListStack other) { m_top = other.m_top; } #endregion #region Properties /// /// Returns true if the is empty. /// public bool IsEmpty { get { return m_top.IsEmpty; } } #endregion #region Methods /// /// Inserts an object at the top of the . /// /// /// This method is an O(1) operation. /// /// The object to push onto the . The value can be a null reference for reference types. public void Push(T value) { m_top = m_top.Push(value); } /// /// Removes and returns the object at the top of the . /// /// /// This method is similar to the method, but does not modify the . /// This method is an O(1) operation. /// /// The object removed from the top of the . public T Pop() { T current = m_top.Peek(); m_top = m_top.Pop(); return current; } /// /// Returns the object at the top of the without removing it. /// /// /// This method is similar to the method, but does not modify the . /// This method is an O(1) operation. /// /// The object at the top of the . /// The Stack is empty. public T Peek() { return m_top.Peek(); } /// /// Clones the current stack. /// /// public LinkedListStack Clone() { return new LinkedListStack(this); } object ICloneable.Clone() { return Clone(); } IEnumerator IEnumerable.GetEnumerator() { return m_top.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return ((IEnumerable)this).GetEnumerator(); } #endregion } }