用C++实现数据结构中的各种算法.docx
![资源得分’ 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++实现数据结构中的各种算法.docx》由会员分享,可在线阅读,更多相关《用C++实现数据结构中的各种算法.docx(221页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、目求11、顺序表1Seqlist .h1Test. cpp42单链表5ListNode .h5SingleList .h6test. cpp12双向循环链表13NodeList .h13DoubleList. h14Test. cpp204单项循环链表21ListNode .h21CircularList .h22Test . cpp28顺序栈29SeqStack.h29Test. cpp326 链式栈33StackNode . h33Linkstack. h33Test. cpp367 .顺序队列37SeqQueue .h37Test . cpp40*、! 0 rlQueueNode. h4
2、1LinkQueue . h42Test cpp44QueueNode. h46Priori tyQueue. h47ltt串52MyString. cpp54l二叉树61BinTreeNode . h62BinaryTree . h66ThreadNodu . h 74Threadinorderiterator. h76U堆83MinHeap. h83BinTreeNode . h88BinaryTree . h89MinHeap. h92Huffman. h95Test. cpp961树97TreeNode . h100Tree. h10016硼IllBTreeNode . h111BTr
3、ee.h113test. cpp1261图127MinHeap.h127Vertex, h131Graph, h132test. cpp1441a排序145QueueNode. h1491、顺序表Seqlist.hconst int DefaultSize=100;template class SeqListpublic:SeqList(int sz=DefaultSize):m_nmaxsize(sz)z m_ncurrentsize(-1) if (sz0)m_elements=new Typem_nmaxsize;SeqList()delete m_elements;int Length
4、() constreturn m ncurrentsize+1;/get the lengthint int int intFind(Type x) const;IsElement(Type x) const; Insert(Type xz int i); Remove(Type x);/find the position /is it in the list /insert data /delete dataOf Xint IsEmpty()return m_ncurrentsize=l;)int IsFull()return m_ncurrentsize=m_nmaxsize-1; Typ
5、e Get(int i)/get the ith datareturn im_ncurrentsize?(coutHcant find the elementendlz0) :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;coutcan11 find the e
6、lement you want to findendl; return -1;)template int SeqList:IsElement(Type x) const if(Find(x)=-1)return 0;return 1;template int SeqList:Insert(Type xz int i) if (im_ncurrentsize + lI |m_ncurrentsize=m_nmaxsize-l)coutHthe operate is illegalni;j-)m_elementsj=m_elementsj-1; m_elementsi=x;return 1;)te
7、mplate int SeqList:Remove(Type x)int size=m_ncurrentsize;for(int i=0;im_ncurrentsize;)if(m_elementsi=x)for(int j =i;jm_ncurrentsize;j +)m_elementsj=m_elementsj +1;)m_ncurrentsize-;continue; i + +; if(size=m_ncurrentsize)cout11 can t find the element you want to removeHendl ;return 0;)return 1;templa
8、te void SeqList:Print() for(int i=0;i=m_ncurrentsize;i+)couti + l * : tHm_elements i endl ;coutendlendl;Test . 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);) endlendl;test.Insert(1,0)
9、;cout(test.Find(0)?cant be found :Be found ) 0test .Remove(7);test . Print();test.Remove(9);test.Print();test ,Remove(0); test.Print(); return 0;2 单链表ListNode.h template class SingleList;template class ListNodeprivate:friend typename SingleList;ListNode () :m_pnext (NULL) ListNode(const Type item,Li
10、stNode *next=NULL):m_data(item)zm_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& osz ListNode& out)osout.m_data
11、;return os;SingleList.h#include ListNode.htemplate class SingleListpublic:SingleList():head(new ListNode()SingleList()MakeEmpty();delete head; public:数据结构算法实现2008-9-3void MakeEmpty();int Length();ListNode *Find(Type valuez intListNode *Find(int n);bool Insert(Type item,int n=0);Type Remove(int n=0);
12、bool RemoveAll(Type item);Type Get(int n);void Print();/make the list empty/get the lengthn); /find thd nth data which is equal to value/find the nth data/insert the data in the nth position/remove the nth data/remove all the data which is equal to item/get the nth data/print the listprivate:ListNod
13、e *head;);template void SingleList:MakeEmpty()ListNode *pdel;while(head-m_pnext!=NULL)pdel=head-m_pnext;head-m_pnext=pde1-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 ListNod
14、e* SingleListType: if(n0)coutThe n is out of boundaryendl;return NULL;)ListNode *pmove=head-m_pnext;for(int i=0;im_pnext;) 一if(pmove = =NULL) coutHThe n is out of boundaryendl;return NULL;)return pmove;template ListNode* SingleListType: if(nl)coutThe n is illegalendl;return NULL;)ListNode *pmove=hea
15、d;int count=0;while(count!=n&pmove)pmove=pmove-m_pnext;if(pmove-m_data=value)count+;:Find(int n):Find(Type valuez int n)if(pmove =NULL) coutcant find the elementendl; return NULL;)return pmove;template bool SingleList:Insert(Type item, int n) if(n0)coutThe n is illegalendl;return 0;)ListNode *pmove=
16、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=pnode;return 1;template bool SingleList:RemoveAll(Type item)ListNode *pmove=head;ListNode *pdel=head-m_pnext ;whi
17、le(pdel!=NULL)if(pdel-m_data=item)pmove-m_pnext=pde1-m_pnext;delete pdel;pde1=pmove-m_pnext;continue;)pmove=pmove-m_pnext;pde1=pde1-m_pnext;) return 1;template Type SingleList:Remove(int n)if(n0)coutcan11 find the elementendl;exit(1);)ListNode pmove=head,*pdel;for(int i=0;im_pnext;i+)pmove=pmove-m_p
18、next;)一if(pmove-m_pnext = =NULL)coutcant find the elementm_pnext;pmove-m_pnext =pde1-m_pnext;Type temp=pdel-m_data;delete pdel; return temp;)template Type SingleList:Get(int n) if(n0)cout11 The n is out of boundary Hendl ;exit(1);)ListNode pmove=head-m_pnext;for(int i=0;im_pnext;if(NULL=pmove) coutT
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 实现 数据结构 中的 各种 算法
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内