logo资料库

C++实现数据结构算法.pdf

第1页 / 共166页
第2页 / 共166页
第3页 / 共166页
第4页 / 共166页
第5页 / 共166页
第6页 / 共166页
第7页 / 共166页
第8页 / 共166页
资料共166页,剩余部分请下载后查看
目 录
1、顺序表
Seqlist.h
Test.cpp
2、 单链表
ListNode.h
SingleList.h
test.cpp
3、 双向循环链表
NodeList.h
DoubleList.h
Test.cpp
4、 单项循环链表
ListNode.h
CircularList.h
Test.cpp
5、 顺序栈
SeqStack.h
Test.cpp
6、 链式栈
StackNode.h
LinkStack.h
Test.cpp
7.顺序队列
SeqQueue.h
Test.cpp
8、链式队列
QueueNode.h
LinkQueue.h
Test.cpp
9、优先级队列
QueueNode.h
Compare.h
PriorityQueue.h
Test.cpp
10、串
MyString.h
MyString.cpp
test.cpp
11、二叉树
BinTreeNode.h
BinaryTree.h
Test.cpp
12、线索二叉树
ThreadNode.h
ThreadTree.h
ThreadInorderIterator.h
test.cpp
13、堆
MinHeap.h
test.cpp
14、哈夫曼树
BinTreeNode.h
BinaryTree.h
MinHeap.h
Huffman.h
Test.cpp
15、树
QueueNode.h
LinkQueue.h
TreeNode.h
Tree.h
test.cpp
16、B+树
BTreeNode.h
BTree.h
test.cpp
17、图
MinHeap.h
Edge.h
Vertex.h
Graph.h
test.cpp
18、排序
Data.h
QueueNode.h
LinkQueue.h
Sort.h
test.cpp
目 录 目 录 ...................................................................................................................................................................................................................................................................................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
分享到:
收藏