数据结构各种算法实现C++模板.doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《数据结构各种算法实现C++模板.doc》由会员分享,可在线阅读,更多相关《数据结构各种算法实现C++模板.doc(225页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流数据结构各种算法实现C+模板.精品文档.数据结构各种算法实现(C+模板)目 录1、顺序表1Seqlist.h1Test.cpp62、单链表8ListNode.h8SingleList.h10test.cpp203、双向链表22NodeList.h22DoubleList.h24Test.cpp344、循环链表36ListNode.h36CircularList.h37Test.cpp475、顺序栈49SeqStack.h49Test.cpp546、链式栈55StackNode.h55LinkStack.h56Test.cpp607、顺序队列6
2、2SeqQueue.h63Test.cpp688、链式队列70QueueNode.h70LinkQueue.h71Test.cpp759、优先级队列77QueueNode.h77Compare.h78PriorityQueue.h80Test.cpp8510、串88MyString.h88MyString.cpp90test.cpp10111、二叉树104BinTreeNode.h104BinaryTree.h112Test.cpp12412、线索二叉树126ThreadNode.h126ThreadTree.h128ThreadInorderIterator.h128test.cpp1391
3、3、堆140MinHeap.h140test.cpp14714、哈夫曼树149BinTreeNode.h149BinaryTree.h151MinHeap.h156Huffman.h161Test.cpp16315、树164QueueNode.h164LinkQueue.h165TreeNode.h169Tree.h170test.cpp18716、B+树189BTreeNode.h189BTree.h192test.cpp21517、图217MinHeap.h217Edge.h222Vertex.h223Graph.h224test.cpp24618、排序249Data.h249QueueN
4、ode.h255LinkQueue.h259Sort.h263test.cpp2781、顺序表Seqlist.hconst int DefaultSize=100;template class SeqListpublic:SeqList(int sz=DefaultSize):m_nmaxsize(sz),m_ncurrentsize(-1)if(sz0)m_elements=new Typem_nmaxsize;SeqList()delete m_elements;int Length() const/get the lengthreturn m_ncurrentsize+1;int Fin
5、d(Type x) const;/find the position of xint IsElement(Type x) const;/is it in the listint Insert(Type x,int i);/insert dataint Remove(Type x);/delete dataint IsEmpty()return m_ncurrentsize=-1;int IsFull()return m_ncurrentsize=m_nmaxsize-1;Type Get(int i)/get the ith datareturn im_ncurrentsize?(coutca
6、nt find the elementendl,0):m_elementsi;void Print();private:Type *m_elements;const int m_nmaxsize;int m_ncurrentsize;template int SeqList:Find(Type x) constfor(int i=0;im_ncurrentsize;i+)if(m_elementsi=x)return i;coutcant find the element you want to findendl;return -1;template int SeqList:IsElement
7、(Type x) constif(Find(x)=-1)return 0;return 1;template int SeqList:Insert(Type x, int i)if(im_ncurrentsize+1|m_ncurrentsize=m_nmaxsize-1)coutthe operate is illegali;j-)m_elementsj=m_elementsj-1;m_elementsi=x;return 1;template int SeqList:Remove(Type x)int size=m_ncurrentsize;for(int i=0;im_ncurrents
8、ize;)if(m_elementsi=x)for(int j=i;jm_ncurrentsize;j+)m_elementsj=m_elementsj+1;m_ncurrentsize-;continue;i+;if(size=m_ncurrentsize)coutcant find the element you want to removeendl;return 0;return 1;template void SeqList:Print()for(int i=0;i=m_ncurrentsize;i+)couti+1:tm_elementsiendl;coutendlendl;Test
9、.cpp#include #include SeqList.husing namespace std;int main()SeqList test(15);int array15=2,5,8,1,9,9,7,6,4,3,2,9,7,7,9;for(int i=0;i15;i+)test.Insert(arrayi,0);test.Insert(1,0);cout(test.Find(0)?cant be found :Be found ) 0 endlendl;test.Remove(7);test.Print();test.Remove(9);test.Print();test.Remove
10、(0);test.Print();return 0;2、 单链表ListNode.htemplate class SingleList;template class ListNodeprivate: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
11、& ,ListNode&);private:Type m_data;ListNode *m_pnext;template Type ListNode:GetData()return this-m_data;template ostream& operator(ostream& os,ListNode& out)osout.m_data;return os;SingleList.h#include ListNode.htemplate class SingleListpublic:SingleList():head(new ListNode()SingleList()MakeEmpty();de
12、lete head;public:void MakeEmpty(); /make the list emptyint Length(); /get the lengthListNode *Find(Type value,int n); /find thd nth data which is equal to valueListNode *Find(int n); /find the nth databool Insert(Type item,int n=0); /insert the data in the nth positionType Remove(int n=0); /remove t
13、he nth databool RemoveAll(Type item); /remove all the data which is equal to itemType Get(int n); /get the nth datavoid 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;t
14、emplate 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(n0)coutThe n is out of boundaryendl;return NULL;ListNode *pmove=head-m_pnext;for(int i=0;im_pnext;if(pmove=NULL)coutThe n is o
15、ut of boundaryendl;return NULL;return pmove;template ListNode* SingleList:Find(Type value,int n)if(n1)coutThe n is illegalendl;return NULL;ListNode *pmove=head;int count=0;while(count!=n&pmove)pmove=pmove-m_pnext;if(pmove-m_data=value)count+;if(pmove=NULL)coutcant find the elementendl;return NULL;re
16、turn pmove;template bool SingleList:Insert(Type item, int n)if(n0)coutThe n is illegalendl;return 0;ListNode *pmove=head;ListNode *pnode=new ListNode(item);if(pnode=NULL)coutApplication error!endl;return 0;for(int i=0;im_pnext;if(pmove=NULL)coutthe n is illegalm_pnext=pmove-m_pnext;pmove-m_pnext=pno
17、de;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(n0
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 各种 算法 实现 C+ 模板
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内