///// // The definition of a single-chain table with an additional header node // /// // <br/> template <class T> // defined in the header file "listing list. H "<br/> struct linknode // definition of the linked list node class <br/>{< br/> T data; // data field <br/> linknode <t> * link; // link pointer field <br/> linknode (linknode <t> * PTR = NULL) <br/> {link = PTR;} // only initializes the constructor of pointer members. <br/> linknode (const T & item, linknode <t> * PTR = NULL) <br/> {DATA = item; link = PTR;} // initialize the constructor of data and pointer members <br/> }; <Br/> template <class T> <br/> class list: Public linearlist <t> // single-chain table class definition, you can also implement <br/>{< br/> Public: <br/> List () {First = new linknode <t >;} without inheritance ;} // constructor <br/> List (const T & X) {First = new linknode <t> (x );} // constructor <br/> List (list <t> & L); // copy constructor <br/> ~ List () {makeempty () ;}// destructor <br/> void makeempty (); // sets the linked list to an empty table <br/> int length () const; // calculate the length of the linked list <br/> linknode <t> * gethead () const {return first ;} // return the additional header node address <br/> linknode <t> * search (t x ); // search for elements containing data x <br/> linknode <t> * locate (int I ); // search for the address of element I <br/> bool getdata (int I, T & X ); // retrieve the value of element I <br/> void setdata (int I, T & X ); // use X to modify the value of element I <br/> bool insert (int I, T & X ); // insert X after element I <br/> bool remove (int I, T & X );// Delete the I-th element. x returns the value of this element. <br/> bool isempty () const // determines whether the table is empty? If it is null, false is returned. <br/> {return first-> link = NULL? True; false ;}< br/> bool isfull () const {return false ;}// determines if the table is full? False <br/> void sort (); // sort <br/> void input (); // input <br/> void output (); // output <br/> List <t> & operator = (list <t> & L); // overload function: Value assignment <br/> protected: <br/> linknode <t> * First; // head pointer of the linked list <br/> }; <br/> // member functions of the List class ////////////////////////////////////// /// <br/> template <class T> <br/> List <t>:: List (list <t> & L) <br/>{< br/> // copy the constructor <br/> T value; <br/> linknode <t> * srcptr = L. gethe AD (); // additional header Node Address of the copied table <br/> linknode <t> * destptr = first = new linknode <t>; <br/> while (srcptr-> link! = NULL) // copy nodes one by one <br/>{< br/> value = srcptr-> link-> data; <br/> destptr-> link = new linknode <t> (value); <br/> destptr = destptr-> link; <br/> srcptr = srcptr-> link; <br/>}< br/> destptr-> link = NULL; <br/> }; <br/> template <class T> <br/> void list <t>: makeempty () <br/>{< br/> // set the linked list to an empty table <br/> linknode <t> * q; <br/> while (first-> link! = NULL) // when the link is not empty, delete all nodes in the chain <br/>{< br/> q = first-> link; <br/> first-> link = Q-> link; // Save the deleted node and remove it from the chain. <br/> Delete Q; // Delete (only one header node is retained) <br/>}< br/>}; <br/> template <class T> <br/> int list <t>:: length () const <br/>{< br/> // calculate the length of a single-chain table with an additional header node <br/> linknode <t> * P = first-> link; <br/> int COUNT = 0; <br/> while (P! = NULL) // trace scanning to find the end of a chain <br/>{< br/> P = p-> link; <br/> count ++; <br/>}< br/> return count; <br/> }; <br/> template <class T> <br/> linknode <t> * List <t>: Search (t x) <br/>{< br/> // search for a node with data X in the table. If the search is successful, the function returns the node address; otherwise, the return value is null. <Br/> linknode <t> * Current = first-> link; <br/> while (current! = NULL) <br/>{< br/> If (current-> DATA = x) break; // search for x nodes in the chain. <br/> else current = Current-> link; <br/>}< br/> Return Current; <br/> }; <br/> template <class T> <br/> linknode <t> * List <t>: locate (int I) <br/>{< br/> // locate the function. Returns the address of the I element in the table. If I <0 or I exceeds the number of nodes in the table, null is returned. <Br/> if (I <0) return NULL; // I is unreasonable <br/> linknode <t> * Current = first; int K = 0; <br/> while (current! = NULL & K <I) // trace the I node <br/> {<br/> current = Current-> link; <br/> K ++; <br/>}< br/> Return Current; // return the address of node I. If null is returned, indicates that the I value is too large <br/>}; <br/> template <class T> <br/> bool list <t>: getdata (int I, T & X) <br/>{< br/> // retrieve the value of the I element in the chain <br/> if (I <= 0) return NULL; // The I value is too small <br/> linknode <t> * Current = locate (I); <br/> If (current = NULL) return false; // The I value is too large <br/> else {x = Current-> data; return true ;}< br/>}; <br/> templa Te <class T> <br/> void list <t>: setdata (int I, T & X) <br/>{< br/> // assign X to the I element in the linked list. <br/> if (I <= 0) return; // The I value is too small <br/> linknode <t> * Current = locate (I ); <br/> If (current = NULL) return; // The I value is too large <br/> else current-> DATA = x; <br/> }; <br/> template <class T> <br/> bool list <t>: insert (int I, T & X) <br/>{< br/> // Insert the new element x after the I node in the linked list. <Br/> linknode <t> * Current = locate (I); <br/> If (current = NULL) return false; // insertion failed <br/> linknode <t> * newnode = new linknode <t> (x); <br/> If (newnode = NULL) <br/>{< br/> cerr <"Storage Allocation Error! "<Endl; <br/> exit (1); <br/>}< br/> newnode-> link = Current-> link; // link after current <br/> current-> link = newnode; <br/> return true; // insert successful <br/> }; <br/> template <class T> <br/> bool list <t>: Remove (int I, T & X) <br/> {<br/> // Delete the I-th element in the linked list, returns the value of this element through the referenced parameter x <br/> linknode <t> * Current = locate (I-1 ); <br/> If (current = NULL | current-> link = NULL) <br/> return false; // deletion failed <br/> linknode <t> * del = Current-> L Ink; // re-Zipper, remove the deleted node from the chain <br/> current-> liink = del-> link; <br/> X = del-> data; delete del; // retrieve the data value in the deleted node <br/> return true; <br/> }; <br/> template <class T> <br/> void list <t>: output () <br/>{< br/> // output function of a single-chain table: output all data in a single-chain table to the screen in a logical order <br/> linknode <t> * Current = first-> link; <br/> while (current! = NULL) <br/>{< br/> cout <Current-> data <Endl; <br/> current = Current-> link; <br/>}< br/>}; <br/> template <class T> <br/> List <t> & list <t> :: operator = (list <t> & L) <br/>{< br/> // reload the function: assign a value, for example, A = B, A is the list object that calls this operation, and B is the real parameter combined with referenced parameter l in the parameter table <br/> T value; <br/> linknode <t> * srcptr = L. gethead (); // additional header Node Address of the copied table <br/> linknode <t> * destptr = first = new linknode <t>; <br/> while (srcptr-> link! = NULL) // copy nodes one by one <br/>{< br/> value = srcptr-> link-> data; <br/> destptr-> link = new linknode <t> (value); <br/> destptr = destptr-> link; <br/> srcptr = srcptr-> link; <br/>}< br/> destptr-> link = NULL; <br/> return * This; // return the address of the operation object <br/> };