Single-chain table (C ++)

Source: Internet
Author: User

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


 

Related Article

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.