0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 modes available
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.