// L-11 MCS 360 Fri 17 Sep 2010 : mcs360_list.tc /* This file defines the member function of the templated class List in the namespace mcs360_list. */ #include namespace mcs360_list { template List::List() { first = NULL; last = NULL; } template List::List(T item) { first = new Node(item); last = first; } template void List::append(T item) { if(first == NULL) { first = new Node(item); last = first; } else if(first == last) { first->next = new Node(item); last = first->next; } else { last->next = new Node(item); last = last->next; } } template void List::write() { Node *ptr = first; while(ptr != NULL) { std::cout << "->" << ptr->data; ptr = ptr->next; } } template bool List::member(T item) { Node *ptr = first; while(ptr != NULL) { if(ptr->data == item) return true; ptr = ptr->next; } return false; } template void List::erase(T item) { if(first != NULL) { Node *current = first; if(current->data == item) first = first->next; else { Node *previous = first; current = first->next; if(current != NULL) { while((current->data != item) && (current->next != NULL)) { previous = current; current = current->next; } if(current->data == item) { std::cout << "removing " << item << std::endl; previous->next = current->next; delete current; } if(previous->next == NULL) last = previous; } } } } }