# Include <iostream> using namespace STD; Template <Class E, Class K> class sortedchainnode {public: E data; sortedchainnode <E, k> * link ;}; template <Class E, Class K> class sortedchain {public: sortedchain () {First = 0 ;}~ Sortedchain (); bool isempty () const {return first = 0;} int length () const; bool search (const K & K, E & E); sortedchain <e, k> & Delete (const K & K, E & E); // sortedchain <E, k> & insert (const E & E); sortedchain <e, k> & distinctinsert (const E & E); void output (ostream & out) const; private: sortedchainnode <E, k> * First ;}; class badinput {public: badinput () {cout <"bad input! "<Endl ;}}; template <Class E, Class K> sortedchain <E, K> ::~ Sortedchain () {sortedchainnode <E, k> * Next; // points to the next node while (first) {next = first-> link; Delete first; first = next ;}} // reload operator template <Class E, Class K> ostream & operator <(ostream & out, const sortedchain <E, k> & X) {X. output (out); Return out;} // output linked list template <Class E, Class K> void sortedchain <E, K >:: output (ostream & out) const {sortedchainnode <E, k> * Current; For (current = first; current = Current-> link) {out <Current-> data; If (! Current-> link) {out <"<Endl ;}else {out <",";}}} // input the Matching Element with K. // put the result in E. // if no element exists, falsetemplate <Class E, Class K> bool sortedchain <E, k> is returned :: search (const K & K, E & E) {sortedchainnode <E, k> * P = first; // search for elements matching K (; P & P-> data <K; P = p-> link ); // need to overload <operator // verify if it matches K if (P & P-> DATA = K) {e = p-> data; return true ;} return false;} // Delete the element that matches K. // put the deleted element to E. // if no match exists, an exception badinputtemplate <Class E is returned, class K> sortedchain <E, k> & sortedchain <E, k>: delete (const K & K, E & E) {sortedchainnode <e, k> * P = first; sortedchainnode <E, k> * TP = 0; // search for elements matching K for (; P & P-> data <K; TP = P; P = p-> link); // you need to reload the <operator // verify if it matches K if (P & P-> DATA = K) {e = p-> data; If (TP) {TP-> link = p-> link;} else {First = p-> link;} Delete P; return * This;} Throw (badinput ()); // No matched element exists} // insert an element with the same key value as E in the table // otherwise, an exception badinputtemplate <Class E, Class K> sortedchain <e, k> & sortedchain <E, K >:: distinctinsert (const E & E) {sortedchainnode <E, k> * P = first; sortedchainnode <e, k> * TP = 0; // search for elements matching K (; P & P-> data <E; TP = P, P = p-> link); // needs to overload <operator // verify if it matches K if (P & P-> DATA = E) throw (badinput (); // no matching element exists. // if no duplicate key value exists, a node with the key value e is generated. <e, k> * q = new sortedchainnode <E, k>; q-> DATA = E; // after adding the node to TP, Q-> link = P; If (TP) TP-> link = Q; elsefirst = Q; return * This;} int main () {sortedchain <int, float> mysortedchain; mysortedchain. distinctinsert (12); mysortedchain. distinctinsert (1314); mysortedchain. distinctinsert (520.1314); mysortedchain. distinctinsert (1223); mysortedchain. distinctinsert (1030); mysortedchain. distinctinsert (8); mysortedchain. distinctinsert (40); cout <mysortedchain <Endl; return 0 ;}