Queues and stacks help us organize data in a specific order so we can add and remove items easily. They work like real-life lines or piles.
Queue and Stack behavior in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
using System; using System.Collections.Generic; public class QueueAndStackDemo { public static void Main() { // Queue example Queue<int> numberQueue = new Queue<int>(); numberQueue.Enqueue(1); // Add to end numberQueue.Enqueue(2); int firstInQueue = numberQueue.Dequeue(); // Remove from front // Stack example Stack<int> numberStack = new Stack<int>(); numberStack.Push(1); // Add to top numberStack.Push(2); int topOfStack = numberStack.Pop(); // Remove from top } }
Queue uses FIFO (First In First Out) order: first added is first removed.
Stack uses LIFO (Last In First Out) order: last added is first removed.
Queue<string> tasks = new Queue<string>(); tasks.Enqueue("Task1"); tasks.Enqueue("Task2"); string nextTask = tasks.Dequeue();
Stack<string> history = new Stack<string>(); history.Push("Page1"); history.Push("Page2"); string lastPage = history.Pop();
Queue<int> emptyQueue = new Queue<int>(); // Dequeue on empty queue throws InvalidOperationException
Stack<int> singleStack = new Stack<int>(); singleStack.Push(10); int onlyItem = singleStack.Pop();
This program shows how a queue and a stack work. It adds items, shows them, removes one, then shows the remaining items.
using System; using System.Collections.Generic; public class QueueAndStackBehavior { public static void Main() { // Create a queue and add items Queue<string> customerQueue = new Queue<string>(); customerQueue.Enqueue("Alice"); customerQueue.Enqueue("Bob"); customerQueue.Enqueue("Charlie"); Console.WriteLine("Queue before dequeue:"); foreach (string customer in customerQueue) { Console.WriteLine(customer); } // Remove one item from queue string servedCustomer = customerQueue.Dequeue(); Console.WriteLine($"\nServed customer: {servedCustomer}"); Console.WriteLine("Queue after dequeue:"); foreach (string customer in customerQueue) { Console.WriteLine(customer); } // Create a stack and add items Stack<string> bookStack = new Stack<string>(); bookStack.Push("Book1"); bookStack.Push("Book2"); bookStack.Push("Book3"); Console.WriteLine("\nStack before pop:"); foreach (string book in bookStack) { Console.WriteLine(book); } // Remove one item from stack string topBook = bookStack.Pop(); Console.WriteLine($"\nRemoved from stack: {topBook}"); Console.WriteLine("Stack after pop:"); foreach (string book in bookStack) { Console.WriteLine(book); } } }
Queue operations Enqueue and Dequeue run in O(1) time.
Stack operations Push and Pop run in O(1) time.
Trying to Dequeue or Pop from an empty collection throws an exception; always check if empty first.
Use Queue when order matters and you want to process oldest items first.
Use Stack when you want to reverse order or track recent items first.
Queue follows FIFO: first added, first removed.
Stack follows LIFO: last added, first removed.
Both are useful for organizing data in different real-life scenarios.
Practice
Solution
Step 1: Understand FIFO behavior
A queue removes elements in the order they were added, called First-In-First-Out (FIFO).Step 2: Match behavior to real-life example
A line at a grocery store is FIFO, so the queue matches this behavior.Final Answer:
Queue -> Option DQuick Check:
FIFO = Queue [OK]
- Confusing stack with queue
- Thinking stack is FIFO
- Mixing array behavior with queue
- Assuming dictionary has order
Solution
Step 1: Recall Stack method names
In C#, Stack uses Push() to add items on top.Step 2: Identify correct method
Enqueue is for Queue, Add and Insert are not Stack methods.Final Answer:
stack.Push(item); -> Option AQuick Check:
Push adds to Stack [OK]
- Using Enqueue() on Stack
- Using Add() or Insert() which don't exist
- Confusing Stack and Queue methods
- Syntax errors with method calls
var stack = new Stack<int>(); stack.Push(1); stack.Push(2); stack.Push(3); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Peek());
Solution
Step 1: Trace Push operations
Stack after pushes: bottom=1, middle=2, top=3.Step 2: Execute Pop and Peek
Pop() removes and returns top (3). Peek() returns new top (2) without removing.Final Answer:
3\n2 -> Option AQuick Check:
Pop=3, Peek=2 [OK]
- Mixing Pop and Peek results
- Assuming FIFO order
- Confusing stack order
- Forgetting Pop removes item
var queue = new Queue<string>();
queue.Push("apple");
queue.Enqueue("banana");
Console.WriteLine(queue.Dequeue());Solution
Step 1: Check Queue methods
Queue uses Enqueue() to add, not Push().Step 2: Identify incorrect method usage
Calling Push() on Queue causes a compile error.Final Answer:
Queue does not have Push() method -> Option BQuick Check:
Queue uses Enqueue, no Push [OK]
- Using Push() on Queue
- Confusing Enqueue and Dequeue
- Thinking Dequeue returns last item
- Assuming Queue can't hold strings
string sentence = "hello world from C#";
Solution
Step 1: Understand the goal
Reversing words means last word should come first, so order is reversed.Step 2: Choose data structure behavior
Stack uses Last-In-First-Out (LIFO), perfect for reversing order.Step 3: Eliminate other options
Queue keeps order (FIFO), Dictionary stores pairs unordered, List does not reverse automatically.Final Answer:
Stack, because it reverses order using LIFO -> Option CQuick Check:
Reverse order = Stack (LIFO) [OK]
- Choosing Queue for reversing
- Thinking List auto-sorts
- Using Dictionary for order
- Ignoring LIFO vs FIFO difference
