/// // The abstract base class of the linear table /////////// /// // <br/> Enum bool {false, true }; <br/> template <class T> <br/> class linearlist <br/>{< br/> Public: <br/> linearlist (); // constructor <br/> ~ Linearlist (); // destructor <br/> virtual int size () const = 0; // calculate the maximum table volume <br/> virtual int length () const = 0; // calculate the table length <br/> virtual int search (T & X) const = 0; // search for the given value X in the table <br/> virtual int locate (int I) const = 0; // position the I-th element in the table <br/> virtual bool getdata (int I, T & X) const = 0; // obtain the value of Table I <br/> virtual bool setdata (int I, T & X) const = 0; // modify the value of Table I to X <br/> virtual bool insert (int I, T & X) const = 0; // insert X after table I <br/> virt Ual bool remove (int I, T & X) const = 0; // Delete the I-th table item and return through x <br/> virtual bool isempty () const = 0; // empty table determination <br/> virtual bool isfull () const = 0; // The table determination is full. <br/> virtual void sort () = 0; // sort <br/> virtual void input () = 0; // input <br/> virtual void output () = 0; // output <br/> virtual linearlist <t> operator = (linearlist <t> & L) = 0; // copy <br/> }; <br/> // sequence table class declaration (seqlist. h) /// // <br/> # include <IOS Tream. h> <br/> # include <stdlib. h> <br/> # include "linearlist. H "<br/> const int defaultsize = 100; <br/> template <class T> <br/> class seqlist: public linearlist <t> <br/> {<br/> protected: <br/> T * data; // store the array <br/> int maxsize; // maximum number of items in a table <br/> int last; // the last position of the currently saved table item (starting from 0) <br/> void resize (INT newsize); // change the data array space size <br/> Public: <br/> seqlist (int sz = defaultsize ); // constructor <br/> seqlist (seqlist <t> & L); // copy the constructor Function <br/> ~ Seqlist () {Delete [] data;} // destructor <br/> int size () const {return maxsize ;} // calculate the maximum number of table items that a table can accommodate <br/> int length () const {return last + 1 ;} // calculate the table length <br/> int search (T & X) const; // search for the position of X in the table, number of table items returned by the function <br/> int locate (int x) const; // you can specify the I-th table item, number of table items returned by the function <br/> bool getdata (int I, T & X) const // take the value of table entry I <br/> {if (I> 0 & I <= last + 1) {x = data [I-1]; return true;} else return false;} <br/> void setdata (int I, T & X) const // use X to modify the value of Table I <br/> {if (I> 0 & I <= last + 1) Data [I-1] DATA [I-1] = x ;}< br/> bool insert (int I, T & X ); // insert X after the I table entry <br/> bool remove (int I, T & X); // Delete the I table entry, return the table item value through x <br/> bool isempty () {return (last =-1 )? True; false;} // determines whether the table is empty. If it is null, true is returned. Otherwise, false is returned. <br/> bool isfull () {return (lase = maxSize-1 )? True; false;} // determines whether the table is full or not. True is returned if the table is full. Otherwise, false is returned. <br/> void input (); // input <br/> void output (); // output <br/> seqlist <t> operator = (seqlist <t> & L ); // overall table value assignment <br/>}< br/> // struct constructor and copy constructor -------------------------------------- <br/> template <class T> <br/> seqlist <t >:: seqlist (INT sz) <br/>{< br/> // constructor, define the length of the array by specifying the sz parameter <br/> If (SZ> 0) <br/> {<br/> maxsize = SZ; last =-1; // set the actual table length to null <Br/> DATA = new T [maxsize]; // create an sequence table storage array <br/> If (Data = NULL) // Dynamic Allocation failed <br/>{< br/> cerr <"Storage Allocation failed! "<Endl; <br/> exit (1 ); <br/>}< br/> template <class T> <br/> seqlist <t> :: seqlist (seqlist <t> & L) <br/>{< br/> // copy the constructor, initialize the new sequence table with the given sequence table in the parameter table <br/> maxsize = L. size (); last = L. length ()-1; <br/> DATA = new T [maxsize]; // create an sequence table storage array <br/> If (Data = NULL) // Dynamic Allocation failed <br/>{< br/> cerr <"Storage Allocation Error! "<Endl; <br/> exit (1); <br/>}< br/> for (INT I = 1; I <= last + 1; I ++) <br/> data [I-1] = L. getdata (I); <br/>}< br/> template <class T> <br/> void seqlist <t >:: resize (INT newsize) <br/>{< br/> // Private function: expands the size of the memory array space of the sequence table, the number of elements in the new array is newsize <br/> If (newsize <= 0) // check the rationality of parameters <br/>{< br/> cerr <"invalid array size! "<Endl; <br/> return; <br/>}< br/> If (newsize! = Maxsize) // modify <br/> {<br/> T * newarray = new T [newsize]; // create a new array <br/> If (newarray = NULL) <br/>{< br/> cerr <"Storage Allocation Error! "<Endl; <br/> exit (1); <br/>}< br/> int n = last + 1; <br/> T * srcptr = data; // source array first address <br/> T * destptr = newarray; // destination array first address <br/> while (n --) * desrptr ++ = * srcptr ++; // copy <br/> Delete [] data; // Delete the old array <br/> DATA = newarray; maxsize = newsize; // copy a new array <br/>}< br/> // perform the following operations to search and locate: ------------------------------------------ <br/> template <class T> <br/> int seql Ist <t>: Search (T & X) const <br/> {<br/> // search function: searches tables that match the specified value x in sequence, if it is found, the function returns the first element of the table. Otherwise, 0 is returned, indicating that the search fails. <Br/> for (INT I = 0; I <= last; I ++) <br/> If (data [I] = x) return I + 1; // sequential search <br/> return 0; // search failed <br/>}< br/> template <class T> <br/> int seqlist <t> :: locate (int I) const <br/>{< br/> // positioning function: the function returns the I (1 <= I <= last + 1) table items. Otherwise, the function returns 0, indicating that the positioning fails. <Br/> if (I> = 1 & I <= last + 1) <br/> return I; <br/> else <br/> return 0; <br/>}< br/> // queue insertion and deletion operations ------------------------------------------------ <br/> template <class T> <br/> bool seqlist <t> :: insert (int I, T & X) <br/>{< br/> // Insert the new element x into the table I (1 <= I <= last + 1) tables. The function returns the information about successful insertion. If successful insertion, true is returned. <br/> // otherwise, false is returned. I = 0 is virtual. It is actually inserted to the position of 1st elements. <Br/> If (last = maxSize-1) return false; // The table is full and cannot be inserted <br/> if (I <0 | I> last + 1) return false; // The parameter I is unreasonable and cannot be inserted. <br/> for (Int J = last; j> = I; j --) <br/> data [J + 1] = data [J]; // The data is moved back in sequence, and the position I is left blank. <br/> data [I] = X; // insert <br/> last ++; // Add 1 to the last position <br/> return true; // insert successful <br/>}< br/> template <class T> <br/> bool seqlist <t>: Remove (int I, T & X) <br/> {<br/> // Delete the I (1 <= I <= last + 1) Table items from the table, return the deleted element value by referencing parameter X. <Br/> // The function returns the information about successful deletion. If the deletion is successful, true is returned. Otherwise, false is returned. <Br/> If (last =-1) return false; // The table is empty and cannot be deleted <br/> if (I <0 | I> last + 1) return false; // The parameter I is unreasonable and cannot be deleted <br/> X = data [I-1]; // Save the deleted element value <br/> for (Int J = I; j <= last; j ++) <br/> data [J-1] = data [J]; // move back in sequence to fill in <br/> last --; // the last position minus 1 <br/> return true; // deletion successful <br/>}< br/> // values input/output and value assignment -------------------------------------------- <br/> template <class T> <br/> void seqlist <t >:: input () <Br/> {<br/> // enter data one by one from the standard input (keyboard) and create an order table <br/> cout <"to create an order table, enter the number of elements in the table: "; <br/> while (1) <br/>{< br/> CIN> last; // input element last position <br/> If (last <= maxSize-1) break; <br/> cout <"the number of table elements is incorrect, the range cannot exceed "<maxSize-1 <": "; <br/>}< br/> for (INT I = 0; I <= last; I ++) <br/>{< br/> CIN> data [I]; <br/> cout <I + 1 <Endl; <br/>}< br/> template <class T> <br/> void seqlist <t>: output () <br/>{< br/> // output all elements of the sequence table to the screen <Br/> cout <"the last position of the current element in the sequence table is:" <last <Endl; <br/> for (INT I = 0; I <= last; I ++) <br/> cout <"#" <I + 1 <":" <data [I] <Endl; <br/>}< br/> template <class T> <br/> seqlist <t>: Operator = (seqlist <t> & L) <br/>{< br/> // reload operation: assign values to the entire sequence table. If the currently called table object is L1 and the substitution parameter l is L2, the usage is L1 = L2. For the implementation code, refer to the copy constructor section. <Br/>}