Stacks and Queues
Queue Enqueue/Dequeue
Enqueue values at the back and dequeue them from the front in first-in, first-out order.
Algorithm
Basic Implementation
basic.cs
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static string Render(List<int> values) => string.Join(" -> ", values);
static void Main() {
var queue = new Queue<int>();
foreach (var value in new[] {10, 20, 30}) queue.Enqueue(value);
var removed = new List<int>();
while (queue.Count > 0) removed.Add(queue.Dequeue());
Console.WriteLine(Render(removed));
}
}
Complexity
- Time: O(1) per operation with a real queue
- Space: O(n)
Implementation notes
Queue<int>is the BCL FIFO container; its managed backing storage grows as needed and is reclaimed by GC. CallingDequeue()on an empty queue would throw, so the implementation guards removal withqueue.Count > 0.- The removed values are collected in a
List<int>only for deterministic rendering; the replay still shows the actual enqueue/dequeue state changes rather than jumping straight to the finala -> b -> cstring.
front
The front is the oldest value still waiting in the queue.
FIFO
A queue removes values in first-in, first-out order.