π Scenario: Imagine you have a stack of books on your desk. You can only add a book on top, remove the top book, or look at the top book without removing it. This is how a stack data structure works in computer science.
π― Goal: You will build a simple stack using a list and perform the basic stack operations: push (add a book), pop (remove the top book), and peek (see the top book).
π What You'll Learn
Create a list called stack with initial books
Create a variable called new_book to add to the stack
Write code to push new_book onto stack
Write code to pop the top book from stack and store it in top_book
Write code to peek at the top book of stack and store it in peek_book
π‘ Why This Matters
π Real World
Stacks are used in real life for things like stacking plates, undo features in software, and managing tasks in order.
πΌ Career
Understanding stack operations is important for programming jobs, especially in areas like software development, algorithms, and system design.
Progress0 / 4 steps
1
Create the initial stack
Create a list called stack with these exact books in order: 'Math', 'Science', 'History'.
Data Structures Theory
Hint
Use square brackets [] to create a list and separate items with commas.
2
Add a new book to the stack
Create a variable called new_book and set it to the string 'English'.
Data Structures Theory
Hint
Assign the string 'English' to the variable new_book.
3
Push the new book onto the stack
Use the append() method on stack to add the new_book to the top of the stack.
Data Structures Theory
Hint
Use stack.append(new_book) to add the book on top.
4
Pop and peek the stack
Remove the top book from stack using pop() and store it in top_book. Then, get the current top book without removing it by accessing the last item of stack and store it in peek_book.
Data Structures Theory
Hint
Use pop() to remove the last item and stack[-1] to peek at the last item.
Practice
(1/5)
1. What does the pop operation do in a stack?
easy
A. Shows the bottom item without removing it
B. Adds a new item to the bottom of the stack
C. Removes and returns the top item from the stack
D. Clears all items from the stack
Solution
Step 1: Understand the pop operation
The pop operation removes the last item added to the stack, which is the top item.
Step 2: Confirm the behavior of pop
It returns this top item and removes it from the stack, following last-in, first-out order.
Final Answer:
Removes and returns the top item from the stack -> Option C
Quick Check:
pop = remove top item [OK]
Hint: Pop always removes the last item added [OK]
Common Mistakes:
Thinking pop adds an item
Confusing pop with peek
Assuming pop removes bottom item
2. Which of the following is the correct way to add an item 5 to a stack named s using the push operation?
easy
A. s.pop(5)
B. s.push(5)
C. s.peek(5)
D. s.add(5)
Solution
Step 1: Identify the push syntax
Push adds an item to the top of the stack, usually called with push(item).
Step 2: Match the correct method call
Only s.push(5) correctly adds 5 to the stack.
Final Answer:
s.push(5) -> Option B
Quick Check:
push = add item [OK]
Hint: Push means add, so look for push() method [OK]
Common Mistakes:
Using pop instead of push
Using peek to add items
Using add which is not standard for stacks
3. Given the following stack operations:
s.push(10) s.push(20) s.pop() s.push(30) top = s.peek()
What is the value of top after these operations?
medium
A. 30
B. 20
C. None
D. 10
Solution
Step 1: Trace the stack changes step-by-step
After push(10), stack = [10]. After push(20), stack = [10, 20]. After pop(), top 20 is removed, stack = [10]. After push(30), stack = [10, 30].
Step 2: Determine the top item with peek
Peek returns the top item without removing it, so top = 30.
Final Answer:
30 -> Option A
Quick Check:
Last pushed after pop is 30 [OK]
Hint: Track stack after each operation carefully [OK]