Linked list template

Source: Internet
Author: User

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;

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.