Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the node structure for the linked list queue.
DSA C++
struct Node {
int data;
Node* [1];
};Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' for the pointer.
Using 'head' or 'tail' as pointer names inside the node.
✗ Incorrect
The 'next' pointer links to the next node in the queue.
2fill in blank
mediumComplete the code to initialize the queue's front and rear pointers to nullptr in the constructor.
DSA C++
class Queue { private: Node* front; Node* rear; public: Queue() { front = [1]; rear = [1]; } };
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL or 0 instead of nullptr.
Initializing rear to front instead of nullptr.
✗ Incorrect
Both front and rear pointers should be initialized to nullptr to indicate an empty queue.
3fill in blank
hardFix the error in the enqueue function to correctly add a new node at the rear of the queue.
DSA C++
void enqueue(int value) {
Node* temp = new Node();
temp->data = value;
temp->next = nullptr;
if (rear == nullptr) {
front = temp;
rear = temp;
} else {
rear->[1] = temp;
rear = temp;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using rear->prev instead of rear->next.
Assigning rear->data instead of rear->next.
✗ Incorrect
The rear node's 'next' pointer should point to the new node to link it at the end.
4fill in blank
hardFill both blanks to correctly implement the dequeue function that removes the front node.
DSA C++
int dequeue() {
if (front == nullptr) {
return -1; // Queue is empty
}
Node* temp = front;
int value = temp->data;
front = front->[1];
if (front == nullptr) {
rear = [2];
}
delete temp;
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting rear to front instead of nullptr when queue is empty.
Not updating front to front->next.
✗ Incorrect
After removing the front node, front moves to front->next. If front becomes nullptr, rear must also be nullptr.
5fill in blank
hardFill all three blanks to implement a function that checks if the queue is empty.
DSA C++
bool isEmpty() {
return [1] == [2];
}
// Usage example:
// if (queue.[3]()) { /* queue is empty */ }Attempts:
3 left
💡 Hint
Common Mistakes
Comparing rear to nullptr instead of front.
Using wrong function name like empty instead of isEmpty.
✗ Incorrect
The queue is empty if front equals nullptr. The function name is isEmpty.
