本文共 6424 字,大约阅读时间需要 21 分钟。
循环双链表:循环,即是首尾相连的链表。双链表,即是在原来单链表的基础上,使结点不单只能指向下一个结点,也能指向上一个结点。
LIST-SEARCH(L,k)
x = L.headwhile x ≠NIL and x.key ≠ kx = x.nextreturn xLIST-INSERT(L,x)
x.next = L.headif L.head = NILL.head.pre = xL.head = xx.pre = NILLIST-DELETE(L,x)
if x.pre = NILx.pre.next = x.nextelse L.head = x.nextif x.next = NILx.next.pre = x.pre
//CircularDoubleLink.h#pragma once#include#include template class Node{public: Node(ElemType* pData = NULL, Node * pNext = NULL, Node * pPrev = NULL); ElemType const& GetData() const; void SetData(ElemType val) ; Node * const& GetNext() const; void SetNext(Node * val); Node * const& GetPrev() const; void SetPrev(Node * val) ;private: ElemType* m_pData; Node * m_pNext; Node * m_pPrev;};template class CircularDoubleLink{public: CircularDoubleLink(); unsigned int const& GetLength() const; bool Insert(ElemType elem, unsigned int pos); bool InsertByPosNode(ElemType elem, Node * posNode, Node ** RetInsetNode = NULL); bool Delete(unsigned int pos, ElemType* elem); bool Search(unsigned int pos, ElemType* elem) const; bool Visit(ElemType* elem, const unsigned int& pos) const; bool Empty(); Node * HavaHeadNode(); Node * const& GetHeadNode() const;private: Node * m_pHeadNode; unsigned int m_length;};//————————————————————————————————//Node类的实现template Node ::Node(ElemType* pData /* = NULL */, Node * pNext /* = NULL */, Node * pPrev /* = NULL */) :m_pNext(pNext),m_pData(pData),m_pPrev(pPrev){}template void Node ::SetNext(Node * val){ m_pNext = val;}template Node * const& Node ::GetNext() const{ return m_pNext;}template void Node ::SetData(ElemType val){ m_pData = val;}template ElemType const& Node ::GetData() const{ return *m_pData;}template void Node ::SetPrev(Node * val){ m_pPrev = val;}template Node * const& Node ::GetPrev() const{ return m_pPrev;}//————————————————————————————————//CircularDoubleLink类实现template CircularDoubleLink ::CircularDoubleLink() :m_pHeadNode(new Node ()),m_length(0){ m_pHeadNode->SetNext(m_pHeadNode); m_pHeadNode->SetPrev(m_pHeadNode);}template bool CircularDoubleLink ::InsertByPosNode(ElemType elem, Node * posNode, Node ** RetInsetNode /*= NULL*/){ Node * insertNode = new Node (new ElemType(elem),posNode->GetNext(),posNode); posNode->GetNext()->SetPrev(insertNode); posNode->SetNext(insertNode); ++m_length; if (RetInsetNode) { *RetInsetNode = insertNode; } return true;}template bool CircularDoubleLink ::Insert(ElemType elem, unsigned int pos){ if (pos > GetLength() || pos < 0) { assert(false && "Error: SinglyLink's insert pos is out of range!\n"); return false; } for(Node * pCurrentNode = m_pHeadNode; ; pCurrentNode = pCurrentNode->GetNext()) { if (pos-- == 0) { InsertByPosNode(elem, pCurrentNode); return true; } } assert(false && "Error: SinglyLink Insert failed for unknow reason!"); return false;}template bool CircularDoubleLink ::Delete(unsigned int pos, ElemType* elem){ if (pos >= GetLength() || pos < 0) { assert(false && "Error: SinglyLink's delete pos is out of range!\n"); } for(Node * pCurrentNode = m_pHeadNode->GetNext(); ; pCurrentNode = pCurrentNode->GetNext()) { if (pos-- == 0) { Node * deleteNode = pCurrentNode; deleteNode->GetNext()->SetPrev(deleteNode->GetPrev()); pCurrentNode->GetPrev()->SetNext(deleteNode->GetNext()); *elem = deleteNode->GetData(); delete deleteNode; --m_length; return true; } } assert(false && "Error: SinglyLink pos delete failed for unknow reason!"); return false;}template unsigned int const& CircularDoubleLink ::GetLength() const{ return m_length;}template bool CircularDoubleLink ::Search(unsigned int pos, ElemType* elem) const{ if (pos >= GetLength() || pos < 0) { assert(false && "Error: SinglyLink's search pos is out of range!\n"); } for(Node * pCurrentNode = m_pHeadNode->GetNext(); ; pCurrentNode = pCurrentNode->GetNext()) { if (pos-- == 0) { *elem = pCurrentNode->GetData(); return true; } } return false;}template bool CircularDoubleLink ::Visit(ElemType* elem, const unsigned int& pos) const{ if (pos >= GetLength() || pos < 0) { return false; } return Search(pos,elem);}template bool CircularDoubleLink ::Empty(){ return !m_length;}template Node * CircularDoubleLink ::HavaHeadNode(){ return m_pHeadNode;}template Node * const& CircularDoubleLink ::GetHeadNode() const{ return m_pHeadNode;}
//Util.h#pragma oncenamespace Util{ templatevoid PrintMemory(const T& dateStruct, unsigned int size) { cout << "PrintMemory: "; for (int i = 0; i != size; i++) { ElemType tempElem; if (!dateStruct.Visit(&tempElem,i)) { printf("\n"); return; } printf("%d ",tempElem); } printf("\n"); }}
//main.cpp#include "Util.h"#include "CircularDoubleLink.h"#includeusing namespace std;typedef int ElemType;int main(){ CircularDoubleLink testCircularDoubleLink; cout << "testCircularDoubleLink is " << (testCircularDoubleLink.Empty() ? "Empty." : "Not Empty.") << endl; Util::PrintMemory(testCircularDoubleLink,testCircularDoubleLink.GetLength()); for (int i = 0; i != 5; i++) { testCircularDoubleLink.Insert(i+1,i); cout << "\nInsert:" << i << endl; cout << "testCircularDoubleLink is " << (testCircularDoubleLink.Empty() ? "Empty." : "Not Empty.") << endl; Util::PrintMemory(testCircularDoubleLink,testCircularDoubleLink.GetLength()); } for (int i = 0; i != 2; i++) { ElemType tempElem; testCircularDoubleLink.Delete(i,&tempElem); cout << "\nDelete:" << tempElem << endl; cout << "testCircularDoubleLink is " << (testCircularDoubleLink.Empty() ? "Empty." : "Not Empty.") << endl; Util::PrintMemory(testCircularDoubleLink,testCircularDoubleLink.GetLength()); } return 0;}
testCircularDoubleLink is Empty.
PrintMemory:Insert:0
testCircularDoubleLink is Not Empty. PrintMemory: 1Insert:1
testCircularDoubleLink is Not Empty. PrintMemory: 1 2Insert:2
testCircularDoubleLink is Not Empty. PrintMemory: 1 2 3Insert:3
testCircularDoubleLink is Not Empty. PrintMemory: 1 2 3 4Insert:4
testCircularDoubleLink is Not Empty. PrintMemory: 1 2 3 4 5Delete:1
testCircularDoubleLink is Not Empty. PrintMemory: 2 3 4 5Delete:3
testCircularDoubleLink is Not Empty. PrintMemory: 2 4 5
转载地址:http://gnyii.baihongyu.com/