Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new node at the end of the queue.
DSA C++
void enqueue(int value) {
Node* newNode = new Node(value);
if (rear == nullptr) {
front = rear = newNode;
return;
}
rear->[1] = newNode;
rear = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' pointer.
Not updating the rear pointer after adding the new node.
✗ Incorrect
The 'next' pointer of the current rear node should point to the new node to add it at the end.
2fill in blank
mediumComplete the code to check if the queue is empty before enqueueing.
DSA C++
void enqueue(int value) {
Node* newNode = new Node(value);
if ([1] == nullptr) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'newNode' instead of queue pointers.
Checking 'rear' which might not be updated yet.
✗ Incorrect
Checking if 'front' is nullptr tells us if the queue is empty.
3fill in blank
hardFix the error in the enqueue function to correctly update the rear pointer.
DSA C++
void enqueue(int value) {
Node* newNode = new Node(value);
if (front == nullptr) {
front = rear = newNode;
return;
}
rear->next = newNode;
[1] = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Updating front instead of rear.
Not updating any pointer after adding the node.
✗ Incorrect
The rear pointer must be updated to the new node after adding it.
4fill in blank
hardFill both blanks to complete the enqueue function that handles empty and non-empty queues.
DSA C++
void enqueue(int value) {
Node* newNode = new Node(value);
if ([1] == nullptr) {
[2] = newNode;
rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using rear in the condition instead of front.
Not setting front when queue is empty.
✗ Incorrect
Check if front is nullptr to detect empty queue, then assign front to newNode.
5fill in blank
hardFill all three blanks to implement enqueue that updates pointers correctly and handles empty queue.
DSA C++
void enqueue(int value) {
Node* newNode = new Node(value);
if ([1] == nullptr) {
[2] = newNode;
rear = newNode;
return;
}
rear->[3] = newNode;
rear = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using rear instead of front in the condition.
Using 'prev' instead of 'next' pointer for linking.
✗ Incorrect
Check if front is nullptr, set front to newNode, and link rear->next to newNode.
