rn p; else {cerr<<"pos is out range!"< return NULL;} } //返回双向循环链表中指定序号的结点值 ElemType DuLinkList::GetElem(int pos) {if(pos<1) {cerr<<"pos is out range!"< DuLNode* p=head->next; int i=0; while(p!=head) {i++; if(i==pos) break; p=p->next;} if(p!=head) return p->data; else {cerr<<"pos is out range!"< return pos;} } //遍历双向循环链表 void DuLinkList::TraverseCList() {DuLNode *p=head->next; while(p!=head) {cout<data; p=p->next;} cout< } //当前指针curr指向pos结点并返回curr DuLNode *DuLinkList::Reset(int pos) {DuLNode* p=curr=head->next; int i=-1; while(p!=head) {i++; if(i==pos) break; p=p->next;curr=curr->next;} return curr; } //当前指针curr指向下一结点并返回 DuLNode *DuLinkList::Next() {curr=curr->next; return curr; } //当前指针curr指向上一结点并返回 DuLNode *DuLinkList::Prior() {curr=curr->prior; return curr; } // 判双向循环链表当前指针curr==head 否 bool DuLinkList::EndOCList() {return curr==head;} //判双向循环链表当前指针curr->next是否到达表尾 bool DuLinkList::EndCList() {return curr->next==head;} //判双向循环链表当前指针curr->prior是否到达表尾 bool DuLinkList::PEndCList() {return curr->prior==head;} //删除curr->next所指结点,并返回所删结点的data ElemType DuLinkList::DeleteNt() {DuLNode *p=curr->next; curr->next=p->next; curr->next->next->prior=p->prior; ElemType data=p->data; delete p; count--; return data; } //从双向循环链表中查找元素 bool DuLinkList::FindCList(ElemType& item) {DuLNode* p=head->next; while(p!=head) if(p->data==item) {item=p->data;return true;} else p=p->next; return false; } //更新双向循环链表中的给定元素 bool DuLinkList::UpdateCList(const ElemType &item,ElemType &e) {DuLNode* p=head->next; while(p!=head) //查找元素 if(p->data==item) break; else p=p->next; if(p==head) return false; else { //更新元素 p->data=e;return true;} } //向链表中第pos个结点前插入域值为item的新结点 void DuLinkList::InsertCLfront(const ElemType& item,int pos) {DuLNode *newP=new DuLNode; newP->data=item; DuLNode* p=head->next; int i=0; while(p!=head) {i++; if(i==pos) break; p=p->next;} newP->prior=p->prior; p->prior->next=newP; newP->next=p; p->prior=newP; count++; } //向链表中第pos个结点后插入域值为item的新结点 void DuLinkList::InsertCLafter(const ElemType& item,int pos) {DuLNode *newP=new DuLNode; newP->data=item; DuLNode* p=head->next; int i=-1; while(p!=head) {i++; if(i==pos) break; p=p->next;} newP->prior=p->prior; p->prior->next=newP; newP->next=p; p->prior=newP; count++; } //从链表中删除第pos个结点并返回被删结点的data ElemType DuLinkList::DeleteCList(int pos) {if(pos<1) {cerr<<"pos is out range!"< DuLNode *p=head->next; ElemType data; int i=0; while(p!=head) {i++; if(i==pos) break; p=p->next;} if(p!=head) {data=p->data; p->prior->next=p->next; p->next->prior=p->prior; delete []p;count--;return data;} else return pos; }
调用如下 [cpp] #include "stdafx.h" #include "dcirlinkl.h" void main() { cout<<"运行结果:\n"; int m=150,i,n=10,x,it; DuLinkList p,t,q,mylink; p.CreateCLinkL(n,m,1); if(p.CListEmpty()) cout<<"双向循环链表p空!\n"; else cout<<"双向循环链表p非空!\n"; cout<<"双向循环链表p(升序):\n"; p.TraverseCList(); if(p.CListEmpty()) cout<<"双向循环链表p空!\n"; else cout<<"双向循环链表p非空!\n"; if(p.EndCList()) cout<<"双向循环链表p满!\n"; else cout<<"双向循环链表p非满!\n"; cout<<"双向循环链表t(无序):\n"; t.CreateCLinkL(n-2,m); t.TraverseCList(); cout<<"双向循环链表t的长度:"< cout<<"双向循环链表q(降序):\n"; q.CreateCLinkL(n,m,-1); q.TraverseCList(); cout<<"双向循环链表q的长度:"< cout<<"链表q的第1个元素:"< cout<<"链表q的第1个元素地址:"< cout<<"链表q的第5个元素:"< cout<<"链表q的第5个元素地址:"< cout<<"链表q的第10个元素:"< cout<<"链表q的第10个元素地址:"< cout<<"链表q的curr->next所指元素地址:"< x=65;it=66; if(q.FindCList(x)) cout< else cout< if(q.UpdateCList(x,it)) cout< else cout< cout<<"更新后双向循环链表q:\n"; q.TraverseCList(); cout<<"插入后双向循环链表q:\n"; it=100;q.InsertCLfront(it,1); q.Traver |