using System.Collections.Generic; using System.Linq; /// /// Extensions for . /// public static class QueueExtensions { /// /// Removes the last item from the . /// /// Type of object in . /// Instance of to remove item from. /// Item removed from the . public static T DequeueLast(this Queue q) { for (var i = 1; i < q.Count; i++) q.Enqueue(q.Dequeue()); return q.Dequeue(); } /// /// Removes the last item(s) from the . /// /// Type of object in . /// Instance of to remove item from. /// Number of items to pop off the end of the . /// Item removed from the . public static IEnumerable DequeueLast(this Queue q, int quantity) { for (var i = quantity; i < q.Count; i++) q.Enqueue(q.Dequeue()); var poppedItems = new List(quantity); for (int i = 0; i < quantity; i++) poppedItems.Add(q.Dequeue()); return poppedItems; } /// /// Adds an item() to the start of the . /// /// Type of object in . /// Instance of to remove item from. public static void EnqueueFirst(this Queue q, T item) { q.Enqueue(item); for (var i = 1; i < q.Count; i++) q.Enqueue(q.Dequeue()); } /// /// Adds items() to the start of the . /// /// Type of object in . /// Instance of to remove item from. /// List of items() to add to the . public static void EnqueueFirst(this Queue q, IEnumerable items) { if (items == null || !items.Any()) return; foreach (var item in items) q.Enqueue(item); for (var i = items.Count(); i < q.Count; i++) q.Enqueue(q.Dequeue()); } }