Complete the code to push an element onto the array-based stack.
public void Push(int value) {
if (top < capacity - 1) {
stackArray[++[1]] = value;
}
}We increment the 'top' index before assigning the new value to the array.
Complete the code to pop an element from the linked list stack.
public int Pop() {
if (head == null) throw new InvalidOperationException("Stack is empty");
int value = head.data;
head = [1];
return value;
}After popping, we move the head pointer to the next node in the linked list.
Fix the error in the array stack resize method to double the capacity.
private void Resize() {
int newCapacity = capacity * [1];
int[] newArray = new int[newCapacity];
Array.Copy(stackArray, newArray, capacity);
stackArray = newArray;
capacity = newCapacity;
}Doubling the capacity means multiplying by 2.
Fill both blanks to create a linked list node and push it onto the stack.
public void Push(int value) {
Node newNode = new Node([1]);
newNode.next = [2];
head = newNode;
}We create a new node with the value and link it to the current head before updating head.
Fill all three blanks to check if the array stack is full and resize if needed before pushing.
public void Push(int value) {
if (top == [1] - 1) {
[2]();
}
stackArray[++[3]] = value;
}We check if 'top' reached 'capacity - 1', call 'Resize()', then increment 'top' before assignment.
