// first version of a linked list class (doesn't pass valgrind test) #include #include #include using namespace std; struct list_node { list_node(int init_data = 0, list_node * link = nullptr) { data = init_data; next = link; } int data; list_node * next; }; class linked_list { public: linked_list() { front = nullptr; back = nullptr; count = 0; } void add(int value) { if (count == 0) { front = new list_node(value); back = front; count = 1; } else { back->next = new list_node(value); count++; back = back->next; } } int size() const { return count; } string to_string() const { ostringstream out; out << "["; if (count > 0) { out << front->data; list_node * current = front->next; while (current != nullptr) { out << ", " << current->data; current = current->next; } } out << "]"; return out.str(); } private: list_node * front; list_node * back; int count; }; int main() { linked_list list; list.add(18); list.add(7); list.add(5); cout << "list = " << list.to_string() << " size = " << list.size() << endl; return 0; }