Bird
Raised Fist0
Data Structures Theoryknowledge~6 mins

Stack operations (push, pop, peek) in Data Structures Theory - Full Explanation

Choose your learning style10 modes available
Introduction
Imagine you have a stack of plates. You can only add a new plate on top or take the top plate off. This problem of managing items in a last-in, first-out order is solved by stack operations.
Explanation
Push Operation
Push means adding a new item to the top of the stack. This operation increases the stack size by one and places the new item above all others. It is like placing a new plate on the top of a pile.
Push adds an item to the top of the stack.
Pop Operation
Pop removes the item from the top of the stack and returns it. This decreases the stack size by one. You can only pop the most recently added item, following the last-in, first-out rule.
Pop removes and returns the top item from the stack.
Peek Operation
Peek lets you look at the top item of the stack without removing it. This helps to check what is on top without changing the stack. It is like glancing at the top plate without taking it off.
Peek shows the top item without removing it.
Real World Analogy

Think of a stack of books on a table. You add a new book on top (push), take the top book off to read (pop), or just look at the title of the top book without moving it (peek).

Push Operation → Adding a new book on top of the stack
Pop Operation → Removing the top book to read it
Peek Operation → Looking at the title of the top book without removing it
Diagram
Diagram
Stack (top)
┌─────┐
│  3  │  <-- Peek shows this
├─────┤
│  2  │
├─────┤
│  1  │
└─────┘

Operations:
Push: Add new item on top
Pop: Remove top item
Peek: View top item
This diagram shows a stack with three items and illustrates push, pop, and peek operations on the top item.
Key Facts
Stack → A collection that follows last-in, first-out order.
Push → Operation to add an item to the top of the stack.
Pop → Operation to remove and return the top item from the stack.
Peek → Operation to view the top item without removing it.
Last-In, First-Out (LIFO) → The order where the last added item is the first to be removed.
Code Example
Data Structures Theory
class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        if not self.items:
            return None
        return self.items.pop()

    def peek(self):
        if not self.items:
            return None
        return self.items[-1]

stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.peek())  # Should print 3
print(stack.pop())   # Should print 3
print(stack.peek())  # Should print 2
OutputSuccess
Common Confusions
Thinking pop removes any item from the stack, not just the top one.
Thinking pop removes any item from the stack, not just the top one. Pop always removes the most recently added item at the top, following the LIFO rule.
Believing peek removes the item it shows.
Believing peek removes the item it shows. Peek only shows the top item without changing the stack.
Summary
Stack operations manage items in a last-in, first-out order.
Push adds an item on top, pop removes the top item, and peek shows the top item without removing it.
These operations help organize data where only the most recent item is accessible.

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

  1. Step 1: Understand the pop operation

    The pop operation removes the last item added to the stack, which is the top item.
  2. Step 2: Confirm the behavior of pop

    It returns this top item and removes it from the stack, following last-in, first-out order.
  3. Final Answer:

    Removes and returns the top item from the stack -> Option C
  4. 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

  1. Step 1: Identify the push syntax

    Push adds an item to the top of the stack, usually called with push(item).
  2. Step 2: Match the correct method call

    Only s.push(5) correctly adds 5 to the stack.
  3. Final Answer:

    s.push(5) -> Option B
  4. 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

  1. 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].
  2. Step 2: Determine the top item with peek

    Peek returns the top item without removing it, so top = 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    Last pushed after pop is 30 [OK]
Hint: Track stack after each operation carefully [OK]
Common Mistakes:
  • Forgetting pop removes top
  • Assuming peek removes item
  • Mixing order of operations
4. Consider this code snippet for a stack:
stack = []
stack.push(1)
stack.push(2)
item = stack.pop()

What is the error in this code?
medium
A. stack is not initialized correctly
B. pop() does not return the top item
C. stack.pop() removes the bottom item, not top
D. push() is not a method of list in Python

Solution

  1. Step 1: Check stack initialization

    The stack is initialized as an empty list, which is correct in Python.
  2. Step 2: Verify method usage on list

    Python lists use append() to add items, not push(). Using push() causes an error.
  3. Final Answer:

    push() is not a method of list in Python -> Option D
  4. Quick Check:

    Python list uses append(), not push() [OK]
Hint: Remember Python list uses append(), not push() [OK]
Common Mistakes:
  • Assuming push() works on Python lists
  • Confusing pop() behavior
  • Thinking stack initialization is wrong
5. You want to reverse a list [1, 2, 3] using a stack. Which sequence of stack operations correctly reverses the list?
hard
A. Push all items in order, then pop all items to get reversed list
B. Pop all items first, then push them back in order
C. Peek all items, then push them again
D. Push items in reverse order, then pop all items

Solution

  1. Step 1: Understand stack reversal logic

    Pushing items in original order stores them with last item on top. Popping all items removes them in reverse order.
  2. Step 2: Apply push then pop to reverse list

    Push 1, then 2, then 3. Popping returns 3, 2, 1, which is the reversed list.
  3. Final Answer:

    Push all items in order, then pop all items to get reversed list -> Option A
  4. Quick Check:

    Push then pop reverses order [OK]
Hint: Push in order, pop all to reverse list [OK]
Common Mistakes:
  • Pushing in reverse order first
  • Using peek to reverse
  • Popping before pushing
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