add solution of lab 5

This commit is contained in:
2025-02-26 12:16:28 -05:00
parent 05c92a01ae
commit c20ce7c2c3
8 changed files with 146 additions and 29 deletions

View File

@@ -21,6 +21,36 @@ public:
NodeB* prev;
};
template <typename NodeType>
void PushBack(NodeType*& head, NodeType* newNode) {
// If the list is empty, newNode becomes the head
if (!head) {
head = newNode;
} else {
// Otherwise, find the tail and attach there
NodeType* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
template <typename NodeType>
void PrintList(NodeType* head) {
NodeType* current = head;
while (current != nullptr) {
// For doubles, you might do: std::cout << std::fixed << std::setprecision(5);
std::cout << current->data;
if (current->next != nullptr) {
std::cout << " -> ";
}
current = current->next;
}
std::cout << " -> nullptr" << std::endl;
}
int main() {
// Part 1: test NodeA class.
// Initialize an empty linked list, consisting of NodeA nodes.