C ++ implementation of a Single-Chain Data Structure table:
// March 17, 2013 AD
// Single List -- By Paul
# Ifndef _ SingleList _
# Define _ SingleList _
# Include <iostream>
Using namespace std;
Template <typename Type> class SingleList;
// Node class...
Template <typename Type> class ListNode {
Private:
Type data;
ListNode * pnext;
Private:
Friend typename SingleList <Type>;
ListNode (): pnext (null ){}
ListNode (const Type item, ListNode <Type> * next = null): data (item), pnext (next ){}
~ ListNode ()
{
Pnext = null;
}
Public:
Type GetData ();
Friend ostream & operator <Type> (ostream &, ListNode <Type> &);
};
Template <typename Type> Type ListNode <Type >:: GetData ()
{
Return this-> data;
}
Template <typename Type> Tostream & operator <(ostream & OS, ListNode <Type> & out)
{
OS <out. data;
Return OS;
}
// Single-chain table Type
Template <typename Type> class SingleList
{
Private:
ListNode <Type> * head;
Public:
SingleList (): head (new ListNode <Type> ()){};
~ SingleList ()
{
MakeEmpty ();
Delete head;
}
// Other functions
Void MakeEmpty ();
Int Length ();
ListNode <Type> * Find (Type value, int n );
ListNode <Type> * Find (int n );
Bool Insert (Type item, int n = 0 );
Type Remove (int n = 0 );
Bool RemoveAll (Type item );
Type Get (int n );
Void Print ();
};
// Function implementation
Template <typename Type> void SingleList <Type>: MakeEmpty ()
{
ListNode <Type> * pdel;
While (head-> pnext! = Null)
{
Pdel = head-> pnext;
Head-> pnext = pdel-> pext;
Delete pdel;
}
}
Template <typename Type> int SingleList <Type>: Length ()
{
ListNode <Type> * pmove = head-> pnext;
Int count = 0;
While (pmove! = Null)
{
Pmove = pmove-> pnext;
Count ++;
}
Return count;
}
Template <typename Type> ListNode <Type> * SingleList <Type>: Find (int n)
{
If (n <0)
{
Cout <"The N is out of boundry" <endl;
Return null;
}
ListNode <Type> * pmove = head-> pnext;
For (int I = 0; I <n & pmove; I ++)
{
Pmove = pmove-> pnext;
}
If (pmove = null)
{
Cout <"The N is out of boundary" <endl;
Return null;
}
}
Template <typename Type> ListNode <Type> * SingleList <Type>: Find (Type value, int n ){
If (n <1 ){
Cout <"The n is illegal" <endl;
Return NULL;
}
ListNode <Type> * pmove = head;
Int count = 0;
While (count! = N & pmove ){
Pmove = pmove-> pnext;
If (pmove-> data = value ){
Count ++;
}
}
If (pmove = NULL ){
Cout <"can't find the element" <endl;
Return NULL;
}
Return pmove;
}
Template <typename Type> bool SingleList <Type>: Insert (Type item, int n ){
If (n <0 ){
Cout <"The n is illegal" <endl;
Return 0;
}
ListNode <Type> * pmove = head;
ListNode <Type> * pnode = new ListNode <Type> (item );
If (pnode = NULL ){
Cout <"Application error! "<Endl;
Return 0;
}
For (int I = 0; I <n & pmove; I ++ ){
Pmove = pmove-> pnext;
}
If (pmove = null ){
Cout <"the n is illegal" <endl;
Return 0;
}
Pnode-> pnext = pmove-> pnext;
Pmove-> pnext = pnode;
Return 1;
}
# Endif
// March 17, 2013 AD
// Single List -- By Paul
# Ifndef _ SingleList _
# Define _ SingleList _
# Include <iostream>
Using namespace std;
Template <typename Type> class SingleList;
// Node class...
Template <typename Type> class ListNode {
Private:
Type data;
ListNode * pnext;
Private:
Friend typename SingleList <Type>;
ListNode (): pnext (null ){}
ListNode (const Type item, ListNode <Type> * next = null): data (item), pnext (next ){}
~ ListNode ()
{
Pnext = null;
}
Public:
Type GetData ();
Friend ostream & operator <Type> (ostream &, ListNode <Type> &);
};
Template <typename Type> Type ListNode <Type >:: GetData ()
{
Return this-> data;
}
Template <typename Type> Tostream & operator <(ostream & OS, ListNode <Type> & out)
{
OS <out. data;
Return OS;
}
// Single-chain table Type
Template <typename Type> class SingleList
{
Private:
ListNode <Type> * head;
Public:
SingleList (): head (new ListNode <Type> ()){};
~ SingleList ()
{
MakeEmpty ();
Delete head;
}
// Other functions
Void MakeEmpty ();
Int Length ();
ListNode <Type> * Find (Type value, int n );
ListNode <Type> * Find (int n );
Bool Insert (Type item, int n = 0 );
Type Remove (int n = 0 );
Bool RemoveAll (Type item );
Type Get (int n );
Void Print ();
};
// Function implementation
Template <typename Type> void SingleList <Type>: MakeEmpty ()
{
ListNode <Type> * pdel;
While (head-> pnext! = Null)
{
Pdel = head-> pnext;
Head-> pnext = pdel-> pext;
Delete pdel;
}
}
Template <typename Type> int SingleList <Type>: Length ()
{
ListNode <Type> * pmove = head-> pnext;
Int count = 0;
While (pmove! = Null)
{
Pmove = pmove-> pnext;
Count ++;
}
Return count;
}
Template <typename Type> ListNode <Type> * SingleList <Type>: Find (int n)
{
If (n <0)
{
Cout <"The N is out of boundry" <endl;
Return null;
}
ListNode <Type> * pmove = head-> pnext;
For (int I = 0; I <n & pmove; I ++)
{
Pmove = pmove-> pnext;
}
If (pmove = null)
{
Cout <"The N is out of boundary" <endl;
Return null;
}
}
Template <typename Type> ListNode <Type> * SingleList <Type>: Find (Type value, int n ){
If (n <1 ){
Cout <"The n is illegal" <endl;
Return NULL;
}
ListNode <Type> * pmove = head;
Int count = 0;
While (count! = N & pmove ){
Pmove = pmove-> pnext;
If (pmove-> data = value ){
Count ++;
}
}
If (pmove = NULL ){
Cout <"can't find the element" <endl;
Return NULL;
}
Return pmove;
}
Template <typename Type> bool SingleList <Type>: Insert (Type item, int n ){
If (n <0 ){
Cout <"The n is illegal" <endl;
Return 0;
}
ListNode <Type> * pmove = head;
ListNode <Type> * pnode = new ListNode <Type> (item );
If (pnode = NULL ){
Cout <"Application error! "<Endl;
Return 0;
}
For (int I = 0; I <n & pmove; I ++ ){
Pmove = pmove-> pnext;
}
If (pmove = null ){
Cout <"the n is illegal" <endl;
Return 0;
}
Pnode-> pnext = pmove-> pnext;
Pmove-> pnext = pnode;
Return 1;
}
# Endif