※数据结构※→☆线性表结构(list)☆============单向循环链表结构(list circular single)(四)
循环链表是另一种形式的链式存贮结构。它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环。
单循环链表——在单链表中,将终端结点的指针域NULL改为指向表头结点或开始结点即可。
循环链表的特点是无须增加存储量,仅对表的链接方式稍作改变,即可使得表处理更加方便灵活。
注意:
①循环链表中没有NULL指针。涉及遍历操作时,其终止条件就不再是像非循环链表那样判别p或p->next是否为空,而是判别它们是否等于某一指定指针,如头指针或尾指针等。
②在单链表中,从一已知结点出发,只能访问到该结点及其后续结点,无法找到该结点之前的其它结点。而在单循环链表中,从任一结点出发都可访问到表中所有结点,这一优点使某些运算在单循环链表上易于实现。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
以后的笔记潇汀会尽量详细讲解一些相关知识的,希望大家继续关注我的博客。
本节笔记到这里就结束了。
潇汀一有时间就会把自己的学习心得,觉得比较好的知识点写出来和大家一起分享。
编程开发的路很长很长,非常希望能和大家一起交流,共同学习,共同进步。
如果文章中有什么疏漏的地方,也请大家指正。也希望大家可以多留言来和我探讨编程相关的问题。
最后,谢谢你们一直的支持~~~
C++完整个代码示例(代码在VS2005下测试可运行)
AL_ListCircularSingle.h
/**
@(#)$Id: AL_ListCircularSingle.h 27 2013-09-02 07:46:27Z xiaoting $
@brief One-way circular list is another form of one-way linked list structure, which is characterized by a pointer to the list
of the last one is no longer a null value, but point to the head of the linked list. So long as we know any node in the linked list
can be found circulating list of all other nodes. Singly linked list (single list) is a list, which is characterized by the direction of the chain links are unidirectional,
Chain accessed through sequential reads starting from the head; linked list is constructed using pointers list; known the list of
nodes, as a linked list nodes is assembled; wherein each node has a pointer member variable refers to the next list node; @Author $Author: xiaoting $
@Date $Date: 2013-09-02 15:46:27 +0800 (周一, 02 九月 2013) $
@Revision $Revision: 27 $
@URL $URL: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_ListCircularSingle.h $
@Header $Header: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_DataStructure/groupinc/AL_ListCircularSingle.h 27 2013-09-02 07:46:27Z xiaoting $
*/ #ifndef CXX_AL_LISTCIRCULARSINGLE_H
#define CXX_AL_LISTCIRCULARSINGLE_H #ifndef CXX_AL_NODE_H
#include "AL_Node.h"
#endif ///////////////////////////////////////////////////////////////////////////
// AL_ListCircularSingle
/////////////////////////////////////////////////////////////////////////// template<typename T>
class AL_ListCircularSingle
{
public:
static const DWORD LISTSINGLE_POSITION_INVALID = 0xffffffff;
/**
* Construction
*
* @param
* @return
* @note
* @attention
*/
AL_ListCircularSingle(); /**
* Destruction
*
* @param
* @return
* @note
* @attention
*/
~AL_ListCircularSingle(); /**
* Length
*
* @param VOID
* @return DWORD
* @note get the length of the list
* @attention
*/
DWORD Length() const; /**
* Find
*
* @param const T& tTemplate
* @return DWORD
* @note find the position of tTemplate
* @attention if not find, will be return 0xffffffff
*/
DWORD Find(const T& tTemplate ) const; /**
* IsElement
*
* @param const T& tTemplate
* @return BOOL
* @note the tTemplate is in the list?
* @attention
*/
BOOL IsElement(const T& tTemplate ) const; /**
* Insert
*
* @param DWORD dwIndex
* @param const T& tTemplate
* @return BOOL
* @note inset the tTemplate into the list at the position
* @attention
*/
BOOL Insert(DWORD dwIndex,const T& tTemplate ); /**
* InsertBegin
*
* @param const T& tTemplate
* @return BOOL
* @note inset the tTemplate into the list at the position
* @attention
*/
BOOL InsertBegin(const T& tTemplate ); /**
* InsertEnd
*
* @param const T& tTemplate
* @return BOOL
* @note inset the tTemplate into the list at the position
* @attention
*/
BOOL InsertEnd(const T& tTemplate ); /**
* Remove
*
* @param const T& tTemplate
* @return BOOL
* @note remove the tTemplate into the list
* @attention
*/
BOOL Remove(const T& tTemplate ); /**
* IsEmpty
*
* @param VOID
* @return BOOL
* @note the list has data?
* @attention
*/
BOOL IsEmpty() const; /**
* Get
*
* @param
* @return BOOL
* @note get the const T& at the position
* @attention the dwIndex must is little than the list length
*/
T Get(DWORD dwIndex) const; /**
* Set
*
* @param DWORD dwIndex
* @param const T& tTemplate
* @return BOOL
* @note Replaced with the element element element on position index, and returns the old element...
* @attention Index must in the list
*/
T Set(DWORD dwIndex, const T& tTemplate ); /**
* Clear
*
* @param VOID
* @return VOID
* @note clear the data in the list
* @attention all data will be clear
*/
VOID Clear(); protected:
private:
/**
* GetNodeByIndex
*
* @param
* @return BOOL
* @note get the const T& at the position
* @attention the dwIndex must is little than the list length
*/
AL_Node<T>* GetNodeByIndex(DWORD dwIndex) const; public:
protected:
private:
AL_Node<T>* m_pHeader;
}; ///////////////////////////////////////////////////////////////////////////
// AL_ListCircularSingle
///////////////////////////////////////////////////////////////////////////
/**
* Construction
*
* @param
* @return
* @note
* @attention
*/
template<typename T>
AL_ListCircularSingle<T>::AL_ListCircularSingle():
m_pHeader(NULL)
{
m_pHeader = new AL_Node<T>;
m_pHeader->m_pNext = m_pHeader;
} /**
* Destruction
*
* @param
* @return
* @note
* @attention
*/
template<typename T>
AL_ListCircularSingle<T>::~AL_ListCircularSingle()
{
Clear();
//delete the header
delete m_pHeader;
m_pHeader = NULL;
} /**
* Length
*
* @param
* @return
* @note get the length of the list
* @attention
*/
template<typename T> DWORD
AL_ListCircularSingle<T>::Length() const
{
if (TRUE == IsEmpty()) {
return 0;
}
AL_Node<T>* pMove = NULL;
DWORD dwCount = 1; pMove = m_pHeader->m_pNext;
while (m_pHeader != pMove->m_pNext) {
dwCount ++;
pMove = pMove->m_pNext;
}
return dwCount;
} /**
* Find
*
* @param const T& tTemplate
* @return DWORD
* @note find the position of tTemplate
* @attention if not find, will be return 0xffffffff
*/
template<typename T> DWORD
AL_ListCircularSingle<T>::Find(const T& tTemplate ) const
{
if (TRUE == IsEmpty()) {
return LISTSINGLE_POSITION_INVALID;
} AL_Node<T>* pMove = NULL;
DWORD dwCount = 1; //loop the next data;
pMove = m_pHeader->m_pNext;
while (m_pHeader != pMove->m_pNext) {
if (tTemplate == pMove->m_data) {
//find the data
return dwCount-1;
}
dwCount ++;
pMove = pMove->m_pNext;
} //the end
if (tTemplate == pMove->m_data) {
//find the data
return dwCount-1;
} return LISTSINGLE_POSITION_INVALID;
} /**
* IsElement
*
* @param const T& tTemplate
* @return BOOL
* @note the tTemplate is in the list?
* @attention
*/
template<typename T> BOOL
AL_ListCircularSingle<T>::IsElement(const T& tTemplate ) const
{
if (LISTSINGLE_POSITION_INVALID == Find(tTemplate )) {
return FALSE;
} return TRUE;
} /**
* Insert
*
* @param const T& tTemplate
* @param DWORD dwIndex
* @return BOOL
* @note inset the tTemplate into the list at the position
* @attention
*/
template<typename T> BOOL
AL_ListCircularSingle<T>::Insert(DWORD dwIndex, const T& tTemplate )
{
if (dwIndex > Length()) {
//can not insert to this position
return FALSE;
}
AL_Node<T>* pInsert = new AL_Node<T>;
pInsert->m_data = tTemplate; AL_Node<T>* pPre = NULL;
//get the previous Node
if (0x00 == dwIndex) {
pPre = m_pHeader;
}
else {
pPre = GetNodeByIndex(dwIndex - 1);
} if ((NULL == pPre)) {
//error
return FALSE;
}
if (Length() == dwIndex){
//end
pPre->m_pNext = pInsert;
pInsert->m_pNext = m_pHeader;
}
else {
//among of the list
AL_Node<T>* pIndexNode = GetNodeByIndex(dwIndex);
if ((NULL == pIndexNode)) {
//error
return FALSE;
}
pInsert->m_pNext = pIndexNode;
pPre->m_pNext = pInsert;
}
return TRUE;
} /**
* InsertBegin
*
* @param const T& tTemplate
* @return BOOL
* @note inset the tTemplate into the list at the position
* @attention
*/
template<typename T> BOOL
AL_ListCircularSingle<T>::InsertBegin(const T& tTemplate )
{
return Insert(0, tTemplate);
} /**
* InsertEnd
*
* @param const T& tTemplate
* @return BOOL
* @note inset the tTemplate into the list at the position
* @attention
*/
template<typename T> BOOL
AL_ListCircularSingle<T>::InsertEnd(const T& tTemplate )
{
return Insert(Length(), tTemplate);
} /**
* Remove
*
* @param const T& tTemplate
* @return BOOL
* @note remove the tTemplate into the list
* @attention
*/
template<typename T> BOOL
AL_ListCircularSingle<T>::Remove(const T& tTemplate )
{
if (TRUE == IsEmpty()) {
return FALSE;
} DWORD dwPosition = Find(tTemplate);
if (LISTSINGLE_POSITION_INVALID == dwPosition) {
//can not find the data
return FALSE;
} AL_Node<T>* pDelete = GetNodeByIndex(dwPosition);
if (NULL == pDelete) {
//error
return FALSE;
} AL_Node<T>* pPre = NULL;
//get the previous Node
if (0x00 == dwPosition) {
pPre = m_pHeader;
}
else {
pPre = GetNodeByIndex(dwPosition - 1);
} if (NULL == pPre) {
//error
return FALSE;
}
pPre->m_pNext = pDelete->m_pNext; delete pDelete;
pDelete = NULL;
return TRUE;
} /**
* IsEmpty
*
* @param
* @return BOOL
* @note the list has data?
* @attention
*/
template<typename T> BOOL
AL_ListCircularSingle<T>::IsEmpty() const
{
return (m_pHeader == m_pHeader->m_pNext) ? TRUE:FALSE;
} /**
* Get
*
* @param
* @return T
* @note get the T at the position
* @attention the dwIndex must is little than the list length
*/
template<typename T> T
AL_ListCircularSingle<T>::Get(DWORD dwIndex) const
{
T tTypeTemp;
memset(&tTypeTemp, 0x00, sizeof(T)); if (TRUE == IsEmpty()) {
//error
return tTypeTemp;
} if (Length()-1 < dwIndex) {
//error
return tTypeTemp;
} AL_Node<T>* pGet = GetNodeByIndex(dwIndex);
if (NULL == pGet) {
//error
return tTypeTemp;
}
return pGet->m_data; /* //test code
T tTypeTemp;
if (TRUE == IsEmpty()) {
//error
return tTypeTemp;
} if (Length()-1 < dwIndex) {
//error
return tTypeTemp;
} AL_Node<T>* pGet = GetNodeByIndex(dwIndex);
if (NULL == pGet) {
//error
return tTypeTemp;
} AL_Node<T>* pStop = pGet;
while(pStop != pGet->m_pNext) {
if (m_pHeader == pGet) {
//header no data
}
pGet = pGet->m_pNext;
}
*/
} /**
* Set
*
* @param DWORD dwIndex
* @param const T& tTemplate
* @return T
* @note Replaced with the element element element on position index, and returns the old element...
* @attention Index must in the list
*/
template<typename T> T
AL_ListCircularSingle<T>::Set(DWORD dwIndex, const T& tTemplate )
{
T tTypeTemp;
memset(&tTypeTemp, 0x00, sizeof(T)); if (Length()-1 < dwIndex) {
//error
return tTypeTemp;
} AL_Node<T>* pSet = GetNodeByIndex(dwIndex);
if (NULL == pSet) {
//error
return tTypeTemp;
} tTypeTemp = pSet->m_data;
pSet->m_data = tTemplate;
return tTypeTemp;
} /**
* Clear
*
* @param VOID
* @return VOID
* @note clear the data in the list
* @attention all data will be clear
*/
template<typename T> VOID
AL_ListCircularSingle<T>::Clear()
{
if (TRUE == IsEmpty()) {
//No data,
return;
} AL_Node<T>* pDelete = NULL;
while(m_pHeader != m_pHeader->m_pNext){
//get the node
pDelete = m_pHeader->m_pNext;
m_pHeader->m_pNext = pDelete->m_pNext;
delete pDelete;
pDelete = NULL;
}
} /**
* GetNodeByIndex
*
* @param
* @return BOOL
* @note get the const T& at the position
* @attention the dwIndex must is little than the list length
*/
template<typename T> AL_Node<T>*
AL_ListCircularSingle<T>::GetNodeByIndex(DWORD dwIndex) const
{
if (Length()-1 < dwIndex) {
//error
return NULL;
} AL_Node<T>* pMove = NULL;
DWORD dwCount = 1; //loop the next data;
pMove = m_pHeader->m_pNext;
while (m_pHeader != pMove->m_pNext) {
if (dwCount-1 == dwIndex) {
//get this place
return pMove;
}
dwCount ++;
pMove = pMove->m_pNext;
} //the end
return pMove;
} #endif // CXX_AL_LISTCIRCULARSINGLE_H
/* EOF */
测试代码
#ifdef TEST_AL_LISTCIRCULARSINGLE
AL_ListCircularSingle<DWORD> cListCircularSingle;
BOOL bEmpty = cListCircularSingle.IsEmpty();
std::cout<<bEmpty<<std::endl; int array[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
for(int i=0;i<15;i++)
cListCircularSingle.Insert(cListCircularSingle.Length(), array[i]);
bEmpty = cListCircularSingle.IsEmpty();
std::cout<<bEmpty<<std::endl; //test the interface
DWORD dwListSeqLen = cListCircularSingle.Length();
std::cout<<dwListSeqLen<<std::endl; DWORD dwFind = cListCircularSingle.Find(16);
std::cout<<dwFind<<std::endl;
dwFind = cListCircularSingle.Find(12);
std::cout<<dwFind<<std::endl; BOOL bElement = cListCircularSingle.IsElement(16);
std::cout<<bElement<<std::endl;
bElement = cListCircularSingle.IsElement(14);
std::cout<<bElement<<std::endl; BOOL bInsert = cListCircularSingle.Insert(0, 0);
std::cout<<bInsert<<std::endl;
bInsert = cListCircularSingle.Insert(16, 16);
std::cout<<bInsert<<std::endl;
bInsert = cListCircularSingle.Insert(16, 999);
std::cout<<bInsert<<std::endl; BOOL bRemove = cListCircularSingle.Remove(9846354);
std::cout<<bRemove<<std::endl;
bRemove = cListCircularSingle.Remove(999);
std::cout<<bRemove<<std::endl; bRemove = cListCircularSingle.Remove(10);
std::cout<<bRemove<<std::endl; INT it = 0x00;
for (DWORD i=0; i<cListCircularSingle.Length(); i++) {
it = cListCircularSingle.Get(i);
std::cout<<it<<std::endl;
} DWORD dwSet = cListCircularSingle.Set(16, 999);
std::cout<<dwSet<<std::endl;
dwSet = cListCircularSingle.Set(0, 888);
std::cout<<dwSet<<std::endl;
dwSet = cListCircularSingle.Set(11, 777);
std::cout<<dwSet<<std::endl; for (DWORD i=0; i<cListCircularSingle.Length(); i++) {
it = cListCircularSingle.Get(i);
std::cout<<it<<std::endl;
} cListCircularSingle.Clear();
bEmpty = cListCircularSingle.IsEmpty();
std::cout<<bEmpty<<std::endl;
dwListSeqLen = cListCircularSingle.Length();
std::cout<<dwListSeqLen<<std::endl; bInsert = cListCircularSingle.Insert(1, 999);
std::cout<<bInsert<<std::endl; bInsert = cListCircularSingle.Insert(0, 666);
std::cout<<bInsert<<std::endl;
bRemove = cListCircularSingle.Remove(666);
std::cout<<bRemove<<std::endl;
bEmpty = cListCircularSingle.IsEmpty();
std::cout<<bEmpty<<std::endl;
dwListSeqLen = cListCircularSingle.Length();
std::cout<<dwListSeqLen<<std::endl; #endif
※数据结构※→☆线性表结构(list)☆============单向循环链表结构(list circular single)(四)的更多相关文章
- 数据结构----线性表顺序和链式结构的使用(c)
PS:在学习数据结构之前,我相信很多博友也都学习过一些语言,比如说java,c语言,c++,web等,我们之前用的一些方法大都是封装好的,就java而言,里面使用了大量的封装好的方法,一些算法也大都写 ...
- C++线性表的链式存储结构
C++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...
- C# 数据结构 线性表(顺序表 链表 IList 数组)
线性表 线性表是最简单.最基本.最常用的数据结构.数据元素 1 对 1的关系,这种关系是位置关系. 特点 (1)第一个元素和最后一个元素前后是没有数据元素,线性表中剩下的元素是近邻的,前后都有元素. ...
- [从今天开始修炼数据结构]线性表及其实现以及实现有Itertor的ArrayList和LinkedList
一.线性表 1,什么是线性表 线性表就是零个或多个数据元素的有限序列.线性表中的每个元素只能有零个或一个前驱元素,零个或一个后继元素.在较复杂的线性表中,一个数据元素可以由若干个数据项组成.比如牵手排 ...
- [数据结构-线性表1.2] 链表与 LinkedList<T>(.NET 源码学习)
[数据结构-线性表1.2] 链表与 LinkedList<T> [注:本篇文章源码内容较少,分析度较浅,请酌情选择阅读] 关键词:链表(数据结构) C#中的链表(源码) 可空类 ...
- Java数据结构-线性表之单链表LinkedList
线性表的链式存储结构,也称之为链式表,链表:链表的存储单元能够连续也能够不连续. 链表中的节点包括数据域和指针域.数据域为存储数据元素信息的域,指针域为存储直接后继位置(一般称为指针)的域. 注意一个 ...
- C语言 严蔚敏数据结构 线性表之链表实现
博主最近在考成都大学皇家计算机科学与技术专业,复习专业课数据结构,正好学习到线性结构中的线性表用链表这种存储结构来实现. 首先,数据结构包括1.数据的操作2.逻辑结构3.存储结构(数据结构三要素. 直 ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)
循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- c数据结构 -- 线性表之 复杂的链式存储结构
复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...
随机推荐
- 一幅图的知识科普--Google免费DNS服务器
学习源于问题的解决 前段时间在虚拟机上安装了一个oracle软件,虚拟机和宿主机器的网络连接方式是桥接模式的,手动设置了静态ip地址,网关地址,但是发现虚拟机的浏览器不能用. 于是通过ping测试了一 ...
- Android 学习开发笔记《Android认识》
1. Android:2007年11月5日第一版,2009年5月豪华版,2010年HTC手机 2. Android框架主要:应用程序.应用程序框架.函数库.运行时. ...
- 我的Python成长之路---第七天---Python基础(21)---2016年2月27日(晴)
四.面向对象进阶 1.类方法 普通的方法通过对象调用,至少有一个self参数(调用的时候系统自动传递,不需要手工传递),而类方法由类直接调用,至少有一个cls参数,执行时,自动将调用该方法的类赋值个c ...
- HYSBZ 2818 gcd
/** 大意: 给定整数N,1<= x,y <= N 求解有多少gcd(x,y) 为素数 n=10^7 思路: 首先考虑到n 如此之大,用的快速求欧拉函数. 先默认 y〉x 分析: gcd ...
- js触屏事件
js的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend.这三个事件最重要的属性是 pageX和 pageY,表示X,Y坐标. touchstart在触摸开始 ...
- java学习之字符流与字节流的转换
package com.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExce ...
- Windows 8.1 RTM初体验
Windows 8.1正式发布在10月17日,现在可以在MSDN/TechNet进行订阅下载. 操作系统版本号和Windows Server 2012 R2保持一致. 开始屏幕动态磁贴现在有4种尺寸可 ...
- Android 使用动态载入框架DL进行插件化开发
如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 (来自时之沙的csdn博客) 概述: 随着应用的不断迭代.应用的体积不断增大,项目越来越臃肿,冗余添 ...
- SEO市场是在扩大还是缩小 Seoer终于会变成什么?
近期有两件全然背道而驰的事情同一时候发生.第一件事情是以SEO业务为主要业务的业者逐渐降低,很多原本是SEO的业者都纷纷转向其它业务.SEO业务反而变成副业.第二件事情是中小企业的SEO需求添加了.而 ...
- 完整的Android手机短信验证源码
短信验证功能我分两个模块来说,短信验证码的后台和代码实现短信验证码的功能. 一.短信验证码的后台 1.注册Mob账号:http://www.mob.com/#/login 2.注册成功之后, ...