Bird
Raised Fist0
DSA Pythonprogramming

Push Operation on Stack in DSA Python

Choose your learning style10 modes available
Mental Model
A stack is like a pile where you add new items only on top. Pushing means placing a new item on the top of this pile.
Analogy: Imagine a stack of plates. When you add a new plate, you always put it on the top of the stack.
Top -> null
Dry Run Walkthrough
Input: Start with empty stack, push values 1, then 2, then 3
Goal: Add each new value on top of the stack so the last pushed is on top
Step 1: Push 1 onto empty stack
Top -> [1] -> null
Why: First item becomes the top of the stack
Step 2: Push 2 onto stack
Top -> [2] -> [1] -> null
Why: New item goes on top, previous top moves down
Step 3: Push 3 onto stack
Top -> [3] -> [2] -> [1] -> null
Why: Keep adding new items on top, stack grows upwards
Result:
Top -> 3 -> 2 -> 1 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class Stack:
    def __init__(self):
        self.top = None

    def push(self, value):
        new_node = Node(value)  # create new node with value
        new_node.next = self.top  # new node points to current top
        self.top = new_node  # update top to new node

    def __str__(self):
        result = []
        current = self.top
        while current:
            result.append(str(current.value))
            current = current.next
        return ' -> '.join(result) + ' -> null'

# Driver code
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack)
new_node = Node(value) # create new node with value
create a new node to hold the pushed value
new_node.next = self.top # new node points to current top
link new node to the current top node to keep stack chain
self.top = new_node # update top to new node
update stack top pointer to new node, making it the new top
OutputSuccess
3 -> 2 -> 1 -> null
Complexity Analysis
Time: O(1) because push adds one item on top without traversing
Space: O(1) extra space for the new node created during push
vs Alternative: Compared to array-based stack, linked list push avoids resizing and copying, always O(1)
Edge Cases
Empty stack
Push sets top to new node correctly
DSA Python
self.top = new_node  # update top to new node
When to Use This Pattern
When you need to add an item to a stack quickly and always on top, use the push operation to maintain last-in-first-out order.
Common Mistakes
Mistake: Forgetting to link the new node's next to the current top
Fix: Always set new_node.next = self.top before updating self.top
Mistake: Updating top before linking new node's next
Fix: Link new_node.next first, then update self.top to new_node
Summary
Push adds a new item on top of the stack.
Use push when you want to insert elements in last-in-first-out order.
The key is to link the new node to the current top before updating the top pointer.

Practice

(1/5)
1. What does the push operation do in a stack?
easy
A. Checks if the stack is empty
B. Removes the top element from the stack
C. Returns the bottom element of the stack
D. Adds an element to the top of the stack

Solution

  1. Step 1: Understand stack behavior

    A stack follows Last In First Out (LIFO) principle, so new elements are added on top.
  2. Step 2: Define push operation

    Push means adding a new element on the top of the stack.
  3. Final Answer:

    Adds an element to the top of the stack -> Option D
  4. Quick Check:

    Push = Add top element [OK]
Hint: Push always adds element on top of stack [OK]
Common Mistakes:
  • Confusing push with pop operation
  • Thinking push removes elements
  • Mixing stack with queue operations
2. Which of the following is the correct syntax to push an element 5 into a stack named stack implemented as a Python list?
easy
A. stack.append(5)
B. stack.push(5)
C. stack.add(5)
D. stack.insert(0, 5)

Solution

  1. Step 1: Recall Python list methods for stack

    Python lists use append() to add elements at the end, which acts as top of stack.
  2. Step 2: Check each option

    push() and add() are not list methods; insert(0, 5) adds at bottom, not top.
  3. Final Answer:

    stack.append(5) -> Option A
  4. Quick Check:

    Use append() to push in Python list stack [OK]
Hint: Use append() to push in Python list stack [OK]
Common Mistakes:
  • Using push() which is not a Python list method
  • Using insert(0, value) which adds at bottom
  • Confusing add() with append()
3. What will be the content of the stack after executing the following code?
stack = []
stack.append(10)
stack.append(20)
stack.append(30)
medium
A. [20, 10, 30]
B. [30, 20, 10]
C. [10, 20, 30]
D. [10, 30, 20]

Solution

  1. Step 1: Trace each append operation

    Start with empty list. Append 10 -> [10], append 20 -> [10, 20], append 30 -> [10, 20, 30].
  2. Step 2: Understand stack order

    Elements are added at the end; top is last element (30).
  3. Final Answer:

    [10, 20, 30] -> Option C
  4. Quick Check:

    Stack after pushes = [10, 20, 30] [OK]
Hint: Append adds at end; last element is top [OK]
Common Mistakes:
  • Reversing the order of elements
  • Confusing stack top with bottom
  • Assuming append adds at start
4. Identify the error in the following code that tries to push an element 15 into a stack implemented as a list:
stack = [5, 10]
stack.push(15)
medium
A. Missing import statement for stack
B. Using push() method which does not exist for list
C. Stack should be initialized as a tuple
D. Using append() instead of push()

Solution

  1. Step 1: Check list methods for adding elements

    Python lists do not have a push() method; they use append().
  2. Step 2: Analyze the code error

    Calling stack.push(15) will cause an AttributeError because push is undefined.
  3. Final Answer:

    Using push() method which does not exist for list -> Option B
  4. Quick Check:

    List has append(), not push() [OK]
Hint: Use append() for list push, not push() [OK]
Common Mistakes:
  • Assuming push() exists for Python lists
  • Thinking stack must be tuple
  • Ignoring AttributeError on push()
5. Given a stack implemented as a list, how would you push multiple elements [1, 2, 3] onto the stack so that 3 is on top after all pushes?
hard
A. Use a loop to append elements in order: 1, then 2, then 3
B. Use a loop to append elements in reverse order: 3, then 2, then 1
C. Use extend() method with reversed([1, 2, 3])
D. Insert elements at index 0 in order: 1, then 2, then 3

Solution

  1. Step 1: Understand stack push order

    To have 3 on top, push 1 first, then 2, then 3 so 3 is last pushed.
  2. Step 2: Analyze each option

    Loop append in order 1,2,3 puts 3 on top. Reverse order puts 1 on top. extend(reversed([1,2,3])) adds 3,2,1 so 1 on top. Insert at index 0 in order ends with [3,2,1], 1 on top.
  3. Final Answer:

    Use a loop to append elements in order: 1, then 2, then 3 -> Option A
  4. Quick Check:

    Push in order to get last element on top [OK]
Hint: Push elements in order to have last on top [OK]
Common Mistakes:
  • Pushing in reverse order causing wrong top
  • Using insert(0, val) which adds at bottom
  • Using extend(reversed([1, 2, 3])) which puts wrong order on top
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