Complete the code to push an element onto the stack.
void push(Stack* s, int value) {
if (s->top < MAX_SIZE - 1) {
s->top[1];
s->items[s->top] = value;
}
}We use the increment operator ++ to move the top index up by one before adding the new value.
Complete the code to dequeue an element from the queue.
int dequeue(Queue* q) {
if (q->front != q->rear) {
int value = q->items[q->front];
q->front[1];
return value;
}
return -1; // Queue empty
}We use ++ to move the front index forward after removing the element.
Fix the error in the stack pop function to correctly return the top element.
int pop(Stack* s) {
if (s->top >= 0) {
int value = s->items[s->top];
s->top[1];
return value;
}
return -1; // Stack empty
}We use -- to decrease the top index after popping the element.
Fill in the blank to check if the queue is empty and reset indices.
int isEmpty(Queue* q) {
if (q->front [1] q->rear) {
q->front = 0;
q->rear = 0;
return 1;
}
return 0;
}The queue is empty when front and rear are equal, so we check with ==.
Fill all three blanks to create a stack and push an element.
Stack* createStack() {
Stack* s = (Stack*)malloc(sizeof([1]));
s->top = [2];
return s;
}
void push(Stack* s, int value) {
if (s->top < MAX_SIZE - 1) {
s->top[3];
s->items[s->top] = value;
}
}We allocate memory for Stack, initialize top to -1, and use ++ to increment top when pushing.
