博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法导论 循环双链表
阅读量:4088 次
发布时间:2019-05-25

本文共 6424 字,大约阅读时间需要 21 分钟。

循环双链表

1. 什么是循环双链表?

循环双链表:循环,即是首尾相连的链表。双链表,即是在原来单链表的基础上,使结点不单只能指向下一个结点,也能指向上一个结点。

2. 循环双链表的基本操作(伪代码)

LIST-SEARCH(L,k)

x = L.headwhile x ≠NIL and x.key ≠ kx = x.nextreturn x

LIST-INSERT(L,x)

x.next = L.headif L.head = NILL.head.pre = xL.head = xx.pre = NIL

LIST-DELETE(L,x)

if x.pre = NILx.pre.next = x.nextelse L.head = x.nextif x.next = NILx.next.pre = x.pre

3. 循环双链表的实现(C++代码)

//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{    template
void 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"#include 
using 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;}

3. 程序运行结果

testCircularDoubleLink is Empty.

PrintMemory:

Insert:0

testCircularDoubleLink is Not Empty.
PrintMemory: 1

Insert:1

testCircularDoubleLink is Not Empty.
PrintMemory: 1 2

Insert:2

testCircularDoubleLink is Not Empty.
PrintMemory: 1 2 3

Insert:3

testCircularDoubleLink is Not Empty.
PrintMemory: 1 2 3 4

Insert:4

testCircularDoubleLink is Not Empty.
PrintMemory: 1 2 3 4 5

Delete:1

testCircularDoubleLink is Not Empty.
PrintMemory: 2 3 4 5

Delete:3

testCircularDoubleLink is Not Empty.
PrintMemory: 2 4 5

转载地址:http://gnyii.baihongyu.com/

你可能感兴趣的文章
Ranger集成Kerberos
查看>>
solr集成kerberos认证
查看>>
Flink安装部署
查看>>
Hadoop — MapReduce原理解析
查看>>
elasticSearch安装部署
查看>>
elasticSearch基本使用
查看>>
HBase读写的几种方式(一)java篇
查看>>
Jetson Nano安装pytorch 基于torch1.6和torchvision0.7
查看>>
【Jetson-Nano】2.Tensorflow和Pytorch的安装
查看>>
ubuntu 系统下的Caffe环境搭建
查看>>
Yolov5系列AI常见数据集(1)车辆,行人,自动驾驶,人脸,烟雾
查看>>
【Jetson-Nano】2.Tensorflow object API和Pytorch的安装
查看>>
荔枝派 Nano 全志 F1C100s 编译运行 Linux ubuntu并升级gcc
查看>>
C++ STL 四种智能指针
查看>>
基于sympy的python实现三层BP神经网络算法
查看>>
玩玩机器学习1——ubuntu16.04 64位安装TensorFlow GPU+python3+cuda8.0+cudnn8.0
查看>>
CentOS7 搭建Pulsar 消息队列环境,CentOS(Linux)部署Pulsar,亲测成功,以及Python操作Pulsar实例驱动
查看>>
Git报错: OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443
查看>>
Java学习笔记--带有验证码的登录案例
查看>>
数据结构与算法学习笔记(1)--数组
查看>>