Complete the code to remove an element from the front of the dequeue.
int dequeueFront() {
if (front == -1) {
return -1; // Dequeue is empty
}
int data = arr[front];
if (front == rear) {
front = rear = -1;
} else {
front = [1];
}
return data;
}When removing from the front, we move the front pointer forward by 1.
Complete the code to remove an element from the rear of the dequeue.
int dequeueRear() {
if (rear == -1) {
return -1; // Dequeue is empty
}
int data = arr[rear];
if (front == rear) {
front = rear = -1;
} else {
rear = [1];
}
return data;
}When removing from the rear, we move the rear pointer backward by 1.
Fix the error in the condition that checks if the dequeue is empty.
int isEmpty() {
if ([1]) {
return 1;
}
return 0;
}The dequeue is empty when both front and rear are -1.
Fill both blanks to correctly update the front pointer in a circular dequeue after removing from front.
void dequeueFrontCircular() {
if (front == -1) {
return; // Empty dequeue
}
if (front == rear) {
front = rear = -1;
} else {
front = (front [1] 1) [2] size;
}
}In circular dequeue, front moves forward by 1 and wraps around using modulo operator.
Fill all three blanks to correctly update the rear pointer in a circular dequeue after removing from rear.
void dequeueRearCircular() {
if (rear == -1) {
return; // Empty dequeue
}
if (front == rear) {
front = rear = -1;
} else {
rear = (rear [1] 1 + [2]) [3] size;
}
}To move rear backward in circular dequeue, subtract 1, add size to avoid negative, then modulo size.
