0
0
DSA Pythonprogramming~15 mins

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

Choose your learning style9 modes available
Check if Stack is Empty or Full
📖 Scenario: Imagine you have a stack of books on a shelf. You want to check if the shelf is empty or if it is full and cannot hold more books.
🎯 Goal: You will create a simple stack using a list, set a maximum size, write functions to check if the stack is empty or full, and then print the results.
📋 What You'll Learn
Create a list called stack with these exact elements: ['book1', 'book2', 'book3']
Create a variable called max_size and set it to 5
Write a function called is_empty that returns True if stack is empty, otherwise False
Write a function called is_full that returns True if the length of stack is equal to max_size, otherwise False
Print the result of is_empty() and is_full() exactly as shown
💡 Why This Matters
🌍 Real World
Stacks are used in many places like undo features in apps, browser history, and managing tasks in order.
💼 Career
Understanding stack operations is important for software development, especially in areas like memory management and algorithm design.
Progress0 / 4 steps
1
Create the initial stack
Create a list called stack with these exact elements: 'book1', 'book2', 'book3'
DSA Python
Hint

Use square brackets to create a list and include the three book names as strings.

2
Set the maximum stack size
Create a variable called max_size and set it to 5
DSA Python
Hint

Just assign the number 5 to the variable max_size.

3
Write functions to check if stack is empty or full
Write a function called is_empty that returns True if stack is empty, otherwise False. Then write a function called is_full that returns True if the length of stack is equal to max_size, otherwise False
DSA Python
Hint

Use len(stack) to check the number of items in the stack.

4
Print the results of the checks
Print the result of is_empty() and is_full() exactly as shown
DSA Python
Hint

Use print("Is stack empty?", is_empty()) and similarly for is_full().