Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Queue and Stack behavior in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Queue and Stack behavior
Start
Choose Data Structure
Stack (LIFO)
Push/Pop
Last In, First Out
End
This flow shows choosing between Stack and Queue, then performing their main operations: Stack uses push/pop with last-in-first-out order, Queue uses enqueue/dequeue with first-in-first-out order.
Execution Sample
C Sharp (C#)
Stack<int> stack = new Stack<int>();
stack.Push(10);
stack.Push(20);
int top = stack.Pop();

Queue<int> queue = new Queue<int>();
queue.Enqueue(10);
queue.Enqueue(20);
int front = queue.Dequeue();
This code adds two numbers to a stack and a queue, then removes one element from each, showing their different behaviors.
Execution Table
StepOperationStack State (top on right)Queue State (front on left)OutputExplanation
1Create empty stack and queue[][]Both stack and queue start empty
2stack.Push(10)[10][]10 added to stack top
3stack.Push(20)[10, 20][]20 added on top of 10
4stack.Pop()[10][]2020 removed from stack top (last in)
5queue.Enqueue(10)[10][10]10 added to queue end
6queue.Enqueue(20)[10][10, 20]20 added behind 10 in queue
7queue.Dequeue()[10][20]1010 removed from queue front (first in)
8End[10][20]Stack and queue have one element each after removals
💡 Operations complete; stack and queue show different removal orders.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
stackempty[10][10, 20][10][10][10][10][10]
queueemptyemptyemptyempty[10][10, 20][20][20]
top (stack.Pop())N/AN/AN/A20N/AN/AN/AN/A
front (queue.Dequeue())N/AN/AN/AN/AN/AN/A10N/A
Key Moments - 3 Insights
Why does stack.Pop() return 20, not 10?
Because stack is Last In First Out (LIFO). The last pushed item (20) is removed first, as shown in step 4 of the execution table.
Why does queue.Dequeue() return 10, not 20?
Queue is First In First Out (FIFO). The first enqueued item (10) is removed first, as shown in step 7 of the execution table.
After popping from stack and dequeuing from queue, why do their states differ?
Stack removes from the top (last added), queue removes from the front (first added). So after removal, stack has [10], queue has [20], as shown in step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the value returned by stack.Pop()?
A20
B10
CEmpty
DError
💡 Hint
Check the 'Output' column at step 4 in the execution table.
At which step does the queue first contain two elements?
AStep 3
BStep 6
CStep 5
DStep 7
💡 Hint
Look at the 'Queue State' column to see when two elements appear.
If we called stack.Pop() again after step 4, what would be the next output?
A20
BEmpty
C10
DError
💡 Hint
After popping 20, the next top is 10 as shown in the stack state after step 4.
Concept Snapshot
Stack and Queue behavior:
- Stack: Last In First Out (LIFO)
- Use Push() to add, Pop() to remove top
- Queue: First In First Out (FIFO)
- Use Enqueue() to add, Dequeue() to remove front
- Stack removes newest item first, queue removes oldest item first
Full Transcript
This lesson shows how stacks and queues work differently. A stack adds items on top and removes the last added item first, called Last In First Out. A queue adds items at the end and removes the first added item first, called First In First Out. The example code pushes 10 and 20 onto a stack, then pops the top (20). It also enqueues 10 and 20 into a queue, then dequeues the front (10). The execution table tracks each step, showing how the stack and queue states change. Key moments explain why stack.Pop() returns 20 and queue.Dequeue() returns 10. The quiz tests understanding of these behaviors.

Practice

(1/5)
1. Which data structure removes elements in the order they were added, like a line at a grocery store?
easy
A. Array
B. Stack
C. Dictionary
D. Queue

Solution

  1. Step 1: Understand FIFO behavior

    A queue removes elements in the order they were added, called First-In-First-Out (FIFO).
  2. Step 2: Match behavior to real-life example

    A line at a grocery store is FIFO, so the queue matches this behavior.
  3. Final Answer:

    Queue -> Option D
  4. Quick Check:

    FIFO = Queue [OK]
Hint: FIFO means first in, first out like a queue line [OK]
Common Mistakes:
  • Confusing stack with queue
  • Thinking stack is FIFO
  • Mixing array behavior with queue
  • Assuming dictionary has order
2. Which of the following is the correct way to add an item to a Stack in C#?
easy
A. stack.Push(item);
B. stack.Enqueue(item);
C. stack.Add(item);
D. stack.Insert(item);

Solution

  1. Step 1: Recall Stack method names

    In C#, Stack uses Push() to add items on top.
  2. Step 2: Identify correct method

    Enqueue is for Queue, Add and Insert are not Stack methods.
  3. Final Answer:

    stack.Push(item); -> Option A
  4. Quick Check:

    Push adds to Stack [OK]
Hint: Use Push() to add to Stack, Enqueue() for Queue [OK]
Common Mistakes:
  • Using Enqueue() on Stack
  • Using Add() or Insert() which don't exist
  • Confusing Stack and Queue methods
  • Syntax errors with method calls
3. What is the output of this C# code?
var stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
Console.WriteLine(stack.Pop());
Console.WriteLine(stack.Peek());
medium
A. 3\n2
B. 1\n2
C. 2\n3
D. 3\n3

Solution

  1. Step 1: Trace Push operations

    Stack after pushes: bottom=1, middle=2, top=3.
  2. Step 2: Execute Pop and Peek

    Pop() removes and returns top (3). Peek() returns new top (2) without removing.
  3. Final Answer:

    3\n2 -> Option A
  4. Quick Check:

    Pop=3, Peek=2 [OK]
Hint: Pop removes top, Peek shows top without removing [OK]
Common Mistakes:
  • Mixing Pop and Peek results
  • Assuming FIFO order
  • Confusing stack order
  • Forgetting Pop removes item
4. Identify the error in this C# code using Queue:
var queue = new Queue<string>();
queue.Push("apple");
queue.Enqueue("banana");
Console.WriteLine(queue.Dequeue());
medium
A. Dequeue() returns last item added
B. Queue does not have Push() method
C. Enqueue() should be Dequeue()
D. Queue cannot store strings

Solution

  1. Step 1: Check Queue methods

    Queue uses Enqueue() to add, not Push().
  2. Step 2: Identify incorrect method usage

    Calling Push() on Queue causes a compile error.
  3. Final Answer:

    Queue does not have Push() method -> Option B
  4. Quick Check:

    Queue uses Enqueue, no Push [OK]
Hint: Queue uses Enqueue(), Stack uses Push() [OK]
Common Mistakes:
  • Using Push() on Queue
  • Confusing Enqueue and Dequeue
  • Thinking Dequeue returns last item
  • Assuming Queue can't hold strings
5. You want to reverse the order of words in a sentence using C#. Which data structure is best and why?
string sentence = "hello world from C#";
hard
A. Queue, because it keeps original order using FIFO
B. Dictionary, because it stores key-value pairs
C. Stack, because it reverses order using LIFO
D. List, because it sorts items automatically

Solution

  1. Step 1: Understand the goal

    Reversing words means last word should come first, so order is reversed.
  2. Step 2: Choose data structure behavior

    Stack uses Last-In-First-Out (LIFO), perfect for reversing order.
  3. Step 3: Eliminate other options

    Queue keeps order (FIFO), Dictionary stores pairs unordered, List does not reverse automatically.
  4. Final Answer:

    Stack, because it reverses order using LIFO -> Option C
  5. Quick Check:

    Reverse order = Stack (LIFO) [OK]
Hint: Use Stack to reverse order with LIFO behavior [OK]
Common Mistakes:
  • Choosing Queue for reversing
  • Thinking List auto-sorts
  • Using Dictionary for order
  • Ignoring LIFO vs FIFO difference