📖 Scenario: Imagine you are managing a line at a coffee shop and a stack of plates in the kitchen. You want to understand how to use a queue and a stack to organize these tasks properly.
🎯 Goal: You will create a queue and a stack using Python lists, add items to each, and then remove items to see how they behave differently. This will help you understand when to use a queue or a stack.
📋 What You'll Learn
Create a queue list with specific customers in order
Create a stack list with specific plates in order
Add a new customer to the queue and a new plate to the stack
Remove one customer from the queue and one plate from the stack
Print the final state of both the queue and the stack
💡 Why This Matters
🌍 Real World
Queues are used in real life when people wait in line, like at a coffee shop or bank. Stacks are used when you stack plates or books and take from the top.
💼 Career
Understanding queues and stacks helps in programming tasks like managing tasks, undo features, and breadth-first or depth-first searches.
Progress0 / 4 steps
1
Create the initial queue and stack
Create a list called queue with these exact customers in order: 'Alice', 'Bob', 'Charlie'. Also create a list called stack with these exact plates in order: 'Plate1', 'Plate2', 'Plate3'.
DSA Python
Hint
Use square brackets to create lists and separate items with commas.
2
Add a new customer to the queue and a new plate to the stack
Add 'David' to the end of the queue list using the append() method. Also add 'Plate4' to the end of the stack list using the append() method.
DSA Python
Hint
Use list.append(item) to add an item to the end of a list.
3
Remove one customer from the queue and one plate from the stack
Remove the first customer from the queue list using pop(0). Remove the last plate from the stack list using pop() without arguments.
DSA Python
Hint
Use pop(0) to remove the first item from a list (queue behavior) and pop() to remove the last item (stack behavior).
4
Print the final state of the queue and stack
Print the queue list and then print the stack list to show their final states after the removals.
DSA Python
Hint
Use print() to display the lists.
Practice
(1/5)
1. Which data structure should you use when you want to process items in the exact order they arrive, like people standing in a line at a ticket counter?
easy
A. Queue
B. Stack
C. Tree
D. Graph
Solution
Step 1: Understand the order of processing
People in line are served in the order they arrive, which is First In First Out (FIFO).
Step 2: Match the data structure to FIFO
A Queue follows FIFO, so it fits this scenario perfectly.
Final Answer:
Queue -> Option A
Quick Check:
FIFO = Queue [OK]
Hint: Use Queue for FIFO order, like lines [OK]
Common Mistakes:
Confusing Stack with Queue
Thinking Stack processes in arrival order
Choosing Tree or Graph for simple order
2. Which of the following is the correct way to add an element to a stack in Python using a list named stack?
easy
A. stack.enqueue(element)
B. stack.push(element)
C. stack.append(element)
D. stack.insert(0, element)
Solution
Step 1: Recall Python list methods for stack
Python lists use append() to add elements at the end, simulating stack push.
Step 2: Check other options
enqueue() and push() are not list methods; insert(0, element) adds at front, not top of stack.
Final Answer:
stack.append(element) -> Option C
Quick Check:
Stack push = append() in Python [OK]
Hint: Use append() to push on Python list stack [OK]
Common Mistakes:
Using enqueue() which is for queues
Using push() which is not a Python list method
Inserting at index 0 which is queue front
3. What will be the output of the following Python code?