/////// File : List.C /////// #include #include "List.h" List::~List() { free(); } void List::free() // private method { ListCell* n, *p = head; while (p) { n = p->next; delete p; p=n; } } int List::shorten(int n) { while (n-- && head) { ListCell* tmp=head; head = head->next; delete(tmp); } return(0); } int List::remove(char c) { ListCell *tmp, *p = head; int count = 0; if ( p == NULL ) return(count); while (p->next) // treat all but head cell { if ((p->next)->item == c) { count++; tmp = p->next; p->next = tmp->next; delete(tmp); // free up store } else p = p->next; } if( head->item == c ) // treat head cell { tmp = head; head = head->next; delete(tmp); count++; } return(count); // number of items removed } int List::put_on(char c) { ListCell* tmp = new ListCell(c,head); if ( tmp ) { head = tmp; return(0); } else return(-1); // failed } // insert after entry e int List::insert(char c, ListCell* e) { ListCell* tmp = new ListCell(c,e->next); if ( tmp ) { e->next = tmp; return(0); } else return(-1); // failed } ListCell* List::last() { ListCell* p = head; while( p && p->next ) p = p->next; return(p); } ListCell* List::find(char c) { for( ListCell* p = head; p ; p = p->next ) if( p->item == c ) return(p); return(NULL); // c not on list } int List::substitute(char r, char s) { ListCell* p = find(s); if( p == NULL ) return(-1); // s not on list p->item = r; return(0); } // display from p to end void List::display(ListCell* p) { cout << "("; while ( p ) { cout << p->item; if ( p = p->next ) cout << " "; } cout << ")\n"; }