// Linked list with integer values #include "MyList.h" // The list is implemented by having a node for the head of // the list, which points to the first real node of the list. // The destructor must clean up all nodes that exist in the list template MyList::~MyList() { Node * cur = head.next(); while (cur != NULL) { Node *tmp = cur; cur = cur->next(); delete cur; } } // Add the value to end of list template void MyList::append(T n) { Node * cur = &head; while (cur->next() != NULL) cur = cur->next(); cur->next(new Node(n)); } // Insert the value at specified position template void MyList::insert(T n, int position) { // Find the node this value should be linked after int count = 0; Node * place = &head; while (place->next() != NULL && count < position) { place = place->next(); ++count; } // Create a node for the value and link it here place->next(new Node(n, place->next())); } // Remove all matching values template void MyList::remove(T n) { Node * cur = &head; while ( cur->next() != NULL ) { if ( cur->next()->value() == n ) { Node * tmp = cur->next()->next(); delete cur->next(); cur->next(tmp); } else cur = cur->next(); } } // Display the list template void MyList::display() const { for ( Node * current = head.next(); current != NULL; current = current->next() ) { if (current != head.next()) cout << ' '; cout << current->value(); } cout << endl; }