This paper illustrates the realization of the link list of C + + language realization of linear table. Share to everyone for your reference. The specific analysis is as follows:
Inserts, deletes the node the code to be somewhat many, but this enhances the code the readability, and does not increase the time complexity, does not affect the program performance
#include <iostream> using namespace std;
Template<typename t> class CList;
Template<class T> class Node {friend clist<t>; private:t m_data;
Node *m_pnext;
};
Template<class t> class CList {public:clist ();
~clist ();
BOOL IsEmpty ();
void Append (const T &data);
void Delete (const int &pos);
void Print ();
int GetLength ();
T Find (const int &pos);
void Insert (const int &pos,const T &data);
Private:node<t> *m_phead;
Node<t> *m_pend;
int M_len;
void Create ();
void Destroy ();
};
Allocate space for head nodes Template<class t> void Clist<t>::create () {m_phead = new node<t>;
M_pend = new node<t>;
M_phead->m_pnext = NULL;
M_pend->m_pnext = m_phead->m_pnext;
M_len = 0; Template<class t> clist<t>::clist () {Create ();}//Delete all nodes template<class t> void clist<t>::D es
Troy () {Node<t> *PF = m_phead->m_pnext;
Node<t> *pt;
while (PF) {PT = PF; PF = Pf-> m_pnext;
Delete PT; } template<class t> clist<t>::~clist () {Destroy ();}//Determine if NULL Template<class t> bool Clist<t>:
: IsEmpty () {if (!m_phead->m_pnext) {return true;
else {return false; }///Add an element from the end of the table Template<class t> void clist<t>::append (const T &data) {node<t> *pt = new NODE&L T
t>;
Pt->m_data = data;
Pt->m_pnext = NULL;
if (!m_phead->m_pnext) {m_phead->m_pnext = PT;
else {(m_pend->m_pnext)->m_pnext = PT;
} m_pend->m_pnext = PT;
++m_len;
//Delete an element template<class t> void Clist<t>::D elete (const int &pos) {if (pos < 0 | | Pos < m_len) {
cout<< "Location not valid" <<endl;
Return node<t> *ppre = null;//Store the previous node node<t> *pbehind = null;//Store the latter node node<t> *pt = m_phead->m_pnext;
Target node Int ix =-1;
while (PT) {++ix;
if (ix = = pos-1-1) {ppre = PT;
else if (ix = = pos-1) {Pbehind = pt->m_pnext; BReak;
} PT = pt->m_pnext;
if (!ppre)//If the pointer is empty, the POS refers to the first element {delete PT;
M_phead->m_pnext = Pbehind;
--m_len;
Return
if (!pbehind)//If the pointer is empty, the POS refers to the last element {m_pend = Ppre;
Delete PT;
} ppre->m_pnext = Pbehind;
--m_len;
//Output all data template<class t> void Clist<t>::P rint () {node<t> *pt = m_phead->m_pnext;
while (PT) {cout<<pt->m_data<< ",";
PT = pt->m_pnext;
} cout<<endl; Template<class t> int clist<t>::getlength () {return m_len;}//Find data template<class t> T CLIST<T&G
T;::find (const int &pos) {if (pos <= 0) {cout<< "entered illegal" <<endl;
return NULL;
} if (pos > M_len) {cout<< "exceeded table Length" <<endl;
return NULL;
int i = 0;
node<t> *pt = m_phead->m_pnext;
while (PT) {++i;
if (i = = pos) {return pt->m_data;
} PT = pt->m_pnext;
return NULL; } template<class t> void Clist<t>::insert (const int &pos,consT T &data {if (pos <= 0 | | | pos >m_len) {cout<< "input illegal" <<endl;
Return
int i = 0;
node<t> *pt = m_phead->m_pnext;
node<t> *ppre = NULL;
node<t> *pbehind = NULL;
while (PT) {++i;
if (i = = pos-1) {ppre = PT;
} if (i = = pos) {pbehind = pt->m_pnext;
Break
} PT = pt->m_pnext;
} node<t> *pnew = new node<t>;
Pnew->m_data = data;
if (!ppre)//If the pointer is empty, it means that the POS is the first element {Pnew->m_pnext = m_phead->m_pnext;
M_phead->m_pnext = pnew;
++m_len;
Return
if (!pbehind)//If the pointer is empty, the POS refers to the last element {m_pend->m_pnext = pnew;
} ppre->m_pnext = Pnew;
Pnew->m_pnext = PT;
++m_len; }
I hope this article will help you with your C + + programming.