数据结构c++.doc
文本预览下载声明
ListNode.h
templatetypename Type class SingleList;
templatetypename Type class ListNode{
private:
friend typename SingleListType;
ListNode():m_pnext(NULL){}
ListNode(const Type item,ListNodeType *next=NULL):m_data(item),m_pnext(next){}
~ListNode(){
m_pnext=NULL;
}
public:
Type GetData();
friend ostream operator Type(ostream ,ListNodeType);
private:
Type m_data;
ListNode *m_pnext;
};
templatetypename Type Type ListNodeType::GetData(){
return this-m_data;
}
templatetypename Type ostream operator(ostream os,ListNodeType out){
osout.m_data;
return os;
}
SingleList.h
#include ListNode.h
templatetypename Type class SingleList{
public:
SingleList():head(new ListNodeType()){}
~SingleList(){
MakeEmpty();
delete head;
}
public:
void MakeEmpty(); //make the list empty
int Length(); //get the length
ListNodeType *Find(Type value,int n); //find thd nth data which is equal to value
ListNodeType *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 list
private:
ListNodeType *head;
};
templatetypename Type void SingleListType::MakeEmpty(){
ListNodeType *pdel;
while(head-m_pnext!=NULL){
pdel=head-m_pnext;
head-m_pnext=pdel-m_pnext;
delete pdel;
}
}
templatetypename Type int SingleListType::Length(){
ListNodeType *pmove=head-m_pnext;
int count=0;
while(pmove!=NULL){
pmove=pmove-m_pnext;
count++;
}
return count;
}
templatetypename Type ListNodeType* SingleListType::Find(int n){
if(n0){
coutThe n is out of boundaryendl;
return NULL;
}
ListNodeType *pmove=head-m_pnext;
for(int i=0;inpmove;i++){
pmove=pmove-m_pnext;
}
if(pmove==
显示全部