Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node for enqueue operation.
DSA C++
Node* newNode = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of the value for the new node.
Using head or tail pointer instead of the value.
✗ Incorrect
We create a new node with the given value to enqueue it.
2fill in blank
mediumComplete the code to check if the queue is empty before enqueue.
DSA C++
if (tail == [1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if tail equals head instead of NULL.
Checking if tail equals newNode which is not yet assigned.
✗ Incorrect
If tail is NULL, the queue is empty and we need to set head and tail to newNode.
3fill in blank
hardFix the error in linking the new node to the queue.
DSA C++
tail->next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning tail->next to head or value instead of newNode.
Assigning tail->next to NULL which breaks the chain.
✗ Incorrect
The current tail's next pointer should point to the new node being enqueued.
4fill in blank
hardFill both blanks to update the tail pointer after enqueue.
DSA C++
tail = [1]; [2] = NULL;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting tail to head instead of newNode.
Not setting newNode->next to NULL.
✗ Incorrect
After enqueue, tail should point to newNode and newNode's next should be NULL.
5fill in blank
hardFill all three blanks to complete the enqueue function using linked list.
DSA C++
void enqueue(int value) {
Node* newNode = new Node(value);
if (tail == [1]) {
head = newNode;
tail = newNode;
} else {
tail->next = [2];
[3] = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head instead of NULL in the if condition.
Assigning tail->next to head instead of newNode.
Not updating tail pointer after linking newNode.
✗ Incorrect
If queue is empty (tail == NULL), set head and tail to newNode. Otherwise, link tail->next to newNode and update tail to newNode.
