Complete the code to print all elements of a doubly linked list in forward direction.
void printForward(Node* head) {
Node* current = head;
while (current != [1]) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "null" << std::endl;
}The loop continues until current becomes nullptr, which means the end of the list.
Complete the code to print all elements of a doubly linked list in backward direction starting from tail.
void printBackward(Node* tail) {
Node* current = tail;
while (current != [1]) {
std::cout << current->data << " -> ";
current = current->prev;
}
std::cout << "null" << std::endl;
}The loop stops when current becomes nullptr, indicating the start of the list has been passed.
Fix the error in the code to traverse forward correctly.
void traverseForward(Node* head) {
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " -> ";
current = current->[1];
}
std::cout << "null" << std::endl;
}To move forward in a doubly linked list, use the next pointer.
Fill both blanks to traverse backward and print data.
void traverseBackward(Node* tail) {
Node* current = tail;
while (current != [1]) {
std::cout << current->[2] << " -> ";
current = current->prev;
}
std::cout << "null" << std::endl;
}The loop ends when current is nullptr. To print the node's value, use the data field.
Fill all three blanks to create a forward traversal that prints uppercase data assuming data is a char.
void traverseForwardUpper(Node* head) {
Node* current = head;
while (current != [1]) {
std::cout << (char)toupper(current->[2]) << " -> ";
current = current->[3];
}
std::cout << "null" << std::endl;
}Traversal ends at nullptr, print the data field in uppercase, move forward using next pointer.
