目 录
1、顺序表 1
Seqlist.h 1
Test.cpp 6
2、单链表 8
ListNode.h 8
SingleList.h 10
test.cpp 20
3、双向链表 22
NodeList.h 22
DoubleList.h 24
Test.cpp 34
4、循环链表 36
ListNode.h 36
CircularList.h 37
Test.cpp 47
5、顺序栈 49
SeqStack.h 49
Test.cpp 54
6、链式栈 55
StackNode.h 55
LinkStack.h 56
Test.cpp 60
7、顺序队列 62
SeqQueue.h 63
Test.cpp 68
8、链式队列 70
QueueNode.h 70
LinkQueue.h 71
Test.cpp 75
9、优先级队列 77
QueueNode.h 77
Compare.h 78
PriorityQueue.h 80
Test.cpp 85
10、串 88
MyString.h 88
MyString.cpp 90
test.cpp 101
11、二叉树 104
BinTreeNode.h 104
BinaryTree.h 112
Test.cpp 124
12、线索二叉树 126
ThreadNode.h 126
ThreadTree.h 128
ThreadInorderIterator.h 128
test.cpp 139
13、堆 140
MinHeap.h 140
test.cpp 147
14、哈夫曼树 149
BinTreeNode.h 149
BinaryTree.h 151
MinHeap.h 156
Huffman.h 161
Test.cpp 163
15、树 164
QueueNode.h 164
LinkQueue.h 165
TreeNode.h 169
Tree.h 170
test.cpp 187
16、B+树 189
BTreeNode.h 189
BTree.h 192
test.cpp 215
17、图 217
MinHeap.h 217
Edge.h 222
Vertex.h 223
Graph.h 224
test.cpp 246
18、排序 249
Data.h 249
QueueNode.h 255
LinkQueue.h 259
Sort.h 263
test.cpp 278
1、顺序表
Seqlist.h
const int DefaultSize=100;
template
class SeqList{
public:
SeqList(int sz=DefaultSize)
:m_nmaxsize(sz),m_ncurrentsize(-1){
if(sz>0){
m_elements=new Type[m_nmaxsize];
}
}
~SeqList(){
delete[] m_elements;
}
int Length() const{ //get the length
return m_ncurrentsize+1;
}
int Find(Type x) const; //find the position of x
int IsElement(Type x) const; //is it in the list
int Insert(Type x,int i); //insert data
int Remove(Type x); //delete data
int IsEmpty(){
return m_ncurrentsize==-1;
}
int IsFull(){
return m_ncurrentsize==m_nmaxsize-1;
}
Type Get(int i){ //get the ith data
return i<0||i>m_ncurrentsize?(cout<<"can't find the element"<
int SeqList
::Find(Type x) const{
for(int i=0;i
int SeqList
::IsElement(Type x) const{ if(Find(x)==-1) return 0; return 1; } template
int SeqList
::Insert(Type x, int i){ if(i<0||i>m_ncurrentsize+1||m_ncurrentsize==m_nmaxsize-1){ cout<<"the operate is illegal"<
i;j--){ m_elements[j]=m_elements[j-1]; } m_elements[i]=x; return 1; } template
int SeqList
::Remove(Type x){ int size=m_ncurrentsize; for(int i=0;i
void SeqList
::Print(){ for(int i=0;i<=m_ncurrentsize;i++) cout<
<<":\t"<
<
#include "SeqList.h" using namespace std; int main() { SeqList
test(15); int array[15]={2,5,8,1,9,9,7,6,4,3,2,9,7,7,9}; for(int i=0;i<15;i++){ test.Insert(array[i],0); } test.Insert(1,0); cout<<(test.Find(0)?"can't be found ":"Be found ")<< 0 << endl<
class 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<
class 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 list private: 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"<
<
<
using 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 "<
<
class DoublyList; template
class ListNode{ private: friend class DoublyList
; ListNode():m_pprior(NULL),m_pnext(NULL){} ListNode(const Type item,ListNode
*prior=NULL,ListNode
*next=NULL) :m_data(item),m_pprior(prior),m_pnext(next){} ~ListNode(){ m_pprior=NULL; m_pnext=NULL; } public: Type GetData(); private: Type m_data; ListNode *m_pprior; ListNode *m_pnext; }; template
Type ListNode
::GetData(){ return this->m_data; } DoubleList.h #include "ListNode.h" template
class DoublyList{ public: DoublyList():head(new ListNode
()){ //the head node point to itself head->m_pprior=head; head->m_pnext=head; } ~DoublyList(){ MakeEmpty(); delete head; } public: void MakeEmpty(); //make the list empty int Length(); //get the length of the list ListNode
*Find(int n=0); //find the nth data ListNode
* FindData(Type item); //find the data which is equal to item bool Insert(Type item,int n=0); //insert item in the nth data Type Remove(int n=0); //delete the nth data Type Get(int n=0); //get the nth data void Print(); //print the list private: ListNode
*head; }; template
void DoublyList
::MakeEmpty(){ ListNode
*pmove=head->m_pnext,*pdel; while(pmove!=head){ pdel=pmove; pmove=pdel->m_pnext; delete pdel; } head->m_pnext=head; head->m_pprior=head; } template
int DoublyList
::Length(){ ListNode
*pprior=head->m_pprior,*pnext=head->m_pnext; int count=0; while(1){ if(pprior->m_pnext==pnext){ break; } if(pprior==pnext&&pprior!=head){ count++; break; } count+=2; pprior=pprior->m_pprior; pnext=pnext->m_pnext; } return count; } template
ListNode
* DoublyList
::Find(int n = 0){ if(n<0){ cout<<"The n is out of boundary"<
*pmove=head->m_pnext; for(int i=0;i
m_pnext; if(pmove==head){ cout<<"The n is out of boundary"<
bool DoublyList
::Insert(Type item,int n){ if(n<0){ cout<<"The n is out of boundary"<
*newnode=new ListNode
(item),*pmove=head; if(newnode==NULL){ cout<<"Application Erorr!"<
m_pnext; if(pmove==head){ cout<<"The n is out of boundary"<
m_pnext=pmove->m_pnext; newnode->m_pprior=pmove; pmove->m_pnext=newnode; newnode->m_pnext->m_pprior=newnode; return 1; } template
Type DoublyList
::Remove(int n = 0){ if(n<0){ cout<<"The n is out of boundary"<
*pmove=head,*pdel; for(int i=0;i
m_pnext; if(pmove==head){ cout<<"The n is out of boundary"<
m_pprior->m_pnext=pdel->m_pnext; pmove->m_pnext->m_pprior=pdel->m_pprior; Type temp=pdel->m_data; delete pdel; return temp; } template
Type DoublyList
::Get(int n = 0){ if(n<0){ cout<<"The n is out of boundary"<
*pmove=head; for(int i=0;i
m_pnext; if(pmove==head){ cout<<"The n is out of boundary"<
m_data; } template
void DoublyList
::Print(){ ListNode
*pmove=head->m_pnext; cout<<"head"; while(pmove!=head){ cout<<"--->"<
m_data; pmove=pmove->m_pnext; } cout<<"--->over"<
<
<
ListNode
* DoublyList
::FindData(Type item){ ListNode
*pprior=head->m_pprior,*pnext=head->m_pnext; while(pprior->m_pnext!=pnext && pprior!=pnext){ //find the data in the two direction if(pprior->m_data==item){ return pprior; } if(pnext->m_data==item){ return pnext; } pprior=pprior->m_pprior; pnext=pnext->m_pnext; } cout<<"can't find the element"<
#include "DoublyList.h" using namespace std; int main() { DoublyList
list; for(int i=0;i<20;i++){ list.Insert(i*3,i); } cout<<"the Length of the list is "<
<
class CircularList; template
class ListNode{ private: friend class CircularList
; ListNode():m_pnext(NULL){} ListNode(const Type item,ListNode
*next=NULL):m_data(item),m_pnext(next){} ~ListNode(){ m_pnext=NULL; } private: Type m_data; ListNode *m_pnext; }; CircularList.h #include "ListNode.h" template
class CircularList{ public: CircularList():head(new ListNode
()){ head->m_pnext=head; } ~CircularList(){ MakeEmpty(); delete head; } public: void MakeEmpty(); //clear the list int Length(); //get the length ListNode
*Find(Type value,int n); //find the 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 into the nth data of the list Type Remove(int n=0); //delete the nth data bool RemoveAll(Type item); //delete all the datas which are equal to value Type Get(int n); //get the nth data void Print(); //print the list private: ListNode
*head; }; template
void CircularList
::MakeEmpty(){ ListNode
*pdel,*pmove=head; while(pmove->m_pnext!=head){ pdel=pmove->m_pnext; pmove->m_pnext=pdel->m_pnext; delete pdel; } } template
int CircularList
::Length(){ ListNode
*pmove=head; int count=0; while(pmove->m_pnext!=head){ pmove=pmove->m_pnext; count++; } return count; } template
ListNode
* CircularList
::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==head){ cout<<"The n is out of boundary"<
ListNode
* CircularList
::Find(Type value,int n){ if(n<1){ cout<<"The n is illegal"<
*pmove=head; int count=0; while(count!=n){ pmove=pmove->m_pnext; if(pmove->m_data==value){ count++; } if(pmove==head){ cout<<"can't find the element"<
bool CircularList
::Insert(Type item, int n){ if(n<0){ cout<<"The n is out of boundary"<
*pmove=head; ListNode
*pnode=new ListNode
(item); if(pnode==NULL){ cout<<"Application error!"<
m_pnext; if(pmove==head){ cout<<"The n is out of boundary"<
m_pnext=pmove->m_pnext; pmove->m_pnext=pnode; return 1; } template
bool CircularList
::RemoveAll(Type item){ ListNode
*pmove=head; ListNode
*pdel=head->m_pnext; while(pdel!=head){ 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 CircularList
::Remove(int n){ if(n<0){ cout<<"can't find the element"<
*pmove=head,*pdel; for(int i=0;i
m_pnext!=head;i++){ pmove=pmove->m_pnext; } if(pmove->m_pnext==head){ 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 CircularList
::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(pmove==head){ cout<<"The n is out of boundary"<
m_data; } template
void CircularList
::Print(){ ListNode
*pmove=head->m_pnext; cout<<"head"; while(pmove!=head){ cout<<"--->"<
m_data; pmove=pmove->m_pnext; } cout<<"--->over"<
<
<
#include "CircularList.h" using namespace std; int main() { CircularList
list; for(int i=0;i<20;i++){ list.Insert(i*3,i); } cout<<"the Length of the list is "<
<
class SeqStack{ public: SeqStack(int sz):m_ntop(-1),m_nMaxSize(sz){ m_pelements=new Type[sz]; if(m_pelements==NULL){ cout<<"Application Error!"<
void SeqStack
::Push(const Type item){ if(IsFull()){ cout<<"The stack is full!"<
Type SeqStack
::Pop(){ if(IsEmpty()){ cout<<"There is no element!"<
Type SeqStack
::GetTop() const{ if(IsEmpty()){ cout<<"There is no element!"<
void SeqStack
::Print(){ cout<<"bottom"; for(int i=0;i<=m_ntop;i++){ cout<<"--->"<
top"<
<
<
using namespace std; #include "SeqStack.h" int main(){ SeqStack
stack(10); int init[10]={1,2,6,9,0,3,8,7,5,4}; for(int i=0;i<10;i++){ stack.Push(init[i]); } stack.Print(); stack.Push(88); cout<
<
class LinkStack; template
class StackNode{ private: friend class LinkStack
; StackNode(Type dt,StackNode
*next=NULL):m_data(dt),m_pnext(next){} private: Type m_data; StackNode
*m_pnext; }; LinkStack.h #include "StackNode.h" template
class LinkStack{ public: LinkStack():m_ptop(NULL){} ~LinkStack(){ MakeEmpty(); } public: void MakeEmpty(); //make the stack empty void Push(const Type item); //push the data Type Pop(); //pop the data Type GetTop() const; //get the data void Print(); //print the stack bool IsEmpty() const{ return m_ptop==NULL; } private: StackNode
*m_ptop; }; template
void LinkStack
::MakeEmpty(){ StackNode
*pmove; while(m_ptop!=NULL){ pmove=m_ptop; m_ptop=m_ptop->m_pnext; delete pmove; } } template
void LinkStack
::Push(const Type item){ m_ptop=new StackNode
(item,m_ptop); } template
Type LinkStack
::GetTop() const{ if(IsEmpty()){ cout<<"There is no elements!"<
m_data; } template
Type LinkStack
::Pop(){ if(IsEmpty()){ cout<<"There is no elements!"<
*pdel=m_ptop; m_ptop=m_ptop->m_pnext; Type temp=pdel->m_data; delete pdel; return temp; } template
void LinkStack
::Print(){ StackNode
*pmove=m_ptop; cout<<"buttom"; while(pmove!=NULL){ cout<<"--->"<
m_data; pmove=pmove->m_pnext; } cout<<"--->top"<
<
<
using namespace std; #include "LinkStack.h" int main(){ LinkStack
stack; int init[10]={1,3,5,7,4,2,8,0,6,9}; for(int i=0;i<10;i++){ stack.Push(init[i]); } stack.Print(); cout<
<
class SeqQueue{ public: SeqQueue(int sz):m_nrear(0),m_nfront(0),m_ncount(0),m_nMaxSize(sz){ m_pelements=new Type[sz]; if(m_pelements==NULL){ cout<<"Application Error!"<
void SeqQueue
::MakeEmpty(){ this->m_ncount=0; this->m_nfront=0; this->m_nrear=0; } template
bool SeqQueue
::IsEmpty(){ return m_ncount==0; } template
bool SeqQueue
::IsFull(){ return m_ncount==m_nMaxSize; } template
bool SeqQueue
::Append(const Type item){ if(IsFull()){ cout<<"The queue is full!"<
Type SeqQueue
::Delete(){ if(IsEmpty()){ cout<<"There is no element!"<
Type SeqQueue
::Get(){ if(IsEmpty()){ cout<<"There is no element!"<
void SeqQueue
::Print(){ cout<<"front"; for(int i=0;i
"<
rear"<
<
<
using namespace std; #include "SeqQueue.h" int main(){ SeqQueue
queue(10); int init[10]={1,6,9,0,2,5,8,3,7,4}; for(int i=0;i<5;i++){ queue.Append(init[i]); } queue.Print(); cout<
<
class LinkQueue; template
class QueueNode{ private: friend class LinkQueue
; QueueNode(const Type item,QueueNode
*next=NULL) :m_data(item),m_pnext(next){} private: Type m_data; QueueNode
*m_pnext; }; LinkQueue.h #include "QueueNode.h" template
class LinkQueue{ public: LinkQueue():m_prear(NULL),m_pfront(NULL){} ~LinkQueue(){ MakeEmpty(); } void Append(const Type item); //insert data Type Delete(); //delete data Type GetFront(); //get data void MakeEmpty(); //make the queue empty void Print(); //print the queue bool IsEmpty() const{ return m_pfront==NULL; } private: QueueNode
*m_prear,*m_pfront; }; template
void LinkQueue
::MakeEmpty(){ QueueNode
*pdel; while(m_pfront){ pdel=m_pfront; m_pfront=m_pfront->m_pnext; delete pdel; } } template
void LinkQueue
::Append(const Type item){ if(m_pfront==NULL){ m_pfront=m_prear=new QueueNode
(item); } else{ m_prear=m_prear->m_pnext=new QueueNode
(item); } } template
Type LinkQueue
::Delete(){ if(IsEmpty()){ cout<<"There is no element!"<
*pdel=m_pfront; Type temp=m_pfront->m_data; m_pfront=m_pfront->m_pnext; delete pdel; return temp; } template
Type LinkQueue
::GetFront(){ if(IsEmpty()){ cout<<"There is no element!"<
m_data; } template
void LinkQueue
::Print(){ QueueNode
*pmove=m_pfront; cout<<"front"; while(pmove){ cout<<"--->"<
m_data; pmove=pmove->m_pnext; } cout<<"--->rear"<
<
<
using namespace std; #include "LinkQueue.h" int main(){ LinkQueue
queue; int init[10]={1,3,6,8,9,2,0,5,4,7}; for(int i=0;i<10;i++){ queue.Append(init[i]); } queue.Print(); queue.Delete(); queue.Print(); cout<
<
class PriorityQueue; template
class QueueNode{ private: friend class PriorityQueue
; QueueNode(const Type item,QueueNode
*next=NULL) :m_data(item),m_pnext(next){} private: Type m_data; QueueNode
*m_pnext; }; Compare.h template
class Compare{ //处理一般比较大小 public: static bool lt(Type item1,Type item2); }; template
bool Compare
::lt(Type item1, Type item2){ return item1
class PriorityQueue{ //Cmp is Designed for compare public: PriorityQueue():m_prear(NULL),m_pfront(NULL){} ~PriorityQueue(){ MakeEmpty(); } void MakeEmpty(); //make the queue empty void Append(const Type item); //insert data Type Delete(); //delete data Type GetFront(); //get data void Print(); //print the queue bool IsEmpty() const{ return m_pfront==NULL; } private: QueueNode
*m_prear,*m_pfront; }; template
void PriorityQueue
::MakeEmpty(){ QueueNode
*pdel; while(m_pfront){ pdel=m_pfront; m_pfront=m_pfront->m_pnext; delete pdel; } } template
void PriorityQueue
::Append(const Type item){ if(m_pfront==NULL){ m_pfront=m_prear=new QueueNode
(item); } else{ m_prear=m_prear->m_pnext=new QueueNode
(item); } } template
Type PriorityQueue
::Delete(){ if(IsEmpty()){ cout<<"There is no elements!"<
*pdel=m_pfront,*pmove=m_pfront; while(pmove->m_pnext){ //get the minimize priority's data //cmp:: lt is used for compare the two data, if the front one // is less than the back, then return 1 if(Cmp::lt(pmove->m_pnext->m_data,pdel->m_pnext->m_data)){ pdel=pmove; } pmove=pmove->m_pnext; } pmove=pdel; pdel=pdel->m_pnext; pmove->m_pnext=pdel->m_pnext; Type temp=pdel->m_data; delete pdel; return temp; } template
Type PriorityQueue
::GetFront(){ if(IsEmpty()){ cout<<"There is no elements!"<
*pdel=m_pfront,*pmove=m_pfront->m_pnext; while(pmove){ //get the minimize priority's data if(Cmp::lt(pmove->m_data,pdel->m_data)){ pdel=pmove; } pmove=pmove->m_pnext; } return pdel->m_data; } template
void PriorityQueue
::Print(){ QueueNode
*pmove=m_pfront; cout<<"front"; while(pmove){ cout<<"--->"<
m_data; pmove=pmove->m_pnext; } cout<<"--->rear"<
<
<
#include
using namespace std; #include "PriorityQueue.h" int main(){ PriorityQueue
> queue; int init[10]={1,9,3,5,0,8,2,4,6,7}; for(int i=0;i<10;i++){ queue.Append(init[i]); } queue.Print(); queue.Delete(); queue.Print(); system("pause"); system("cls"); PriorityQueue
spe_queue; int init2[5][2]={{34,2},{64,1},{18,3},{24,2},{55,4}}; SpecialData data[5]; for(int i=0;i<5;i++){ data[i].m_npir=init2[i][1]; data[i].m_ntenor=init2[i][0]; } for(int i=0;i<5;i++){ spe_queue.Append(data[i]); } spe_queue.Print(); cout<
<
<
(const CMyString cmp_str) const; bool operator!() const{ return m_ncurlen==0; } CMyString& operator=(const CMyString ©); CMyString& operator+=(const CMyString &add); char& operator[](int i); friend ostream& operator<<(ostream& ,CMyString&); friend istream& operator>>(istream& ,CMyString&); private: void Next(); private: int m_ncurlen; char *m_pstr; int *m_pnext; }; MyString.cpp #include
#include
using namespace std; #include "MyString.h" CMyString::CMyString(){ //create empty string m_pstr=new char[MAXSIZE+1]; if(!m_pstr){ cerr<<"Allocation Error"<
m_ncurlen=0; m_pstr[0]='\0'; } CMyString::CMyString(const char *init){ //initialize the string with char* m_pstr=new char[MAXSIZE+1]; if(!m_pstr){ cerr<<"Allocation Error"<
m_ncurlen=strlen(init); strcpy(m_pstr,init); } CMyString::CMyString(const CMyString ©){ //initialize the string with string m_pstr=new char[MAXSIZE+1]; if(!m_pstr){ cerr<<"Allocation Error"<
m_ncurlen=copy.m_ncurlen; strcpy(m_pstr,copy.m_pstr); } int CMyString::Find(CMyString part) const{ //string match :KMP int posP=0,posT=0; int lengthP=part.m_ncurlen,lengthT=this->m_ncurlen; part.Next(); while(posP
m_pstr[posT]){ posP++; posT++; } else{ if(posP==0){ posT++; } else{ posP=part.m_pnext[posP-1]; } } } delete[] part.m_pnext; if(posP
m_ncurlen; this->m_pnext=new int[length]; this->m_pnext[0]=0; for(int i=1;i
m_pnext[i-1]; while(*(this->m_pstr+i)!=*(this->m_pstr+j)&&j>0){ j=this->m_pnext[j-1]; } if(*(this->m_pstr+i)==*(this->m_pstr+j)){ this->m_pnext[i]=j+1; } else{ this->m_pnext[i]=0; } } // for(int i=0;i
m_pstr; } CMyString& CMyString::operator()(int pos, int len){ //get len char with the begining of pos CMyString *temp=new CMyString; if(pos<0||pos+len-1>MAXSIZE||len<0){ temp->m_ncurlen=0; temp->m_pstr[0]='\0'; } else{ if(pos+len-1>=m_ncurlen){ len=m_ncurlen-pos; } temp->m_ncurlen=len; for(int i=0,j=pos;i
m_pstr[i]=m_pstr[j]; } temp->m_pstr[len]='\0'; } return *temp; } bool CMyString::operator==(const CMyString cmp_str) const{ if(this->m_ncurlen!=cmp_str.m_ncurlen){ return 0; } for(int i=0;i
m_ncurlen;i++){ if(this->m_pstr[i]!=cmp_str.m_pstr[i]) return 0; } return 1; } bool CMyString::operator!=(const CMyString cmp_str) const{ if(*this==cmp_str) return 0; return 1; } bool CMyString::operator<(const CMyString cmp_str) const{ if(this->m_ncurlen!=cmp_str.m_ncurlen){ return this->m_ncurlen
m_ncurlen;i++){ if(this->m_pstr[i]!=cmp_str.m_pstr[i]){ return this->m_pnext[i]
(const CMyString cmp_str) const{ if(*this
m_pstr; this->m_pstr=new char[copy.m_ncurlen+1]; strcpy (this->m_pstr,copy.m_pstr); return *this; } CMyString& CMyString::operator+=(const CMyString &add){ //字符串追加 int length=this->m_ncurlen+add.m_ncurlen; int n=this->m_ncurlen; CMyString temp(*this); delete[] this->m_pstr; this->m_pstr=new char[length+1]; for(int i=0;i
m_pstr[i]=temp[i]; } for(int i=n;i
m_pstr[i]=add.m_pstr[i-n]; } this->m_pstr[length]='\0'; return *this; } char& CMyString::operator[](int i){ //取元素 if(i<0||i>=this->m_ncurlen){ cout<<"out of boundary!"<
m_pstr[i]; } ostream& operator<<(ostream& os,CMyString& str){ os<
>(istream& is,CMyString& str){ is>>str.m_pstr; return is; } test.cpp #include
using namespace std; #include "MyString.h" int main(){ CMyString test1("babc"); CMyString test2("abababcdefb"); cout<
<
test2){ cout<
<<">"<
<
class BinaryTree; template
class BinTreeNode{ public: friend class BinaryTree
; BinTreeNode():m_pleft(NULL),m_pright(NULL){} BinTreeNode(Type item,BinTreeNode
*left=NULL,BinTreeNode
*right=NULL) :m_data(item),m_pleft(left),m_pright(right){} Type GetData() const; //get thd data BinTreeNode
*GetLeft() const; //get the left node BinTreeNode
*GetRight() const; //get the right node void SetData(const Type data); //change the data void SetLeft(const BinTreeNode
*left); //change thd left node void SetRight(const BinTreeNode
*right); //change the right node void InOrder(); //inorder the tree with the root of the node void PreOrder(); //perorder the tree with the root of the node void PostOrder(); //postoder the tree with the root of the node int Size(); //get size int Height(); //get height BinTreeNode
*Copy(const BinTreeNode
*copy); //copy the node void Destroy(){ //destroy the tree with the root of the node if(this!=NULL){ this->m_pleft->Destroy(); this->m_pright->Destroy(); delete this; } } friend bool equal
(const BinTreeNode
*s,const BinTreeNode
*t); //is equal? private: BinTreeNode
*m_pleft,*m_pright; Type m_data; }; template
Type BinTreeNode
::GetData() const{ return this!=NULL?m_data:-1; } template
BinTreeNode
* BinTreeNode
::GetLeft() const{ return this!=NULL?m_pleft:NULL; } template
BinTreeNode
* BinTreeNode
::GetRight() const{ return this!=NULL?m_pright:NULL; } template
void BinTreeNode
::SetData(const Type data){ if(this!=NULL){ m_data=data; } } template
void BinTreeNode
::SetLeft(const BinTreeNode
*left){ if(this!=NULL){ m_pleft=left; } } template
void BinTreeNode
::SetRight(const BinTreeNode
*right){ if(this!=NULL){ m_pright=right; } } template
BinTreeNode
* BinTreeNode
::Copy(const BinTreeNode
*copy){ if(copy==NULL){ return NULL; } BinTreeNode
*temp=new BinTreeNode
(copy->m_data); temp->m_pleft=Copy(copy->m_pleft); temp->m_pright=Copy(copy->m_pright); return temp; } template
bool equal(const BinTreeNode
*s,const BinTreeNode
*t){ if(s==NULL&&t==NULL){ return 1; } if(s&&t&&s->m_data==t->m_data&&equal(s->m_pleft,t->m_pleft)&&equal(s->m_pright,t->m_pright)){ return 1; } return 0; } template
void BinTreeNode
::InOrder(){ if(this!=NULL){ this->m_pleft->InOrder(); cout<<"--->"<
m_data; this->m_pright->InOrder(); } } template
void BinTreeNode
::PreOrder(){ if(this!=NULL){ cout<<"--->"<
m_data; this->m_pleft->PreOrder(); this->m_pright->PreOrder(); } } template
void BinTreeNode
::PostOrder(){ if(this!=NULL){ this->m_pleft->PostOrder(); this->m_pright->PostOrder(); cout<<"--->"<
m_data; } } template
int BinTreeNode
::Size(){ if(this==NULL){ return 0; } return 1+this->m_pleft->Size()+this->m_pright->Size(); } template
int BinTreeNode
::Height(){ if(this==NULL){ return -1; } int lheight,rheight; lheight=this->m_pleft->Height(); rheight=this->m_pright->Height(); return 1+(lheight>rheight?lheight:rheight); } BinaryTree.h #include "BinTreeNode.h" template
class BinaryTree{ public: BinaryTree():m_proot(NULL){} BinaryTree(const Type stop):m_stop(stop),m_proot(NULL){} BinaryTree(BinaryTree
& copy); virtual ~BinaryTree(){ m_proot->Destroy(); } virtual bool IsEmpty(){ //is empty? return m_proot==NULL; } virtual BinTreeNode
*GetLeft(BinTreeNode
*current); //get the left node virtual BinTreeNode
*GetRight(BinTreeNode
*current);//get the right node virtual BinTreeNode
*GetParent(BinTreeNode
*current);//ghe thd parent const BinTreeNode
*GetRoot() const; //get root virtual bool Insert(const Type item); //insert a new node virtual BinTreeNode
*Find(const Type item) const; //find thd node with the data void InOrder(); void PreOrder(); void PostOrder(); int Size(); //get size int Height(); //get height BinaryTree
& operator=(const BinaryTree
copy); //evaluate node friend bool operator==
(const BinaryTree
s,const BinaryTree
t);//is equal? friend ostream& operator<<
(ostream& ,BinaryTree
&); //output the data friend istream& operator>>
(istream& ,BinaryTree
&); //input the data private: Type m_stop; //just using for input the data; BinTreeNode
*m_proot; //find the parent of current in the tree with the root of start BinTreeNode
*GetParent(BinTreeNode
*start,BinTreeNode
*current); void Print(BinTreeNode
*start,int n=0); //print the tree with the root of start }; template
BinaryTree
::BinaryTree(BinaryTree
& copy){ if(copy.m_proot){ this->m_stop=copy.m_stop; } m_proot=m_proot->Copy(copy.m_proot); } template
BinTreeNode
* BinaryTree
::GetLeft(BinTreeNode
*current){ return m_proot&¤t?current->m_pleft:NULL; } template
BinTreeNode
* BinaryTree
::GetRight(BinTreeNode
*current){ return m_proot&¤t?current->m_pright:NULL; } template
const BinTreeNode
* BinaryTree
::GetRoot() const{ return m_proot; } template
BinTreeNode
* BinaryTree
::GetParent(BinTreeNode
*start, BinTreeNode
*current){ if(start==NULL||current==NULL){ return NULL; } if(start->m_pleft==current||start->m_pright==current){ return start; } BinTreeNode
*pmove; if((pmove=GetParent(start->m_pleft,current))!=NULL){//find the parent in the left subtree return pmove; } else{ return GetParent(start->m_pright,current); //find the parent in the right subtree } } template
BinTreeNode
* BinaryTree
::GetParent(BinTreeNode
*current){ return m_proot==NULL||current==m_proot?NULL:GetParent(m_proot,current); } template
bool BinaryTree
::Insert(const Type item){ BinTreeNode
*pstart=m_proot,*newnode=new BinTreeNode
(item); if(m_proot==NULL){ m_proot=newnode; return 1; } while(1){ if(item==pstart->m_data){ cout<<"The item "<
<<" is exist!"<
m_data){ if(pstart->m_pleft==NULL){ pstart->m_pleft=newnode; return 1; } pstart=pstart->m_pleft; //if less than the node then insert to the left subtree } else{ if(pstart->m_pright==NULL){ pstart->m_pright=newnode; return 1; } pstart=pstart->m_pright;//if more than the node then insert to the right subtree } } } template
BinTreeNode
* BinaryTree
::Find(const Type item) const{ BinTreeNode
*pstart=m_proot; while(pstart){ if(item==pstart->m_data){ return pstart; } if(item
m_data){ pstart=pstart->m_pleft; //if less than the node then find in the left subtree } else{ pstart=pstart->m_pright;//if more than the node then find in the right subtree } } return NULL; } template
void BinaryTree
::Print(BinTreeNode
*start, int n){ if(start==NULL){ for(int i=0;i
m_pright,n+1); //print the right subtree for(int i=0;i
m_pleft,n+1); //print the left subtree } template
BinaryTree
& BinaryTree
::operator=(const BinaryTree
copy){ if(copy.m_proot){ this->m_stop=copy.m_stop; } m_proot=m_proot->Copy(copy.m_proot); return *this; } template
ostream& operator<<(ostream& os,BinaryTree
& out){ out.Print(out.m_proot); return os; } template
istream& operator>>(istream& is,BinaryTree
& in){ Type item; cout<<"initialize the tree:"<
<<"Input data(end with "<
<<"!):"; is>>item; while(item!=in.m_stop){ //m_stop is the end of input in.Insert(item); is>>item; } return is; } template
bool operator==(const BinaryTree
s,const BinaryTree
t){ return equal(s.m_proot,t.m_proot); } template
void BinaryTree
::InOrder(){ this->m_proot->InOrder(); } template
void BinaryTree
::PreOrder(){ this->m_proot->PreOrder(); } template
void BinaryTree
::PostOrder(){ this->m_proot->PostOrder(); } template
int BinaryTree
::Size(){ return this->m_proot->Size(); } template
int BinaryTree
::Height(){ return this->m_proot->Height(); } Test.cpp #include
using namespace std; #include "BinaryTree.h" int main(){ BinaryTree
tree(-1); // int init[10]={3,6,0,2,8,4,9,1,5,7}; int init[30]={17,6,22,29,14,0,21,13,27,18,2,28,8 ,26,3,12,20,4,9,23,15,1,11,5,19,24,16,7,10,25}; for(int i=0;i<30;i++){ tree.Insert(init[i]); } //cin>>tree; cout<
<
GetData()<
GetRight()->GetData()<
class ThreadInorderIterator; template
class ThreadTree{ public: friend class ThreadInorderIterator
; ThreadTree():m_proot(new ThreadNode
()){} ThreadInorderIterator.h #include "ThreadTree.h" template
class ThreadInorderIterator{ public: ThreadInorderIterator(ThreadTree
&tree):m_ptree(tree),m_pcurrent(tree.m_proot){ //InThread(m_ptree.m_proot->m_pleft,m_ptree.m_proot); } ThreadNode
*First(); ThreadNode
*Prior(); ThreadNode
*Next(); void Print(); void Print(ThreadNode
*start, int n=0); void InOrder(); void InsertLeft(ThreadNode
*left); void InsertRight(ThreadNode
*right); ThreadNode
*GetParent(ThreadNode
*current); private: ThreadTree
&m_ptree; ThreadNode
*m_pcurrent; void InThread(ThreadNode
*current,ThreadNode
*pre); }; template
void ThreadInorderIterator
::InThread( ThreadNode
*current, ThreadNode
*pre){ if(current!=m_ptree.m_proot){ InThread(current->m_pleft,pre); if(current->m_pleft==NULL){ current->m_pleft=pre; current->m_nleftthread=1; } if(pre->m_pright==NULL){ pre->m_pright=current; pre->m_nrightthread=1; } pre=current; InThread(current->m_pright,pre); } } template
ThreadNode
* ThreadInorderIterator
::First(){ while(m_pcurrent->m_nleftthread==0){ m_pcurrent=m_pcurrent->m_pleft; } return m_pcurrent; } template
ThreadNode
* ThreadInorderIterator
::Prior(){ ThreadNode
*pmove=m_pcurrent->m_pleft; if(0==m_pcurrent->m_nleftthread){ while(0==pmove->m_nrightthread){ pmove=pmove->m_pright; } } m_pcurrent=pmove; if(m_pcurrent==m_ptree.m_proot){ return NULL; } return m_pcurrent; } template
ThreadNode
* ThreadInorderIterator
::Next(){ ThreadNode
*pmove=m_pcurrent->m_pright; if(0==m_pcurrent->m_nrightthread){ while(0==pmove->m_nleftthread){ pmove=pmove->m_pleft; } } m_pcurrent=pmove; if(m_pcurrent==m_ptree.m_proot){ return NULL; } return m_pcurrent; } template
void ThreadInorderIterator
::InOrder(){ ThreadNode
*pmove=m_ptree.m_proot; while(pmove->m_pleft!=m_ptree.m_proot){ pmove=pmove->m_pleft; } m_pcurrent=pmove; cout<<"root"; while(pmove!=m_ptree.m_proot&&pmove){ cout<<"--->"<
m_data; pmove=this->Next(); } cout<<"--->end"; } template
void ThreadInorderIterator
::InsertLeft(ThreadNode
*left){ left->m_pleft=m_pcurrent->m_pleft; left->m_nleftthread=m_pcurrent->m_nleftthread; left->m_pright=m_pcurrent; left->m_nrightthread=1; m_pcurrent->m_pleft=left; m_pcurrent->m_nleftthread=0; if(0==left->m_nleftthread){ m_pcurrent=left->m_pleft; ThreadNode
*temp=First(); temp->m_pright=left; } m_pcurrent=left; } template
void ThreadInorderIterator
::InsertRight(ThreadNode
*right){ right->m_pright=m_pcurrent->m_pright; right->m_nrightthread=m_pcurrent->m_nrightthread; right->m_pleft=m_pcurrent; right->m_nleftthread=1; m_pcurrent->m_pright=right; m_pcurrent->m_nrightthread=0; if(0==right->m_nrightthread){ m_pcurrent=right->m_pright; ThreadNode
*temp=First(); temp->m_pleft=right; } m_pcurrent=right; } template
ThreadNode
* ThreadInorderIterator
::GetParent( ThreadNode
*current){ ThreadNode
*pmove=current; while(0==pmove->m_nleftthread){ pmove=pmove->m_pleft; } pmove=pmove->m_pleft; if(pmove==m_ptree.m_proot){ if(pmove->m_pleft==current){ return NULL; } } if(pmove->m_pright==current){ return pmove; } pmove=pmove->m_pright; while(pmove->m_pleft!=current){ pmove=pmove->m_pleft; } return pmove; } template
void ThreadInorderIterator
::Print(ThreadNode
*start, int n){ if(start->m_nleftthread&&start->m_nrightthread){ for(int i=0;i
m_nrightthread==0){ Print(start->m_pright,n+1); } for(int i=0;i
m_nleftthread==0){ Print(start->m_pleft,n+1); } } template
void ThreadInorderIterator
::Print(){ Print(m_ptree.m_proot->m_pleft); } test.cpp #include
using namespace std; #include "ThreadInorderIterator.h" int main(){ ThreadTree
tree; ThreadInorderIterator
threadtree(tree); int init[10]={3,6,0,2,8,4,9,1,5,7}; for(int i=0;i<10;){ threadtree.InsertLeft(new ThreadNode
(init[i++])); threadtree.InsertRight(new ThreadNode
(init[i++])); } threadtree.Print(); cout<
<
*m_proot; }; 13、堆 MinHeap.h template
class MinHeap{ public: MinHeap(int size):m_nMaxSize(size > defaultsize ? size : defaultsize) ,m_pheap(new Type[m_nMaxSize]),m_ncurrentsize(0){} MinHeap(Type heap[],int n); //initialize heap by a array ~MinHeap(){ delete[] m_pheap; } public: bool Insert(const Type item); //insert element bool Delete(const Type item); //delete element bool IsEmpty() const{ return m_ncurrentsize == 0; } bool IsFull() const{ reutrn m_ncurrentsize == m_nMaxSize; } void Print(const int start=0, int n=0); private: //adjust the elements of the child tree with the root of start from top to bottom void Adjust(const int start, const int end); private: static const int defaultsize = 100; const int m_nMaxSize; Type *m_pheap; int m_ncurrentsize; }; template
void MinHeap
::Adjust(const int start, const int end){ int i = start,j = i*2+1; //get the position of the child of i Type temp=m_pheap[i]; while(j <= end){ if(j
m_pheap[j+1]){ //left>right j++; } if(temp <= m_pheap[j]){ //adjust over break; } else{ //change the parent and the child, then adjust the child m_pheap[i] = m_pheap[j]; i = j; j = 2*i+1; } } m_pheap[i] = temp; } template
MinHeap
::MinHeap(Type heap[], int n):m_nMaxSize( n > defaultsize ? n : defaultsize){ m_pheap = new Type[m_nMaxSize]; for(int i=0; i
=0){ Adjust(pos, n-1); pos--; } } template
bool MinHeap
::Insert(const Type item){ if(m_ncurrentsize == m_nMaxSize){ cerr<<"Heap Full!"<
0){ //adjust from bottom to top if(m_pheap[i] <= temp){ break; } else{ m_pheap[j] = m_pheap[i]; j = i; i = (j-1)/2; } } m_pheap[j] = temp; m_ncurrentsize++; return 1; } template
bool MinHeap
::Delete(const Type item){ if(0 == m_ncurrentsize){ cerr<<"Heap Empty!"<
void MinHeap
::Print(const int start, int n){ if(start >= m_ncurrentsize){ return; } Print(start*2+2, n+1); //print the right child tree for(int i=0; i
" << endl; Print(start*2+1, n+1); //print the left child tree } test.cpp #include
using namespace std; #include "MinHeap.h" int main(){ int init[30]={17,6,22,29,14,0,21,13,27,18,2,28,8 ,26,3,12,20,4,9,23,15,1,11,5,19,24,16,7,10,25}; MinHeap
heap(init,30); heap.Print(); cout<
<
<
class BinaryTree; template
void Huffman(Type *, int, BinaryTree
&); template
class BinTreeNode{ public: friend class BinaryTree
; friend void Huffman
(Type *, int, BinaryTree
&); BinTreeNode():m_pleft(NULL),m_pright(NULL){} BinTreeNode(Type item,BinTreeNode
*left=NULL,BinTreeNode
*right=NULL) :m_data(item),m_pleft(left),m_pright(right){} void Destroy(){ //destroy the tree with the root of the node if(this!=NULL){ this->m_pleft->Destroy(); this->m_pright->Destroy(); delete this; } } Type GetData(){ return m_data; } BinTreeNode
*Copy(const BinTreeNode
*copy); //copy the node private: BinTreeNode
*m_pleft,*m_pright; Type m_data; }; template
BinTreeNode
* BinTreeNode
::Copy(const BinTreeNode
*copy){ if(copy==NULL){ return NULL; } BinTreeNode
*temp=new BinTreeNode
(copy->m_data); temp->m_pleft=Copy(copy->m_pleft); temp->m_pright=Copy(copy->m_pright); return temp; } BinaryTree.h #include "BinTreeNode.h" template
void Huffman(Type *, int, BinaryTree
&); template
class BinaryTree{ public: BinaryTree(BinaryTree
&bt1, BinaryTree
&bt2){ m_proot = new BinTreeNode
(bt1.m_proot->m_data + bt2.m_proot->m_data, bt1.m_proot, bt2.m_proot); } BinaryTree(Type item){ m_proot = new BinTreeNode
(item); } BinaryTree(const BinaryTree
©){ this->m_proot = copy.m_proot; } BinaryTree(){ m_proot = NULL; } void Destroy(){ m_proot->Destroy(); } ~BinaryTree(){ // m_proot->Destroy(); } BinaryTree
& operator=(BinaryTree
copy); //evaluate node friend void Huffman
(Type *, int, BinaryTree
&); friend bool operator <
(BinaryTree
&l, BinaryTree
& r); friend bool operator >
(BinaryTree
&l, BinaryTree
& r); friend bool operator <=
(BinaryTree
&l, BinaryTree
& r); friend ostream& operator<<
(ostream& ,BinaryTree
&); //output the data private: BinTreeNode
*m_proot; void Print(BinTreeNode
*start,int n=0); //print the tree with the root of start }; template
bool operator <(BinaryTree
&l, BinaryTree
&r){ return l.m_proot->GetData() < r.m_proot->GetData(); } template
bool operator >(BinaryTree
&l, BinaryTree
&r){ return l.m_proot->GetData() > r.m_proot->GetData(); } template
bool operator <=(BinaryTree
&l, BinaryTree
&r){ return l.m_proot->GetData() <= r.m_proot->GetData(); } template
void BinaryTree
::Print(BinTreeNode
*start, int n){ if(start==NULL){ for(int i=0;i
m_pright,n+1); //print the right subtree for(int i=0;i
m_pleft,n+1); //print the left subtree } template
ostream& operator<<(ostream& os,BinaryTree
& out){ out.Print(out.m_proot); return os; } template
BinaryTree
& BinaryTree
::operator=(BinaryTree
copy){ m_proot=m_proot->Copy(copy.m_proot); return *this; } MinHeap.h template
class MinHeap{ public: MinHeap(Type heap[],int n); //initialize heap by a array ~MinHeap(){ delete[] m_pheap; } public: bool Insert(const Type item); bool DeleteMin(Type &first); private: void Adjust(const int start, const int end); //adjust the elements from start to end private: const int m_nMaxSize; Type *m_pheap; int m_ncurrentsize; }; template
void MinHeap
::Adjust(const int start, const int end){ int i = start,j = i*2+1; Type temp=m_pheap[i]; while(j <= end){ if(j
m_pheap[j+1]){ j++; } if(temp <= m_pheap[j]){ break; } else{ m_pheap[i] = m_pheap[j]; i = j; j = 2*i+1; } } m_pheap[i] = temp; } template
MinHeap
::MinHeap(Type heap[], int n):m_nMaxSize(n){ m_pheap = new Type[m_nMaxSize]; for(int i=0; i
=0){ Adjust(pos, n-1); pos--; } } template
bool MinHeap
::DeleteMin(Type &first){ first = m_pheap[0]; m_pheap[0] = m_pheap[m_ncurrentsize-1]; m_ncurrentsize--; Adjust(0, m_ncurrentsize-1); return 1; } template
bool MinHeap
::Insert(const Type item){ if(m_ncurrentsize == m_nMaxSize){ cerr<<"Heap Full!"<
0){ if(m_pheap[i] <= temp){ break; } else{ m_pheap[j] = m_pheap[i]; j = i; i = (j-1)/2; } } m_pheap[j] = temp; m_ncurrentsize++; return 1; } Huffman.h #include "BinaryTree.h" #include "MinHeap.h" template
void Huffman(Type *elements, int n, BinaryTree
&tree){ BinaryTree
first, second; BinaryTree
node[20]; for (int i=0; i
(elements[i]); } MinHeap
> heap(node, n); for (int i=0; i
GetData() == second.m_proot->GetData()){ tree = *(new BinaryTree
(second, first)); } else { tree = *(new BinaryTree
(first, second)); } heap.Insert(tree); } } Test.cpp #include
using namespace std; #include "Huffman.h" int main(){ BinaryTree
tree; int init[10]={3,6,0,2,8,4,9,1,5,7}; Huffman(init,10,tree); cout << tree; tree.Destroy(); return 0; } 15、树 QueueNode.h template
class LinkQueue; template
class QueueNode{ private: friend class LinkQueue
; QueueNode(const Type item,QueueNode
*next=NULL) :m_data(item),m_pnext(next){} private: Type m_data; QueueNode
*m_pnext; }; LinkQueue.h #include "QueueNode.h" template
class LinkQueue{ public: LinkQueue():m_prear(NULL),m_pfront(NULL){} ~LinkQueue(){ MakeEmpty(); } void Append(const Type item); Type Delete(); Type GetFront(); void MakeEmpty(); bool IsEmpty() const{ return m_pfront==NULL; } void Print(); private: QueueNode
*m_prear,*m_pfront; }; template
void LinkQueue
::MakeEmpty(){ QueueNode
*pdel; while(m_pfront){ pdel=m_pfront; m_pfront=m_pfront->m_pnext; delete pdel; } } template
void LinkQueue
::Append(const Type item){ if(m_pfront==NULL){ m_pfront=m_prear=new QueueNode
(item); } else{ m_prear=m_prear->m_pnext=new QueueNode
(item); } } template
Type LinkQueue
::Delete(){ if(IsEmpty()){ cout<<"There is no element!"<
*pdel=m_pfront; Type temp=m_pfront->m_data; m_pfront=m_pfront->m_pnext; delete pdel; return temp; } template
Type LinkQueue
::GetFront(){ if(IsEmpty()){ cout<<"There is no element!"<
m_data; } template
void LinkQueue
::Print(){ QueueNode
*pmove=m_pfront; cout<<"front"; while(pmove){ cout<<"--->"<
m_data; pmove=pmove->m_pnext; } cout<<"--->rear"<
<
<
class Tree; template
class TreeNode{ public: friend class Tree
; private: Type m_data; TreeNode
*m_pfirst,*m_pnext; TreeNode():m_pfirst(NULL), m_pnext(NULL){} TreeNode(Type item, TreeNode
*first = NULL, TreeNode
*next = NULL) :m_data(item), m_pfirst(first), m_pnext(next){} }; Tree.h #include "TreeNode.h" #include "LinkQueue.h" template
class Tree{ public: Tree():m_proot(NULL), m_pcurrent(NULL){} public: TreeNode
*GetCurrent(){ //Get the current node return m_pcurrent; } voi
C++ 数据结构与算法
最新推荐文章于 2021-03-28 11:32:13 发布