Complete the code to add a new node at the front of the dequeue.
addFront(data: number): void {
const newNode = new Node(data);
if (this.front === null) {
this.front = this.rear = newNode;
} else {
newNode.next = this.[1];
this.front.prev = newNode;
this.front = newNode;
}
}When adding at the front, the new node's next should point to the current front node.
Complete the code to remove a node from the rear of the dequeue.
removeRear(): number | null {
if (this.rear === null) return null;
const data = this.rear.data;
this.rear = this.rear.[1];
if (this.rear !== null) {
this.rear.next = null;
} else {
this.front = null;
}
return data;
}To remove from rear, move rear pointer to the previous node.
Fix the error in the code to correctly check if the dequeue is empty.
isEmpty(): boolean {
return this.[1] === null && this.rear === null;
}The dequeue is empty if both front and rear pointers are null.
Fill both blanks to correctly add a node at the rear of the dequeue.
addRear(data: number): void {
const newNode = new Node(data);
if (this.rear === null) {
this.front = this.rear = newNode;
} else {
this.rear.[1] = newNode;
newNode.[2] = this.rear;
this.rear = newNode;
}
}When adding at rear, the current rear's next points to new node, and new node's prev points back to current rear.
Fill all three blanks to correctly remove a node from the front of the dequeue.
removeFront(): number | null {
if (this.front === null) return null;
const data = this.front.data;
this.front = this.front.[1];
if (this.front !== null) {
this.front.[2] = null;
} else {
this.rear = null;
}
// Update rear if list becomes empty
this.rear = this.front === null ? [3] : this.rear;
return data;
}To remove front, move front pointer to next node, set new front's prev to null, and if empty set rear to null.
