Complete the code to push an element onto an array-based stack.
void push(int stack[], int *top, int value, int max_size) {
if (*top < max_size - 1) {
stack[++(*top)] = [1];
}
}The value to push onto the stack is assigned to the next position in the array.
Complete the code to create a new node for a linked list stack push operation.
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* push(Node* top, int value) {
Node* new_node = (Node*)malloc(sizeof(Node));
if (new_node == NULL) return top;
new_node->data = [1];
new_node->next = top;
return new_node;
}The new node's data field should be set to the value being pushed onto the stack.
Fix the error in the array stack pop function to return the popped value correctly.
int pop(int stack[], int *top) {
if (*top == -1) {
return -1; // Stack empty
}
return stack[[1]];
}Use the post-decrement operator to return the value at the current top and then decrement the top index.
Fill both blanks to check if the linked list stack is empty and return the top element's data.
int peek(Node* top) {
if (top == [1]) {
return -1; // Stack empty
}
return top->[2];
}Check if top is NULL to see if stack is empty, then return the data field of the top node.
Fill all three blanks to implement a function that returns the current size of an array stack.
int size(int *top) {
return [1] + [2];
}
// Usage example:
// int current_size = size(&[3]);The size is *top + 1 because top starts at -1 when empty. Pass &top where top is the top index variable.
