Sequence Table Structure template class

Source: Internet
Author: User
// 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.