Bird
0
0
DSA Cprogramming~30 mins

Check if Stack is Empty or Full in DSA C - Build from Scratch

Choose your learning style9 modes available
Check if Stack is Empty or Full
📖 Scenario: Imagine you are managing a stack of books on a shelf. You want to know if the shelf is empty or completely full before adding or removing books.
🎯 Goal: You will create a simple stack using an array and write code to check if the stack is empty or full.
📋 What You'll Learn
Create an integer array called stack with size 5
Create an integer variable called top initialized to -1
Create an integer variable called max_size set to 5
Write a function isEmpty that returns 1 if the stack is empty, else 0
Write a function isFull that returns 1 if the stack is full, else 0
Print the results of isEmpty() and isFull()
💡 Why This Matters
🌍 Real World
Stacks are used in many places like undo features in apps, browser history, and managing tasks.
💼 Career
Understanding stack operations is important for software development, debugging, and working with system internals.
Progress0 / 4 steps
1
Create the stack array and top variable
Create an integer array called stack with size 5 and an integer variable called top initialized to -1.
DSA C
Hint

The stack is an array of size 5. The top variable keeps track of the last filled position, starting at -1 when empty.

2
Add max_size variable
Add an integer variable called max_size and set it to 5.
DSA C
Hint

The max_size variable holds the maximum number of items the stack can hold.

3
Write isEmpty and isFull functions
Write a function isEmpty that returns 1 if top is -1, else 0. Write a function isFull that returns 1 if top equals max_size - 1, else 0.
DSA C
Hint

The stack is empty if top is -1. It is full if top is one less than max_size.

4
Print if stack is empty or full
Print the results of isEmpty() and isFull() using printf.
DSA C
Hint

Use printf to show the results of isEmpty() and isFull(). Since top is -1, the stack is empty and not full.