Complete the code to push an element onto an array-based stack.
def push(stack, element): stack.[1](element)
We use append to add an element at the end of the list, simulating push in an array stack.
Complete the code to pop an element from a linked list based stack.
def pop(head): if head is None: return None, None popped_value = head.data head = head.[1] return popped_value, head
In a linked list stack, head.next points to the next node after popping the top.
Fix the error in the array stack size check before pushing.
def push(stack, element, max_size): if len(stack) [1] max_size: return "Stack Overflow" stack.append(element) return stack
We check if the stack length is greater than or equal to max size to prevent overflow.
Fill both blanks to check if a linked list stack is empty and to push a new node.
def push(head, element): new_node = Node(element) if head is [1]: head = new_node else: new_node.[2] = head head = new_node return head
We check if head is None to see if stack is empty. Then set new_node.next to head to link nodes.
Fill all three blanks to create a dictionary showing trade-offs: keys are 'ArrayStack' and 'LinkedListStack', values are their main advantage.
trade_offs = {
'[1]': 'Fast access',
'[2]': 'Dynamic size',
}
print(trade_offs['[3]'])ArrayStack has fast access due to contiguous memory. LinkedListStack has dynamic size. The print accesses ArrayStack's advantage.