diff --git a/labs/07_list_implementation/README.md b/labs/07_list_implementation/README.md index 2835680..8a229aa 100644 --- a/labs/07_list_implementation/README.md +++ b/labs/07_list_implementation/README.md @@ -20,6 +20,20 @@ of the push_front, pop_front, and pop_back functions. **To complete this checkpoint**, show a TA the implementation and memory debugger output before and after writing destroy_list. +## Checkpoint 2: +*estimate: 20-30 minutes* + +The PushBack() and the PrintList() function are used in [checkpoint2.cpp](checkpoint2.cpp), but their definitions are missing, please complete these two functions and make sure the program runs and produces the following output. + +```console +$ g++ checkpoint2.cpp +$ ./a.out +Linked List of NodeA nodes: 1 -> 2 -> 3 -> 4 -> 5 -> nullptr +Linked List of NodeB nodes: 1 -> 1.41421 -> 1.73205 -> 2 -> 2.23607 -> nullptr +``` + +**Note**: Hardcoding the PrintList() function to do nothing but just print the above two messages is strictly prohibited. Students doing that will be evicted from the lab room. + ## Checkpoint 3: *estimate: TBD* diff --git a/labs/07_list_implementation/checkpoint2.cpp b/labs/07_list_implementation/checkpoint2.cpp new file mode 100644 index 0000000..e3631d8 --- /dev/null +++ b/labs/07_list_implementation/checkpoint2.cpp @@ -0,0 +1,82 @@ +/* This program attempts to create two linked list, one linked list consists of nodes of class NodeA type, + * the other linked list consists of nodes of class NodeB type, but we would like to use the same + * PushBack() function to append nodes to the end of these two linked lists, and use the same PrintList() function + * to print the elements in both lists, and thus we need to make the PushBack() and the PrintList() templated functions. + */ + +#include +#include + +class NodeA { +public: + int data; + NodeA* next; + NodeA* prev; +}; + +class NodeB{ +public: + double data; + NodeB* next; + NodeB* prev; +}; + +int main() { + // Part 1: test NodeA class. + // Initialize an empty linked list, consisting of NodeA nodes. + NodeA* headA = nullptr; + + // Create nodes and add them to the end of the list using PushBack + for (int i = 1; i <= 5; ++i) { + NodeA* newNode = new NodeA; + // data of NodeA is an int type. + newNode->data = i; + newNode->next = nullptr; + newNode->prev = nullptr; + + // Add the node to the end of the list + PushBack(headA, newNode); + } + + // Print the linked list to verify the nodes + std::cout << "Linked List of NodeA nodes: "; + PrintList(headA); + + // Clean up memory (free nodes) + NodeA* currentA = headA; + while (currentA != nullptr) { + NodeA* next = currentA->next; + delete currentA; + currentA = next; + } + + // Part 2: test NodeB class. + // Initialize an empty linked list, consisting of NodeB nodes. + NodeB* headB = nullptr; + + // Create nodes and add them to the end of the list using PushBack + for (int i = 1; i <= 5; ++i) { + NodeB* newNode = new NodeB; + // data of NodeA is a double type. + newNode->data = (double)sqrt(i); + newNode->next = nullptr; + newNode->prev = nullptr; + + // Add the node to the end of the list + PushBack(headB, newNode); + } + + // Print the linked list to verify the nodes + std::cout << "Linked List of NodeB nodes: "; + PrintList(headB); + + // Clean up memory (free nodes) + NodeB* currentB = headB; + while (currentB != nullptr) { + NodeB* next = currentB->next; + delete currentB; + currentB = next; + } + + return 0; +}