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

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

循环单向链表

1. 循环单向链表如何实现?

循环单向链表只是在单向链表的基础上使尾结点指向头结点。因为每次插入结点,新结点都是指向上一个结点当前的下一个结点。因此在单向链表的基础上,我们使头结点指向自身即可。

因此只需要在以前的博文单链表代码中的构造函数初始化的时候,加上一行代码,便做到了循环:
m_pHeadNode->SetNext(m_pHeadNode);

2. 实现(C++代码)

//CircularSinglyLink.h#pragma once#include 
#include
template
class Node{public: Node(Node
* pNext = NULL, ElemType* pData = NULL); ElemType const& GetData() const; void SetData(ElemType val) ; Node
* const& GetNext() const; void SetNext(Node
* val);private: ElemType* m_pData; Node
* m_pNext;};template
class CircularSinglyLink{public: CircularSinglyLink(); 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();private: Node
* m_pHeadNode; unsigned int m_length;};//————————————————————————————————//Node类的实现template
Node
::Node(Node
* pNext /*= NULL*/, ElemType* pData /*= NULL*/) :m_pNext(pNext),m_pData(pData){}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;}//————————————————————————————————//SinglyLink类实现template
CircularSinglyLink
::CircularSinglyLink() :m_pHeadNode(new Node
()),m_length(0){ m_pHeadNode->SetNext(m_pHeadNode);}template
bool CircularSinglyLink
::InsertByPosNode(ElemType elem, Node
* posNode, Node
** RetInsetNode /*= NULL*/){ Node
* insertNode = new Node
(posNode->GetNext(),new ElemType(elem)); posNode->SetNext(insertNode); ++m_length; *RetInsetNode = insertNode; return true;}template
bool CircularSinglyLink
::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 != NULL; pCurrentNode = pCurrentNode->GetNext()) { if (pos-- == 0) { Node
* insertNode = new Node
(pCurrentNode->GetNext(),new ElemType(elem)); pCurrentNode->SetNext(insertNode); ++m_length; return true; } } assert(false && "Error: SinglyLink Insert failed for unknow reason!"); return false;}template
bool CircularSinglyLink
::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; pCurrentNode != NULL; pCurrentNode = pCurrentNode->GetNext()) { if (pos-- == 0) { Node
* deleteNode = pCurrentNode->GetNext(); pCurrentNode->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& CircularSinglyLink
::GetLength() const{ return m_length;}template
bool CircularSinglyLink
::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; pCurrentNode != NULL; pCurrentNode = pCurrentNode->GetNext()) { if (pos-- == 0 && (pCurrentNode->GetNext() != NULL) ) { *elem = pCurrentNode->GetNext()->GetData(); return true; } } return false;}template
bool CircularSinglyLink
::Visit(ElemType* elem, const unsigned int& pos) const{ if (pos >= GetLength() || pos < 0) { return false; } return Search(pos,elem);}template
bool CircularSinglyLink
::Empty(){ return !m_length;}template
Node
* CircularSinglyLink
::HavaHeadNode(){ 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 "CircularSinglyLink.h"#include 
using namespace std;typedef int ElemType;int main(){ CircularSinglyLink
testCircularSinglyLink; cout << "testCircularSinglyLink is " << (testCircularSinglyLink.Empty() ? "Empty." : "Not Empty.") << endl; Util::PrintMemory(testCircularSinglyLink,testCircularSinglyLink.GetLength()); for (int i = 0; i != 5; i++) { testCircularSinglyLink.Insert(i+1,i); cout << "\nInsert:" << i << endl; cout << "testCircularSinglyLink is " << (testCircularSinglyLink.Empty() ? "Empty." : "Not Empty.") << endl; Util::PrintMemory(testCircularSinglyLink,testCircularSinglyLink.GetLength()); } for (int i = 0; i != 2; i++) { ElemType tempElem; testCircularSinglyLink.Delete(i,&tempElem); cout << "\nDelete:" << tempElem << endl; cout << "testCircularSinglyLink is " << (testCircularSinglyLink.Empty() ? "Empty." : "Not Empty.") << endl; Util::PrintMemory(testCircularSinglyLink,testCircularSinglyLink.GetLength()); } return 0;}

3. 程序运行结果

testCircularSinglyLink is Empty.

PrintMemory:

Insert:0

testCircularSinglyLink is Not Empty.
PrintMemory: 1

Insert:1

testCircularSinglyLink is Not Empty.
PrintMemory: 1 2

Insert:2

testCircularSinglyLink is Not Empty.
PrintMemory: 1 2 3

Insert:3

testCircularSinglyLink is Not Empty.
PrintMemory: 1 2 3 4

Insert:4

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

Delete:1

testCircularSinglyLink is Not Empty.
PrintMemory: 2 3 4 5

Delete:3

testCircularSinglyLink is Not Empty.
PrintMemory: 2 4 5

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

你可能感兴趣的文章
my read work
查看>>
db db2 base / instance database tablespace container
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
webServer kzserver/1.0.0
查看>>
OS + Linux DNS Server Bind
查看>>
linux下安装django
查看>>
Android 解决TextView设置文本和富文本SpannableString自动换行留空白问题
查看>>
Android开发中Button按钮绑定监听器的方式完全解析
查看>>
Android自定义View实现商品评价星星评分控件
查看>>
postgresql监控工具pgstatspack的安装及使用
查看>>
postgresql查看表的和索引的情况,判断是否膨胀
查看>>
postgresql减少wal日志生成量的方法
查看>>
swift中单例的创建及销毁
查看>>
获取App Store中App的ipa包
查看>>
十进制字符串转十六进制字符串
查看>>
自定义大头针
查看>>
UIButton添加block点击事件
查看>>
利用runtime给类别添加属性
查看>>
本地推送
查看>>
FMDB的使用
查看>>