add solution of lab 5
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user