As a result of work needs, we have implemented a chain table encapsulation. Only basic interfaces are implemented. You can add code if you have special requirements.
// Linked list template class. //////////////////////////////////////// //////////////////
/*
Name: linked list template
Copyright:
Author:
Description: implements a linked list.
*/
Template <typename T>
Class clinklist
{
Public:
Clinklist ();
~ Clinklist ();
Void add (T newdata );
Bool Delete (long idx );
Long getcount ();
Bool look (long idx, T * val );
Void clearall ();
T * gethead ();
T * gettail ();
PRIVATE:
T * m_pdata, * m_ptail;
};
Template <typename T>
T * clinklist <t>: gethead ()
{
Return m_pdata;
}
Template <typename T>
T * clinklist <t>: gettail ()
{
Return m_ptail;
}
Template <typename T>
Void clinklist <t >:: clearall () // clear all.
{
If (null = m_pdata) {return ;}
T * TMP = m_pdata;
T * tmp2;
While (TMP)
{
Tmp2 = TMP-> next;
Delete TMP;
TMP = tmp2;
}
M_pdata = m_ptail = NULL;
}
Template <typename T>
Clinklist <t>: clinklist () // Constructor
{
M_pdata = m_ptail = NULL;
}
Template <typename T>
Clinklist <t> ::~ Clinklist () // Structure
{
If (m_pdata)
Clearall ();
}
Template <typename T>
Void clinklist <t>: add (T newdata) // Add a data at the end of the linked list
{
If (m_pdata) // If the linked list is not empty
{
T * newitem = new T;
Memcpy (newitem, & newdata, sizeof t );
Newitem-> next = NULL;
M_ptail-> next = newitem;
M_ptail = newitem;
}
Else
{
M_pdata = new T;
Memcpy (m_pdata, & newdata, sizeof t );
M_pdata-> next = NULL;
M_ptail = m_pdata;
}
}
Template <typename T>
Bool clinklist <t>: delete (long idx) // not implemented
{
Return true;
}
Template <typename T>
Long clinklist <t>: getcount () // obtain the number of data records.
{
Long Count = 0;
T * TMP = m_pdata;
While (TMP)
{
Count ++;
TMP = TMP-> next;
}
Return count;
}
Template <typename T>
Bool clinklist <t>: Look (long idx, T * val) // you can view the data of a specified index.
{
Long Count = 0;
T * TMP = m_pdata;
While (TMP)
{
If (COUNT = idx)
{
Memcpy (Val, TMP, sizeof t );
Return true;
}
Count ++;
TMP = TMP-> next;
}
Return false;
}
The entire template class is defined in the header file.
When instantiating this template, the type used is a structure, which requires a next field of its own pointer type. For example
Typedef struct aaa
{
Double data;
Struct AAA * next;
} Mytype;
Clinklist <mytype> mylist;