Bird
Raised Fist0
DSA C#programming

Check if Stack is Empty or Full in DSA C#

Choose your learning style10 modes available
Mental Model
A stack is like a pile where you add or remove items only from the top. Checking if it is empty means seeing if there is nothing in the pile, and checking if it is full means seeing if the pile has reached its limit.
Analogy: Imagine a stack of plates in a cupboard. You can only take the top plate or add a new plate on top. If there are no plates, the stack is empty. If the cupboard is full and cannot hold more plates, the stack is full.
Top -> [ ] -> [ ] -> [ ] -> null
Index 0  1     2
↑top points here if stack has items
Dry Run Walkthrough
Input: stack size 3, push 1, push 2, check empty, check full, push 3, check full, pop, check empty
Goal: Show how to check if the stack is empty or full after some push and pop operations
Step 1: push 1 onto stack
1 -> null
Top points at 1
Why: Add first item, stack is no longer empty
Step 2: push 2 onto stack
2 -> 1 -> null
Top points at 2
Why: Add second item, stack still not full
Step 3: check if stack is empty
2 -> 1 -> null
Top points at 2
Why: Stack has items, so it is not empty
Step 4: check if stack is full
2 -> 1 -> null
Top points at 2
Why: Stack size is 2, max is 3, so not full yet
Step 5: push 3 onto stack
3 -> 2 -> 1 -> null
Top points at 3
Why: Stack reaches max size, now it is full
Step 6: check if stack is full
3 -> 2 -> 1 -> null
Top points at 3
Why: Stack size equals max size, so it is full
Step 7: pop top item
2 -> 1 -> null
Top points at 2
Why: Remove top item, stack no longer full
Step 8: check if stack is empty
2 -> 1 -> null
Top points at 2
Why: Stack still has items, so not empty
Result:
2 -> 1 -> null
Stack is not empty
Stack is not full
Stack is full
Stack is not full
Stack is not empty
Annotated Code
DSA C#
using System;

class Stack {
    private int[] items;
    private int top;
    private int maxSize;

    public Stack(int size) {
        maxSize = size;
        items = new int[maxSize];
        top = -1; // empty stack
    }

    public bool IsEmpty() {
        return top == -1; // no items
    }

    public bool IsFull() {
        return top == maxSize - 1; // reached max index
    }

    public void Push(int value) {
        if (IsFull()) {
            Console.WriteLine("Stack is full, cannot push " + value);
            return;
        }
        top++;
        items[top] = value;
    }

    public int Pop() {
        if (IsEmpty()) {
            Console.WriteLine("Stack is empty, cannot pop");
            return -1;
        }
        int val = items[top];
        top--;
        return val;
    }

    public void PrintStack() {
        for (int i = top; i >= 0; i--) {
            Console.Write(items[i] + " -> ");
        }
        Console.WriteLine("null");
    }
}

class Program {
    static void Main() {
        Stack stack = new Stack(3);
        stack.Push(1);
        stack.Push(2);
        stack.PrintStack();
        Console.WriteLine("Is stack empty? " + stack.IsEmpty());
        Console.WriteLine("Is stack full? " + stack.IsFull());
        stack.Push(3);
        stack.PrintStack();
        Console.WriteLine("Is stack full? " + stack.IsFull());
        stack.Pop();
        stack.PrintStack();
        Console.WriteLine("Is stack empty? " + stack.IsEmpty());
    }
}
return top == -1; // no items
check if stack has no items by top index
return top == maxSize - 1; // reached max index
check if stack reached max capacity
if (IsFull()) { ... return; }
stop push if stack is full
top++; items[top] = value;
add new item on top and move top pointer
if (IsEmpty()) { ... return -1; }
stop pop if stack is empty
int val = items[top]; top--; return val;
remove top item and move top pointer down
OutputSuccess
2 -> 1 -> null Is stack empty? False Is stack full? False 3 -> 2 -> 1 -> null Is stack full? True 2 -> 1 -> null Is stack empty? False
Complexity Analysis
Time: O(1) for checking empty or full because it only compares the top index to fixed values
Space: O(n) where n is the max size of the stack because it stores items in an array
vs Alternative: Compared to a linked list stack, array stack uses fixed space and constant time checks for empty/full, while linked list stack cannot be full unless memory is exhausted
Edge Cases
empty stack
IsEmpty returns true, IsFull returns false, pop operation prevented
DSA C#
if (IsEmpty()) { ... return -1; }
stack with one item
IsEmpty returns false, IsFull depends on max size, push and pop work normally
DSA C#
return top == -1; // no items
stack full at max size
IsFull returns true, push operation prevented
DSA C#
if (IsFull()) { ... return; }
When to Use This Pattern
When you see a problem asking if a data structure is empty or full, especially with fixed size, reach for stack empty/full checks because they help avoid errors like overflow or underflow.
Common Mistakes
Mistake: Checking if stack is empty by comparing top to 0 instead of -1
Fix: Initialize top to -1 and check top == -1 for empty
Mistake: Not preventing push when stack is full, causing array index errors
Fix: Use IsFull check before pushing and stop if full
Summary
Checks if a stack has no items (empty) or has reached its capacity (full).
Use when managing fixed-size stacks to avoid adding or removing items incorrectly.
Remember top index -1 means empty, and top equal to max size minus one means full.

Practice

(1/5)
1. What does the IsEmpty() method typically check in a stack data structure?
easy
A. If the stack has no elements inside
B. If the stack is full and cannot add more elements
C. If the stack has exactly one element
D. If the stack has more than one element

Solution

  1. Step 1: Understand the purpose of IsEmpty()

    The IsEmpty() method checks whether the stack contains any elements or not.
  2. Step 2: Interpret the meaning of empty stack

    An empty stack means it has zero elements inside, so no items to pop or peek.
  3. Final Answer:

    If the stack has no elements inside -> Option A
  4. Quick Check:

    IsEmpty() = true means stack is empty [OK]
Hint: Empty means zero elements inside the stack [OK]
Common Mistakes:
  • Confusing empty with full
  • Thinking IsEmpty checks for one element
  • Assuming IsEmpty checks capacity
2. Which of the following is the correct C# syntax to check if a stack named myStack is full assuming it has a fixed size?
easy
A. if (myStack.Count == myStack.Capacity) { }
B. if (myStack.IsEmpty()) { }
C. if (myStack.Length == 0) { }
D. if (myStack.IsFull()) { }

Solution

  1. Step 1: Understand stack properties in C#

    A stack with fixed size uses Count for current elements and Capacity for max size.
  2. Step 2: Check if stack is full

    If Count equals Capacity, the stack is full and cannot add more elements.
  3. Final Answer:

    if (myStack.Count == myStack.Capacity) { } -> Option A
  4. Quick Check:

    Count == Capacity means full stack [OK]
Hint: Full means count equals capacity in fixed size stack [OK]
Common Mistakes:
  • Using IsEmpty() to check full
  • Checking Length property which stacks don't have
  • Using IsFull() without defining it
3. Consider this C# code snippet:
Stack<int> stack = new Stack<int>(3);
stack.Push(10);
stack.Push(20);
stack.Push(30);
bool isFull = stack.Count == 3;
bool isEmpty = stack.Count == 0;
Console.WriteLine(isFull);
Console.WriteLine(isEmpty);

What will be the output?
medium
A. True\nTrue
B. True\nFalse
C. False\nTrue
D. False\nFalse

Solution

  1. Step 1: Analyze stack operations

    Three elements (10, 20, 30) are pushed into a stack with capacity 3, so stack is full.
  2. Step 2: Evaluate isFull and isEmpty values

    isFull = stack.Count == 3 is true; isEmpty = stack.Count == 0 is false.
  3. Final Answer:

    True\nFalse -> Option B
  4. Quick Check:

    Count=3 means full=true, empty=false [OK]
Hint: Count equals capacity means full; zero means empty [OK]
Common Mistakes:
  • Assuming stack is empty after pushes
  • Confusing isFull and isEmpty values
  • Ignoring stack capacity
4. The following C# code tries to check if a stack is full but causes a compile error:
Stack<int> stack = new Stack<int>();
if (stack.IsFull()) {
    Console.WriteLine("Stack is full");
}

What is the error and how to fix it?
medium
A. Change Stack<int> to List<int>
B. Stack cannot be empty; remove the if condition
C. IsFull() method does not exist; use stack.Count == capacity instead
D. Use stack.IsEmpty() instead of IsFull()

Solution

  1. Step 1: Identify the error

    The IsFull() method is not defined in C# Stack class, causing a compile error.
  2. Step 2: Correct the check for full stack

    Use stack.Count == capacity to check if stack is full, since Stack has no IsFull() method.
  3. Final Answer:

    IsFull() method does not exist; use stack.Count == capacity instead -> Option C
  4. Quick Check:

    No IsFull() method in Stack class [OK]
Hint: Stack class has no IsFull(); check count vs capacity [OK]
Common Mistakes:
  • Assuming IsFull() exists by default
  • Using IsEmpty() to check full
  • Changing data structure unnecessarily
5. You have a fixed size stack of capacity 5. After pushing 3 elements, you want to check if you can push 2 more without overflow. Which condition correctly checks this in C#?
hard
A. if (stack.Count - 2 >= 0) { /* safe to push */ }
B. if (stack.IsEmpty()) { /* safe to push */ }
C. if (stack.Count == 5) { /* safe to push */ }
D. if (stack.Count + 2 <= 5) { /* safe to push */ }

Solution

  1. Step 1: Understand stack capacity and current count

    Stack capacity is 5, current count is 3, so 2 more pushes are possible if total does not exceed 5.
  2. Step 2: Formulate condition to check safe push

    Check if current count plus 2 is less than or equal to capacity: stack.Count + 2 <= 5.
  3. Final Answer:

    if (stack.Count + 2 <= 5) { /* safe to push */ } -> Option D
  4. Quick Check:

    Count + pushes ≀ capacity means safe [OK]
Hint: Add pushes to count; compare with capacity [OK]
Common Mistakes:
  • Using IsEmpty() to check push safety
  • Checking count equals capacity incorrectly
  • Subtracting pushes instead of adding
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard