#include #include #include using namespace std; template struct Node{ T data; Node *next; Node():data(0),next(0){} Node(T d, Node* n=0):data(d),next(n){} }; template class slink{ private: Node* head; public: slink():head(0){} ~slink(){ Node *c = head; while(c != 0){ Node *t = c; c = c->next; delete t; } } void wipeOut(X d){ if(head==0){ cout << "attempt to wipeOut on empty list" << endl; return; } // search for deletion point Node *c = head; Node *p = 0; while(c != 0 && c->data != d){ p = c; c = c->next; } if(c == 0){ cout << d << " not in list "<next; delete c; } // middle delete else if(p!=0&&c!=0){ p->next = c->next; delete c; } else if(c==0){ p->next = 0; delete c; } } void add(X d){ if(head==0){ head = new Node(d); return; } // search for insertion point Node *c = head; Node *p = 0; while(c != 0 && c->data < d){ p = c; c = c->next; } // beginning insert if(p == 0){ head = new Node(d,head); return; } // end insert else if(c==0){ p->next = new Node(d); } // middle insert else{ p->next = new Node(d,c); } } template friend ostream& operator <<(ostream & o, slink & s); }; template ostream& operator <<(ostream & o, slink & s){ Node *c = s.head; while(c!=0){ o << c->data << endl; c = c->next; } return o; } void main(){ slink s; s.add(3); s.add(1); s.add(17); s.add(5); cout << s << endl; cout << endl << "starting to wipeOut "< ss; ss.add("tau"); ss.add("gamma"); ss.add("mu"); ss.add("chi"); cout << ss << endl; list ld; ld.push_back(3.14159); ld.push_back(2.7182); ld.sort(); ostream_iterator out(cout, "\n"); copy(ld.begin(), ld.end(), out); }