Bird
Raised Fist0
DSA Cprogramming~10 mins

Check if Stack is Empty or Full in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Check if Stack is Empty or Full
Start
↓
Check if top == -1?
Yes→Stack is Empty
|No↓
Check if top == max_size-1?
Yes→Stack is Full
|No↓
Stack has space
↓
End
The flow checks the top pointer to decide if the stack is empty or full, then concludes the stack state.
Execution Sample
DSA C
int isEmpty() {
  return top == -1;
}

int isFull() {
  return top == max_size - 1;
}
This code checks if the stack is empty or full by comparing the top index with boundary values.
Execution Table
StepOperationtop Valuemax_sizeCondition CheckedResultStack Visual State
1Initial state-15top == -1True[] (empty)
2Check isEmpty()-15top == -1True[] (empty)
3Push 1005top == -1?No[10]
4Check isEmpty()05top == -1False[10]
5Push 2015top == max_size-1 (4)?No[10, 20]
6Push 3025top == max_size-1 (4)?No[10, 20, 30]
7Push 4035top == max_size-1 (4)?No[10, 20, 30, 40]
8Push 5045top == max_size-1 (4)?Yes[10, 20, 30, 40, 50]
9Check isFull()45top == max_size-1 (4)True[10, 20, 30, 40, 50]
10Attempt Push 6045top == max_size-1 (4)?Yes - Cannot push[10, 20, 30, 40, 50]
11Pop35top == -1?No[10, 20, 30, 40]
12Check isFull()35top == max_size-1 (4)?No[10, 20, 30, 40]
13Check isEmpty()35top == -1?No[10, 20, 30, 40]
14Pop all to empty-15top == -1?Yes[] (empty)
15Check isEmpty()-15top == -1?True[] (empty)
💡 Execution stops after stack is empty and checks confirm empty state.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6After Step 7After Step 8After Step 9After Step 11After Step 14
top-10123443-1
max_size555555555
Stack[][10][10, 20][10, 20, 30][10, 20, 30, 40][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40][]
Key Moments - 3 Insights
Why does the stack start with top = -1 instead of 0?
Because top = -1 means the stack is empty (no elements). This is shown in execution_table step 1 where top == -1 means empty stack.
Why do we check top == max_size - 1 to know if the stack is full?
Because top points to the last filled index. When top equals max_size - 1, the stack has no space left, as shown in step 8 and 9.
What happens if we try to push when the stack is full?
The push operation is blocked and top does not change, as shown in step 10 where top remains 4 and stack state is unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the top value after pushing 30 (step 6)?
A1
B2
C3
D4
💡 Hint
Check the 'top Value' column at step 6 in execution_table.
At which step does the stack become full?
AStep 8
BStep 7
CStep 9
DStep 10
💡 Hint
Look for when 'top == max_size-1' condition becomes True in execution_table.
If max_size was 3 instead of 5, at which step would the stack be full?
AStep 7
BStep 5
CStep 6
DStep 8
💡 Hint
max_size-1 would be 2, so top == 2 means full; check when top reaches 2 in variable_tracker.
Concept Snapshot
Stack uses 'top' index to track elements.
Empty stack: top == -1.
Full stack: top == max_size - 1.
Check empty/full by comparing top.
Push increments top if not full.
Pop decrements top if not empty.
Full Transcript
This concept shows how to check if a stack is empty or full by using the 'top' index. The stack starts empty with top = -1. When top equals max_size - 1, the stack is full. The code checks these conditions to prevent invalid push or pop operations. The execution table traces pushing elements until full, checking empty and full states, and popping elements back to empty. Variable tracker shows how 'top' and stack contents change step-by-step. Key moments clarify why top starts at -1 and how full condition is detected. The quiz tests understanding of top values and full condition steps.

Practice

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

Solution

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

    The isEmpty() function checks whether the stack contains any elements or not.
  2. Step 2: Identify what 'empty' means for a stack

    A stack is empty when it has zero elements, meaning no data is stored inside.
  3. Final Answer:

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

    Empty stack means zero elements = true [OK]
Hint: Empty stack means no elements inside [OK]
Common Mistakes:
  • Confusing empty with full
  • Thinking empty means one element
  • Mixing up stack size with capacity
2. Which of the following is the correct syntax to check if a stack is full in C, assuming top is the index of the top element and MAX is the stack capacity?
easy
A. if (top == MAX - 1)
B. if (top < MAX - 1)
C. if (top > MAX)
D. if (top == MAX)

Solution

  1. Step 1: Understand stack indexing

    Stack indices start at 0, so the last valid index is MAX - 1.
  2. Step 2: Check condition for full stack

    The stack is full when top points to the last index, which is MAX - 1.
  3. Final Answer:

    if (top == MAX - 1) -> Option A
  4. Quick Check:

    Top equals max index means full [OK]
Hint: Stack full when top equals MAX minus one [OK]
Common Mistakes:
  • Using top == MAX (off by one error)
  • Using top > MAX (invalid index)
  • Using top < MAX - 1 (means not full)
3. Consider this C code snippet for a stack with MAX = 3:
int stack[MAX];
int top = -1;

// Push elements
if (top < MAX - 1) top++;
stack[top] = 10;
if (top < MAX - 1) top++;
stack[top] = 20;

// Check if full
if (top == MAX - 1) printf("Full"); else printf("Not Full");
What will be printed?
medium
A. Not Full
B. Full
C. Runtime Error
D. Compilation Error

Solution

  1. Step 1: Trace the push operations

    Initially, top = -1. First push increments top to 0 and stores 10. Second push increments top to 1 and stores 20.
  2. Step 2: Check if stack is full

    Full means top == MAX - 1 which is 2. Here, top is 1, so stack is not full.
  3. Final Answer:

    Not Full -> Option A
  4. Quick Check:

    Top is 1, max index 2, so not full [OK]
Hint: Full means top equals MAX-1 index [OK]
Common Mistakes:
  • Assuming two pushes fill the stack
  • Confusing top index with count
  • Ignoring initial top = -1
4. Find the error in this C function that checks if a stack is empty:
int isEmpty(int top) {
    if (top == 0)
        return 1;
    else
        return 0;
}
medium
A. It should check if top == MAX - 1
B. It should check if top == -1 instead of 0
C. It should return 0 when empty
D. No error, function is correct

Solution

  1. Step 1: Understand stack empty condition

    In stack implementation, empty means top == -1, not 0.
  2. Step 2: Identify the incorrect condition

    The function checks top == 0, which means stack has one element, so it's wrong.
  3. Final Answer:

    It should check if top == -1 instead of 0 -> Option B
  4. Quick Check:

    Empty stack top is -1, not 0 [OK]
Hint: Empty stack top is -1, not zero [OK]
Common Mistakes:
  • Checking top == 0 for empty
  • Confusing full and empty conditions
  • Returning wrong boolean values
5. You have a stack with capacity 5 and top = 3. You want to push two elements safely. Which sequence of checks and operations is correct to avoid overflow?
hard
A. No need to check; just increment top and push
B. Check if top == 5 before each push; if false, increment top and push
C. Check if top == 3 before each push; if false, increment top and push
D. Check if top == 4 before each push; if false, increment top and push

Solution

  1. Step 1: Understand stack capacity and top indexing

    Stack capacity is 5, so max top index is 4 (0 to 4).
  2. Step 2: Check condition before pushing

    Before each push, check if top == 4 (full). If not full, increment top and push.
  3. Final Answer:

    Check if top == 4 before each push; if false, increment top and push -> Option D
  4. Quick Check:

    Max index is capacity-1 = 4, check top == 4 [OK]
Hint: Check top equals capacity minus one before push [OK]
Common Mistakes:
  • Checking top == capacity (off by one)
  • Not checking before push causes overflow
  • Assuming top == 3 means full
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