// List. h # ifndef list_h # define maxsize 100 # include <iostream> using namespace STD; Template <class T> class list {public: List (); Virtual ~ List (); void initlist (); void insert (INT POs, t Val); // return the inserted content t del (int pos ); // return the deleted content void clean (); bool isempty (); t getval (int pos); t setval (INT POs, t Val); void display (); protected: t data [maxsize]; int Len; private :};# endif // list_h // list. CPP pay attention to the writing situation of functions.
# Include ".. /include/list. H "template <class T> List <t>: List () {// Len = 0; For (INT I = 0; I <MAXSIZE-5; I ++) {data [I] = 1;} Len = MAXSIZE-5;} template <class T> List <t> ::~ List () {Delete [] data;} template <class T> void list <t >:: initlist () {int I = 0; while (1) {CIN> data [I]; If (data [I] = 0) {break;} I ++; Len ++ ;}} template <class T> void list <t>: Display () {for (INT I = 0; I <Len; I ++) {cout <data [I] <";}cout <Endl; cout <Endl ;}template <class T> void list <t> :: insert (INT POs, t Val) // POS is subscript {If (LEN <(MAXSIZE-1) {// determine whether to overflow if (Pos <= Len) {for (INT I = 0; I <(len-pos-1); I ++) {data [Len-I] = data [len-1-i]; // shift} data [POS] = val; Len ++ ;} else {cout <"cannot be inserted! "<Endl ;}}else {cout <" "<Endl ;}}template <class T> T list <t >:: del (int pos) {T temp = data [POS]; for (INT I = Pos; I <len-1; I ++) {data [I] = data [I + 1];} len --; return temp;} template <class T> bool list <t >:: isempty () {If (LEN = 0) {return true;} return false ;} template <class T> void list <t>: Clean () {Len = 0;} template <class T> T list <t >:: getval (int pos) {return data [POS];} template <class T> T list <t >:: setval (INT POs, t Val) {T temp = data [POS]; data [POS] = val; return temp;} // main # include ". /include/list. H "# include ". /src/list. CPP "// if the list in the file main is not added, the int main () {// cout <" initialization sequence table: "<Endl; List <int> list is not defined; // list. initlist (); list. display (); list. insert (99,2); list. insert (2, 2); List. display (); // cout <list. getval (2) <Endl; List. del (2); list. display ();/* List. clean (); cout <list. isempty () <Endl; */list. setval (2, 8); list. display (); Return 0 ;}