C++ 实现双向链表的实例
双向链表C++的实现
本文是通过C++的知识实现数据结构中的双向链表,这里不多说了,代码注释很清楚,
实现代码:
//doubleLinkListimplementwithC++template #includeusingnamespacestd; /*template classDBListADT { public: virtualvoidAppend(constType&)=0; virtualvoidInsert(constType&)=0; virtualvoidRemove(constType&)=0; }; template classDLinkListNode:publicDBListADT //此处必须为实现的类型,当然以派生类的模板类型也可以,但是不能是Type { public: voidAppend(constT&);//这边也需要是当前类的类型,不能为Type voidInsert(constT&); voidRemove(constT&); };*/ template classDLinkList; template classDNode { friendclassDLinkList ;//指定前需声明 public: DNode(){next=NULL;prior=NULL;} ~DNode(){} private: DNode*next; DNode*prior; Typedata; }; template classDLinkList { public: DLinkList() { //head=newDNode [sizeof(DNode )]; head=newDNode ; } ~DLinkList() { if(head->next==NULL) deletehead; else { DNode *p=head->next; DNode *s=NULL; while(p) { s=p->next; deletep; p=s; } } } voidDeleteElement(size_tposition) { DNode *p=head->next; size_tindex=1; for(;index next; if(p==NULL) return; p->prior->next=p->next; p->next->prior=p->prior; deletep; } voidInsertElement(Tdata,size_tposition); voidCreateDLinkList(Ta[],intn); voidPrintDLinkList(); private: DNode *head; }; template voidDLinkList ::InsertElement(Tdata,size_tposition) { DNode *p=head->next; size_tindex=1; for(;index next; if(p==NULL) return; //DNode *s=newDNode [sizeof(DNode )]; DNode *s=newDNode ; s->data=data; s->next=p; s->prior=p->prior; p->prior->next=s; p->prior=s; } template voidDLinkList ::CreateDLinkList(Ta[],intn) { DNode *p=head; DNode *s=NULL; inti=0; for(;i [sizeof(DNode)]; s=newDNode ; s->data=a[i]; p->next=s; s->prior=p; p=s; } s->next=NULL; } template voidDLinkList ::PrintDLinkList() { DNode *p=head->next; while(p) { cout< data< next; } } intmain() { inta[10]={1,2,3,4,5,6,7,8,9,10}; DLinkList Dlist; Dlist.CreateDLinkList(a,10); Dlist.DeleteElement(3); Dlist.InsertElement(3,3); Dlist.PrintDLinkList(); return0; } //doubleLinkListimplementwithC++Class //************************************************************ /*#include usingnamespacestd; classNode { friendclassList; public: //Node(inta):next(NULL),prior(NULL),data(a){} Node(){} private: Node*next; Node*prior; intdata; }; classList { public: List() { cout<<"createheadDBLinkList"< next==NULL) { deletehead; } else { Node*p=head->next; Node*s; deletehead; while(p) { s=p->next; deletep; p=s; } } cout<<"destructorcalledtoclearDBLinkList"< next=NULL; Node*s,*p=head; inti=0; for(;i data=a[i]; p->next=s; s->prior=p; p=s; } s->next=NULL; } voidList::PrintDList() { Node*p=head->next; while(p) { cout< data< next; } } voidList::DeleteElemData(intposition) {//可以通过重载delete运算符来达到这个效果,则直接用delete就OK了 Node*p=head->next; //while(p!=NULL&&p->data!=data) //p=p->next; inti=1; for(;i next; if(p==NULL) return; p->prior->next=p->next; p->next->prior=p->prior; deletep; } voidList::InsertElement(intdata,intposition) {//可以重载new运算符来达到这个效果,则直接用new就OK了 Node*p=head->next; inti=1; for(;i next; Node*s=newNode[sizeof(Node)]; s->data=data; s->prior=p->prior; s->next=p; p->prior->next=s; p->prior=s; } intmain() { ListDlist; inta[10]={1,2,3,4,5,6,7,8,9,10}; Dlist.CreateDoubleLink(a,10); Dlist.DeleteElemData(3); Dlist.InsertElement(3,3); Dlist.PrintDList(); return0; }*/ //*************************************************************************************
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!