This article illustrates an array of C + + language implementations of linear tables. Share to everyone for your reference. The specific analysis is as follows:
It is more reasonable and convenient to describe some data structures with the features of constructors and destructors in C + +. There is a problem, however, that the compiler does not support separate compilation of templates, which is uncomfortable
#include <iostream> using namespace std;
Template<class t> class CArray {public:carray (const int &imax);
CArray ();
~carray ();
void Create (const int &imax);
void Destroy ();
void Print ();
BOOL IsEmpty ();
BOOL Isfull ();
void Append (const T &data);
int GetLength ();
int Getmax ();
BOOL Delete (const int &pos);
BOOL Insert (const int &pos,const T &data);
void operator+= (const T &data);
Private:t *m_parray;
int M_len;
int M_max;
void Reset ();
}; Template<class t> carray<t>::carray (const int &imax) {Create (IMax);} template<class t> CArray< ; T>::~carray () {Destroy ():} template<class t> void carray<t>::create (const int &imax) {M_parray = n
EW T[imax];
M_max = IMax;
M_len = 0;
memset (m_parray,0,sizeof (M_parray)); Template<class t> void Carray<t>::D Estroy () {delete [] m_parray;} template<class t> void carray< T>::P rint () {if (IsEmpty ()) {cout<< "no data!
"<<endl;
else {for (int ix =0; IX < M_len; ++ix) {cout<<m_parray[ix]<< ",";
} cout<<endl;
} template<class t> bool Carray<t>::isempty () {if (0 = M_len) {return true;
else {return false;
} template<class t> bool Carray<t>::isfull () {if (M_len = = M_max) {Reset ();
return false;
else {return false; }} template<class t> void carray<t>::append (const T &data) {if (!
Isfull ()) {++m_len;
M_PARRAY[M_LEN-1] = data; } template<class t> int carray<t>::getlength () {return m_len;} template<class t> bool CARRAY<T&G
t;::D elete (const int &pos) {if (pos > M_len | | pos <= 0) {cout<< "location not valid" <<endl;
return false;
for (int ix = Pos-1 IX < m_len-1 + + ix) {M_parray[ix] = M_parray[ix + 1];
}--m_len;
return true; } template<class t> void carray<t>::operator+= (const T &data) {this->append (data); } template<class t> bool Carray<t>::insert (const int &pos,const T &data) {if (Isfull ()) {return F
Alse;
else {for (int ix = m_len-1 IX >= pos-1;--IX) {M_parray[ix + 1] = M_parray[ix];
} m_parray[pos-1] = data;
++m_len;
return true; } template<class t> Carray<t>::carray () {Create (5);} template<class t> void Carray<t>::reset (
{T *pt = new T[m_max * 2];
memset (pt,0,sizeof (PT));
for (int ix = 0; ix < M_len; + + ix) {Pt[ix] = M_parray[ix];
} delete [] M_parray;
M_parray = PT;
M_max = M_max * 2; Template<class t> int Carray<t>::getmax () {return m_max;}
I hope this article will help you with your C + + programming.