Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the Node structure for the stack.
DSA C++
struct Node {
int data;
[1] *next;
};Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong type name for the pointer.
Forgetting the asterisk '*' for pointer declaration.
✗ Incorrect
The pointer to the next node must be of type 'Node*' to link nodes in the stack.
2fill in blank
mediumComplete the code to initialize the top pointer of the stack to empty.
DSA C++
Node* top = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 'NULL' instead of 'nullptr'.
Using integer 0 instead of a pointer null value.
✗ Incorrect
In modern C++, 'nullptr' is used to represent a null pointer safely.
3fill in blank
hardFix the error in the push function to correctly link the new node.
DSA C++
void push(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = [1];
top = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to nullptr loses the rest of the stack.
Setting next to newNode causes self-loop.
✗ Incorrect
The new node's next pointer should point to the current top node before updating top.
4fill in blank
hardFill both blanks to correctly implement the pop function that removes the top element.
DSA C++
int pop() {
if (top == nullptr) {
return -1; // Stack empty
}
Node* temp = top;
int poppedValue = top->data;
top = [1];
delete [2];
return poppedValue;
}Attempts:
3 left
💡 Hint
Common Mistakes
Deleting top before updating it causes dangling pointer.
Not updating top leads to incorrect stack state.
✗ Incorrect
Update top to the next node, then delete the old top stored in temp.
5fill in blank
hardFill all three blanks to implement a function that prints all stack elements from top to bottom.
DSA C++
void printStack() {
Node* current = [1];
while (current != [2]) {
std::cout << current->[3] << " -> ";
current = current->next;
}
std::cout << "null" << std::endl;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for traversal.
Looping incorrectly causing infinite loop or crash.
✗ Incorrect
Start from top, loop until nullptr, print data of each node.
