Complete the code to initialize the top of the stack as empty.
class Stack: def __init__(self): self.top = [1]
The top of an empty stack is set to None to indicate no nodes are present.
Complete the code to create a new node with the given data.
class Node: def __init__(self, data): self.data = data self.next = [1]
The next pointer of a new node is set to None because it does not point to any other node yet.
Fix the error in the push method to correctly add a new node on top of the stack.
def push(self, data): new_node = Node(data) new_node.next = [1] self.top = new_node
The new node's next should point to the current top node before updating top to the new node.
Fill both blanks to correctly remove the top node and update the stack.
def pop(self): if self.top is None: return None popped_node = self.top self.top = [1] popped_node.next = [2] return popped_node.data
After popping, top moves to the next node, and popped_node.next is set to None to detach it.
Fill all three blanks to check if the stack is empty and return the top element's data.
def peek(self): if self.top == [1]: return [2] return self.top.[3]
Peek returns None if stack is empty (top is None), else returns the data of the top node.