using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; namespace DataStructures.QueueSpace { /// /// /// /// [Serializable] public class Deque : IEnumerable { private List internalList; public int Count { get { return internalList.Count; } } public int Capacity { get { return internalList.Capacity; } } public T PeekFirst { get { if (!internalList.Any()) { return default(T); } return internalList[0]; } } public T PeekLast { get { if(!internalList.Any()) { return default(T); } return internalList[internalList.Count - 1]; } } /// /// Creates a queue using default capacity /// public Deque() { internalList = new List(); } /// /// Creates a deque with default capacity /// /// Default capacity of deque public Deque(int capacity) { Contract.Requires(capacity > 0); internalList = new List(capacity); } public void AddFirst(T item) { Contract.Requires(item != null); internalList.Insert(0, item); } /// /// Adds an item to the last of deque /// /// public void AddLast(T item) { Contract.Requires(item != null); internalList.Add(item); } /// /// /// /// Returns null if list is empty public T RemoveFirst() { if (!internalList.Any()) { return default(T); } T element = internalList[0]; internalList.RemoveAt(0); return element; } /// /// /// /// Returns null if list is empty public T RemoveLast() { if (!internalList.Any()) { return default(T); } T element = internalList[Count - 1]; internalList.RemoveAt(Count - 1); return element; } public IEnumerator GetEnumerator() { return internalList.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } } }