数据结构各种算法实现(C模板).docx
《数据结构各种算法实现(C模板).docx》由会员分享,可在线阅读,更多相关《数据结构各种算法实现(C模板).docx(101页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1、顺序表1Seqlist. h1SingleList. h4test. cpp8i双向链表9NodeList. h9DoubleList .h9Test. cpp134. 循环链表14CircularList. h15顺序栈19Test. cpp216 链式栈22StackNode . h22Test. cpp247 .顺序队列24SeqQueue. h24Test. cpp26QueueNode . h27LinkQueue . h289歹U30Test. cpp33IQ 申34MyString.h34BinTreeNode . h40Test. cpp481ZThreadTree. h4
2、9Threadinorderiterator. h4911 堆54test. cpp57BinTreeNode. h57Tree. h66BTree .h74test. cpp82MinHeap. h83Edge. h85Vertex. h85test. cpp941 & 抖Ej亨 95test. cpp106Seqlist.h const int DeaultSize100;template class SeqList public:SeqList(int szDefaultSize):m mnaxsize(sz)ncurrentsize(-1)if (sz0)m_elementssnew
3、Typemnmaxsize;-SeqList()delete m_elements;intLength() const(/get the lengthreturn m ncurrentsize+1;intFind(Type x) const;/find the position of xintIsElement(Type x) const;/is it in the listintInsert(Type x,int i);/insert dataintRemove(Type x);/delete dataintIsEmpty()intIsFull()return m ncurrentsize=
4、m nmaxsize-1;Type/get the ith datareturn im_ncurrentsize?(coutcan11 find the elementendl,0):m_elementsi;voidPrint();private:Type*m elements;const int m_nmaxsize;int m ncurrentsize;template int SeqList:Find(Type x) const for(int isO;im_ncurrentsize;)i(m_elementsi=x) return i;coutcant ind the element
5、you want to findendl; return -1; template int SeqList;:IsElement(Type x) const i(Find(x)=-1) return 0;return 1;template int SeqList:Insert(Type x, int i) if (im_ncurrentsize-t-l | |m_ncurrentsize=m_nmaxsize-1) ( coutthe operate is illegali;j-) m_elementsj=m_elementsj-1;)m_elementsi=x;return 1;templa
6、te int SeqList:Remove(Type x) int size=m_ncurrentsize;for(int isO;im_ncurrentsize;)if(melementsix)for(int j =i;jm_ncurrentsize;j +) m elementsj=m_elementsj + 1;mncurrentsize-;continue; i+ +;)i(sizessm_ncurrentsize) coutncant find the element you want to removeendl; return 0;) return 1;template void
7、SeqList s s Print()for(int isO;ism_ncurrentsize;i+)couti+lstm_elements1endl;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);)test.Insert(1,0);cout (test.Find(0) ?can t be
8、 found : Be found ,) 0 endlendl;test.Remove(7);test.Print();test.Remove(9);test.Print();test.Remove(0);test.Print();return 0;)2. 单链表ListNode.htemplate class SingleList;template class ListNode private:friend typename SingleList;ListNodeO :m_pnext (NULL) ListNode(const Type item,ListNode *next=NULL):m
9、_data(item)rm_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 SingleListpublic:SingleList
10、():head(new ListNode()SingleList ()MakeEmpty(); delete 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 t
11、he nth positionTypeRemove(int n=0);/remove the nth databoolRemoveAll(Type item);/remove all the data which is equal to itemTypeGet(int n);/get the nth datavoidPrint();/print the listprivate:ListNode *head;);template void SingleList:MakeEmpty()ListNode *pdel;while(head-m_pnext!=NULL)pdel=head-m_pnext
12、;head-m_pnext=pdel-m_pnext;delete pdel;)template int SingleList:Length()ListNode *pmove=head-m_pnext;int countsO;while(pmove!=NULL)pmove=pmove-m_pnext;count+;)return count;template ListNode* SingleList:Find(int n)if(n0)coutnThe n is out of boundaryendl; return NULL;ListNode *pmove=head-m_pnext;for(i
13、nt i0;im_pnext;)i(pmove=NULL) coutThe n is out of boundaryendl; return NULL;)return pmove;template ListNode* SingleList:Find(Type value,int n)if(nl)coutThe n is illegalendl; return NULL;ListNode pmove=head; int counts0;while(count!sn&pmove) pmove=pmove-m_pnext; if(pmove-m_datassvalue) count+;)i(pmov
14、e=NULL)coutcant find the elementnendl;return NULL;) return pmove;)template bool SingleList:Insert(Type item, int n) if(n0) coutThe n is illegalendl; return 0;)ListNode *pmovehead;ListNode *pnodesnew ListNode(item);if(pnode=NULL)coutApplication error 1endl; return 0;)or(int i0;im_pnext;if(pmove=NULL)
15、 coutthe n is illegalm_pnext=pmove-m_pnext;pmove-m_pnextpnode;return 1;)template bool SingleList s:RemoveAll(Type item) ListNode *pmove=head;ListNode *pdel=head-m_pnext;while(pdel!=NULL)数据结构算法实现if(pdel-m_data=sitem)pmove-m_pnext=pdel-m_pnext;delete pdel;pde1spmove-m_pnext;continue;pmove=pmove-m_pnex
16、t;pdel=pdel-m_pnext;)return 1; template Type SingleList:Remove(int n) if(n0) coutcan t ind the elementendl;exit(1);) ListNode *pmove=head,*pdel; for(int i=0;im_pnext;i+) pmove upmove-m_pnext;i(pmove-m_pnext=sNULL) coutcant find the elementm_pnext;pmove-m_pnextpdel-m_pnext;Type temppdel-m_data;delete
17、 pdel;return temp;template Type SingleList::Get(int n) if(n0)coutThe n is out of boundaryendl; exit(1);) ListNode *pmove=head-m_pnext;or(int i0;im_pnext;if(NULL=pmove)coutThe n is out of boundarym_data;template void SingleList s:Print()ListNode pmove=head-m_pnext;couthead;while(pmove)cout m_data;pmo
18、ve=pmove-m_pnext;)coutoverendlendlendl;test.cpp#include using namespace std;#include SingleList.h int main()SingleList list;or(int =0;i20;1+)list.Insert(i*3,i);)for(int i0;i5;i+)list.Insert(3,1*3);)coutthe Length of the list is list.Length()endl; list.Print();list.Remove(5);coutthe Length of the lis
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 各种 算法 实现 模板
限制150内