templateclass SingleList;template class ListNode{private: friend typename SingleList ; ListNode():m_pnext(NULL){} ListNode(const Type item,ListNode *next=NULL):m_data(item),m_pnext(next){} ~ListNode(){ m_pnext=NULL; }public: Type GetData(); friend ostream& operator<< (ostream& ,ListNode &);private: Type m_data; ListNode *m_pnext;};template Type ListNode ::GetData(){ return this->m_data;}template ostream& operator<<(ostream& os,ListNode & out){ os<
#include "ListNode.h"templateclass SingleList{public: SingleList():head(new ListNode ()){} ~SingleList(){ MakeEmpty(); delete head; }public: void MakeEmpty(); //make the list empty int Length(); //get the length ListNode *Find(Type value,int n); //find thd nth data which is equal to value ListNode *Find(int n); //find the nth data bool Insert(Type item,int n=0); //insert the data in the nth position Type Remove(int n=0); //remove the nth data bool RemoveAll(Type item); //remove all the data which is equal to item Type Get(int n); //get the nth data void Print(); //print the listprivate: ListNode *head;};template void SingleList ::MakeEmpty(){ ListNode *pdel; while(head->m_pnext!=NULL){ pdel=head->m_pnext; head->m_pnext=pdel->m_pnext; delete pdel; }}template int SingleList ::Length(){ ListNode *pmove=head->m_pnext; int count=0; while(pmove!=NULL){ pmove=pmove->m_pnext; count++; } return count;}template ListNode * SingleList ::Find(int n){ if(n<0){ cout<<"The n is out of boundary"< *pmove=head->m_pnext; for(int i=0;i m_pnext; } if(pmove==NULL){ cout<<"The n is out of boundary"< ListNode * SingleList ::Find(Type value,int n){ if(n<1){ cout<<"The n is illegal"< *pmove=head; int count=0; while(count!=n&&pmove){ pmove=pmove->m_pnext; if(pmove->m_data==value){ count++; } } if(pmove==NULL){ cout<<"can't find the element"< bool SingleList ::Insert(Type item, int n){ if(n<0){ cout<<"The n is illegal"< *pmove=head; ListNode *pnode=new ListNode (item); if(pnode==NULL){ cout<<"Application error!"< m_pnext; } if(pmove==NULL){ cout<<"the n is illegal"< m_pnext=pmove->m_pnext; pmove->m_pnext=pnode; return 1;}template bool SingleList ::RemoveAll(Type item){ ListNode *pmove=head; ListNode *pdel=head->m_pnext; while(pdel!=NULL){ if(pdel->m_data==item){ pmove->m_pnext=pdel->m_pnext; delete pdel; pdel=pmove->m_pnext; continue; } pmove=pmove->m_pnext; pdel=pdel->m_pnext; } return 1;}template Type SingleList ::Remove(int n){ if(n<0){ cout<<"can't find the element"< *pmove=head,*pdel; for(int i=0;i m_pnext;i++){ pmove=pmove->m_pnext; } if(pmove->m_pnext==NULL){ cout<<"can't find the element"< m_pnext; pmove->m_pnext=pdel->m_pnext; Type temp=pdel->m_data; delete pdel; return temp;}template Type SingleList ::Get(int n){ if(n<0){ cout<<"The n is out of boundary"< *pmove=head->m_pnext; for(int i=0;i m_pnext; if(NULL==pmove){ cout<<"The n is out of boundary"< m_data;}template void SingleList ::Print(){ ListNode *pmove=head->m_pnext; cout<<"head"; while(pmove){ cout<<"--->"< m_data; pmove=pmove->m_pnext; } cout<<"--->over"< < <
#includeusing namespace std;#include "SingleList.h"int main(){ SingleList list; for(int i=0;i<20;i++){ list.Insert(i*3,i); } for(int i=0;i<5;i++){ list.Insert(3,i*3); } cout<<"the Length of the list is "< <
==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/04/08/2438183.html,如需转载请自行联系原作者