diff --git a/labs/06_lists_iterators/checkpoint1.cpp b/labs/06_lists_iterators/checkpoint1.cpp new file mode 100644 index 0000000..e7b9ea4 --- /dev/null +++ b/labs/06_lists_iterators/checkpoint1.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + + +template +void print(std::vector &data, const std::string &label) { + std::cout << label << " "; + for (int i = 0; i < data.size(); i++) { + std::cout << " " << data[i]; + } + std::cout << std::endl; +} + + +template +void reverse(std::vector &data) { + + // FILL IN THIS FUNCTION + +} + + +int main() { + std::vector data; + data.push_back(1); + data.push_back(2); + data.push_back(3); + data.push_back(4); + data.push_back(5); + data.push_back(6); + data.push_back(7); + + print(data,"before:"); + reverse(data); + print(data,"after: "); + + std::vector data2; + data2.push_back("apple"); + data2.push_back("banana"); + data2.push_back("carrot"); + data2.push_back("date"); + + print(data2,"before:"); + reverse(data2); + print(data2,"after: "); +} diff --git a/labs/06_lists_iterators/checkpoint3.cpp b/labs/06_lists_iterators/checkpoint3.cpp new file mode 100644 index 0000000..d0d12b7 --- /dev/null +++ b/labs/06_lists_iterators/checkpoint3.cpp @@ -0,0 +1,64 @@ +#include +#include + + +// Note: It's ok that all the member variables are public for this +// tiny class. We'll build up to a more robust and complete linked +// list implementation in lecture 11. +template +class Node { +public: + T value; + Node *ptr; +}; + + + +template +void print(Node *data, const std::string &label) { + std::cout << label; + Node *tmp = data; + while (tmp != NULL) { + std::cout << " " << tmp->value; + tmp = tmp->ptr; + } + std::cout << std::endl; +} + + + +template +void reverse(Node* &input) { + + // FILL IN THIS FUNCTION + +} + + + + +int main() { + + // manually create a linked list of notes with 4 elements + Node* my_list = new Node; + my_list->value = 1; + my_list->ptr = new Node; + my_list->ptr->value = 2; + my_list->ptr->ptr = new Node; + my_list->ptr->ptr->value = 3; + my_list->ptr->ptr->ptr = new Node; + my_list->ptr->ptr->ptr->value = 4; + my_list->ptr->ptr->ptr->ptr = NULL; + + + print(my_list,"my_list before"); + reverse(my_list); + print(my_list,"my_list after "); + + + // Note: We are not deleting any of the Nodes we created... so this + // program has memory leaks! More on this in lecture 11. + +} + +// ===========================================================================