[   ^ to index...   |   <-- previous   |   next -->   ]

Fixing the LinkedList class

Now we know that the LinkedList class that we wrote last time is broken (even apart from the fact that I gave you incorrect code). How can we fix it? Well, let's add the needed members (copy constructor, assignment operator, destructor) for the list class. It's easiest if we do this in terms of two private helpers, destroy() and copy() (can you guess why?)

struct ListNode { int value; ListNode * next; ListNode() { value = 0; next = NULL; } ListNode(int value, ListNode * next) { this->value = value; this->next = next; } }; class LinkedList { public: LinkedList() { head = NULL; } LinkedList(LinkedList &other); ~LinkedList(); LinkedList& operator=(LinkedList &other); // ... etc. private: ListNode * head; void cleanup(); void copy(LinkedList &other); }; LinkedList::LinkedList(LinkedList &other) { } LinkedList::~LinkedList() { } LinkedList& LinkedList::operator=( LinkedList& other ) { }

Last modified: Mon Jul 17 23:26:59 PDT 2000