Complete the code to create a new node with given data.
struct Node* newNode(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = [1];
temp->prev = NULL;
temp->next = NULL;
return temp;
}The new node's data field should be set to the input parameter data.
Complete the code to insert a node at the front of the dequeue.
void insertFront(struct Dequeue* dq, int data) {
struct Node* temp = newNode(data);
if (dq->front == NULL) {
dq->front = dq->rear = temp;
} else {
temp->next = dq->front;
dq->front->prev = [1];
dq->front = temp;
}
}The previous pointer of the old front node should point to the new node temp.
Fix the error in the code to delete a node from the rear of the dequeue.
void deleteRear(struct Dequeue* dq) {
if (dq->rear == NULL) return;
struct Node* temp = dq->rear;
dq->rear = dq->rear->prev;
if (dq->rear != NULL) {
dq->rear->next = [1];
} else {
dq->front = NULL;
}
free(temp);
}After removing the rear node, the new rear's next pointer should be set to NULL to mark the end of the list.
Fill both blanks to insert a node at the rear of the dequeue correctly.
void insertRear(struct Dequeue* dq, int data) {
struct Node* temp = newNode(data);
if (dq->rear == NULL) {
dq->front = dq->rear = temp;
} else {
dq->rear->next = [1];
temp->prev = [2];
dq->rear = temp;
}
}The current rear's next pointer should point to the new node temp, and the new node's prev pointer should point back to the current rear node.
Fill all three blanks to delete a node from the front of the dequeue correctly.
void deleteFront(struct Dequeue* dq) {
if (dq->front == NULL) return;
struct Node* temp = dq->front;
dq->front = dq->front->[1];
if (dq->front != NULL) {
dq->front->[2] = [3];
} else {
dq->rear = NULL;
}
free(temp);
}After deleting the front node, update dq->front to its next node, then set the new front's prev pointer to NULL to mark the start of the list.
