Complete the code to declare a node structure for the dequeue.
struct Node {
int data;
Node* [1];
Node* next;
};The node in a doubly linked list has a pointer to the previous node called prev.
Complete the code to insert a new node at the front of the dequeue.
void insertFront(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr) {
head->[1] = newNode;
}
head = newNode;
if (tail == nullptr) {
tail = newNode;
}
}When inserting at the front, the old head's previous pointer should point to the new node.
Fix the error in the code to delete a node from the rear of the dequeue.
void deleteRear() {
if (tail == nullptr) return;
Node* temp = tail;
tail = tail->prev;
if (tail != nullptr) {
tail->[1] = nullptr;
} else {
head = nullptr;
}
delete temp;
}After moving tail back, its next pointer should be set to nullptr to mark the end.
Fill both blanks to insert a new node at the rear of the dequeue.
void insertRear(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
newNode->[1] = tail;
if (tail != nullptr) {
tail->[2] = newNode;
}
tail = newNode;
if (head == nullptr) {
head = newNode;
}
}The new node's previous pointer should point to the old tail, and the old tail's next pointer should point to the new node.
Fill all three blanks to delete a node from the front of the dequeue safely.
void deleteFront() {
if (head == nullptr) return;
Node* temp = head;
head = head->[1];
if (head != nullptr) {
head->[2] = nullptr;
} else {
tail = [3];
}
delete temp;
}After moving head forward, set the new head's previous pointer to nullptr. If the list becomes empty, set tail to nullptr.
