目 录
目 录 ...................................................................................................................................................................................................................................................................................1
1、顺序表 .............................................................................................................................................................................................................................................................................1
Seqlist.h .................................................................................................................................................................................................................................................................1
Test.cpp ....................................................................................................................................................................................................................................................................4
2、 单链表 .......................................................................................................................................................................................................................................................................5
ListNode.h ...............................................................................................................................................................................................................................................................5
SingleList.h ..........................................................................................................................................................................................................................................................6
test.cpp ..................................................................................................................................................................................................................................................................12
3、 双向循环链表 .........................................................................................................................................................................................................................................................13
NodeList.h .............................................................................................................................................................................................................................................................13
DoubleList.h ........................................................................................................................................................................................................................................................14
Test.cpp ..................................................................................................................................................................................................................................................................20
4、 单项循环链表 .........................................................................................................................................................................................................................................................21
ListNode.h .............................................................................................................................................................................................................................................................21
CircularList.h ...................................................................................................................................................................................................................................................22
Test.cpp ..................................................................................................................................................................................................................................................................28
5、 顺序栈 .....................................................................................................................................................................................................................................................................29
SeqStack.h .............................................................................................................................................................................................................................................................29
Test.cpp ..................................................................................................................................................................................................................................................................32
6、 链式栈 .....................................................................................................................................................................................................................................................................33
StackNode.h ...........................................................................................................................................................................................................................................................33
LinkStack.h ...........................................................................................................................................................................................................................................................33
Test.cpp ..................................................................................................................................................................................................................................................................36
7.顺序队列 .........................................................................................................................................................................................................................................................................37
SeqQueue.h .............................................................................................................................................................................................................................................................37
Test.cpp ..................................................................................................................................................................................................................................................................40
8、链式队列 .......................................................................................................................................................................................................................................................................41
QueueNode.h ...........................................................................................................................................................................................................................................................41
LinkQueue.h ...........................................................................................................................................................................................................................................................42
Test.cpp ..................................................................................................................................................................................................................................................................44
9、优先级队列 ...................................................................................................................................................................................................................................................................45
QueueNode.h ...........................................................................................................................................................................................................................................................46
Compare.h ...............................................................................................................................................................................................................................................................46
PriorityQueue.h .................................................................................................................................................................................................................................................47
Test.cpp ..................................................................................................................................................................................................................................................................51
10、串 .................................................................................................................................................................................................................................................................................52
MyString.h .............................................................................................................................................................................................................................................................52
MyString.cpp ........................................................................................................................................................................................................................................................54
test.cpp ..................................................................................................................................................................................................................................................................60
11、二叉树 .........................................................................................................................................................................................................................................................................61
BinTreeNode.h ......................................................................................................................................................................................................................................................62
BinaryTree.h ........................................................................................................................................................................................................................................................66
Test.cpp ..................................................................................................................................................................................................................................................................73
12、线索二叉树 .................................................................................................................................................................................................................................................................74
ThreadNode.h ........................................................................................................................................................................................................................................................74
ThreadTree.h ........................................................................................................................................................................................................................................................75
ThreadInorderIterator.h ..............................................................................................................................................................................................................................76
test.cpp ..................................................................................................................................................................................................................................................................82
13、堆 .................................................................................................................................................................................................................................................................................83
MinHeap.h ...............................................................................................................................................................................................................................................................83
test.cpp ..................................................................................................................................................................................................................................................................87
14、哈夫曼树 .....................................................................................................................................................................................................................................................................88
BinTreeNode.h ......................................................................................................................................................................................................................................................88
BinaryTree.h ........................................................................................................................................................................................................................................................89
MinHeap.h ...............................................................................................................................................................................................................................................................92
Huffman.h ...............................................................................................................................................................................................................................................................95
Test.cpp ..................................................................................................................................................................................................................................................................96
15、树 .................................................................................................................................................................................................................................................................................97
QueueNode.h ...........................................................................................................................................................................................................................................................97
LinkQueue.h ...........................................................................................................................................................................................................................................................97
TreeNode.h ........................................................................................................................................................................................................................................................... 100
Tree.h ..................................................................................................................................................................................................................................................................... 100
test.cpp ................................................................................................................................................................................................................................................................ 110
16、B+树 ........................................................................................................................................................................................................................................................................... 111
BTreeNode.h ......................................................................................................................................................................................................................................................... 111
BTree.h .................................................................................................................................................................................................................................................................. 113
test.cpp ................................................................................................................................................................................................................................................................ 126
17、图 ............................................................................................................................................................................................................................................................................... 127
MinHeap.h ............................................................................................................................................................................................................................................................. 127
Edge.h ..................................................................................................................................................................................................................................................................... 130
Vertex.h ................................................................................................................................................................................................................................................................ 131
Graph.h .................................................................................................................................................................................................................................................................. 132
test.cpp ................................................................................................................................................................................................................................................................ 144
18、排序 ........................................................................................................................................................................................................................................................................... 145
Data.h ..................................................................................................................................................................................................................................................................... 146
QueueNode.h ......................................................................................................................................................................................................................................................... 149
LinkQueue.h ......................................................................................................................................................................................................................................................... 152
Sort.h ..................................................................................................................................................................................................................................................................... 154
test.cpp ................................................................................................................................................................................................................................................................ 162
数据结构算法实现 2008-9-3
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
1
数据结构算法实现 2008-9-3
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;
}
2
数据结构算法实现 2008-9-3
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数据结构算法实现 2008-9-3
return 1;
}
template
void SeqList::Print(){
for(int i=0;i<=m_ncurrentsize;i++)
cout<
#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<数据结构算法实现 2008-9-3
test.Print();
test.Remove(0);
test.Print();
return 0;
}
2、 单链表
ListNode.h
template 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&);
5