Files
CSCI-1200/labs/06_lists_iterators/checkpoint3.cpp
2024-02-13 13:06:08 -05:00

64 lines
1.1 KiB
C++

#include <iostream>
#include <string>
// Note: It's ok that all the member variables are public for this
// tiny class.
template <class T>
class Node {
public:
T value;
Node<T> *ptr;
};
template <class T>
void print(Node<T> *data, const std::string &label) {
std::cout << label;
Node<T> *tmp = data;
while (tmp != NULL) {
std::cout << " " << tmp->value;
tmp = tmp->ptr;
}
std::cout << std::endl;
}
template <class T>
void reverse(Node<T>* &input) {
// FILL IN THIS FUNCTION
}
int main() {
// manually create a linked list of nodes with 4 elements
Node<int>* my_list = new Node<int>;
my_list->value = 1;
my_list->ptr = new Node<int>;
my_list->ptr->value = 2;
my_list->ptr->ptr = new Node<int>;
my_list->ptr->ptr->value = 3;
my_list->ptr->ptr->ptr = new Node<int>;
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!
}
// ===========================================================================