using System.Collections; using System.Collections.Generic; namespace Advanced.Algorithms.DataStructures.Foundation; /// /// A stack implementation. /// public class Stack : IEnumerable { private readonly IStack stack; /// The stack type to use. public Stack(StackType type = StackType.Array) { if (type == StackType.Array) stack = new ArrayStack(); else stack = new LinkedListStack(); } /// /// The total number of items in this stack. /// public int Count => stack.Count; public IEnumerator GetEnumerator() { return GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return stack.GetEnumerator(); } /// /// Time complexity:O(1). /// /// The item popped. public T Pop() { return stack.Pop(); } /// /// Time complexity:O(1). /// /// The item to push. public void Push(T item) { stack.Push(item); } /// /// Peek from stack. /// Time complexity:O(1). /// /// The item peeked. public T Peek() { return stack.Peek(); } } internal interface IStack : IEnumerable { int Count { get; } T Pop(); void Push(T item); T Peek(); } /// /// The stack implementation types. /// public enum StackType { Array = 0, LinkedList = 1 }