📄 dchain.hpp
字号:
// 文件名:DChain.hpp
//文本:
// 本文件提供双向链表类,该链表中的每一个结构有两个指针,一个指向上一
// 个结构,另一个指向下一个结构,最后一个结构的指向下一个结构的指针指向0
// ,由此构成一个循环链表结构。
//
//
//
//
//
//创建人:陈子牙
//创建日期:2006/04/15
#ifndef ERR
#define ERR -1
#endif
#define OK 1
//
// 双向链表类
//
//
//
//
//
//
template <class Type>
class CDchainP{
protected:
//
//双向链表子项类
//
//
//
//
template <class Type>
class CDchainInfoP{
public:
int hchain; //该子项的唯一标识符
CDchainInfoP<Type>* p_next_note; //指向下一个链表子项的指针
CDchainInfoP<Type>* p_last_note; //指向上一个链表子项的指针
public:
Type* ptype; //指向数据结构的指针
};
int m_count; //用于记录该链表中子项的数目
int m_handcount; //用于给每个链表子项赋值
CDchainInfoP<Type>* p_first_item; //指向第一个链表子项的指针
CDchainInfoP<Type>* p_end_item; //指向最后一个链表子项的指针
CDchainInfoP<Type>* p_current_item;
public:
//
//
//
CDchainP()
{
m_count=0;
m_handcount=0;
p_first_item=(CDchainInfoP<Type>*)0;
p_end_item=(CDchainInfoP<Type>*)0;
p_current_item=(CDchainInfoP<Type>*)0;
}
//
//
//
~CDchainP()
{
CDchainInfoP<Type>* pcurrent=p_first_item;
for(int i=0;i<m_count;i++)
{
}
}
//在链表尾插入数据
//输入参数:
//返回参数:
//版本: V1.0.0
int fnAddItem(Type* pdat)
{
CDchainInfoP<Type> *newchain=new CDchainInfoP<Type>;
newchain->ptype=pdat;
if(m_count==0)
{
newchain->p_last_note=0;
newchain->p_next_note=0;
p_first_item=newchain;
p_end_item=newchain;
p_current_item=newchain;
}
else
{
newchain->p_last_note=p_end_item;
newchain->p_next_note=0;
p_end_item->p_next_note=newchain;
p_end_item=newchain;
}
++m_handcount;
++m_count;
newchain->hchain=m_handcount;
return m_handcount;
}
//以顺序位置为变量插入数据
//输入参数:
//返回参数:
//版本: V1.0.0
int fnAddItem(Type* pdat,int sequence)
{int i=0;
CDchainInfoP<Type> *newchain=new CDchainInfoP<Type>;
CDchainInfoP<Type>* pcurrent=p_first_item;
newchain->ptype=pdat;
if(m_count==0)
{
return fnAddItem(pdat);
}
else
{
if(sequence == 0)
{
newchain->p_last_note=0;
newchain->p_next_note=p_first_item;
pcurrent->p_last_note=newchain;
p_first_item=newchain;
++m_handcount;
++m_count;
newchain->hchain=m_handcount;
return m_handcount;
}
else if(sequence >= m_count)
{
return fnAddItem(pdat);
}
else
{
while(++i<sequence)
{
pcurrent=pcurrent->p_next_note;
}
newchain->p_last_note=pcurrent;
newchain->p_next_note=pcurrent->p_next_note;
(pcurrent->p_next_note)->p_last_note=newchain;
pcurrent->p_next_note=newchain;
++m_handcount;
++m_count;
newchain->hchain=m_handcount;
return m_handcount;
}
}
}
//以句柄删除数据
//输入参数:
//返回参数:
//版本: V1.0.0
void fnDelItem(int hchain)
{int i=0;
CDchainInfoP<Type>* pcurrent=p_first_item;
p_current_item=p_first_item;
if(m_count==0)
{
return;
}
if(m_count==1)
{
p_first_item=0;
p_end_item=0;
p_current_item=0;
m_count--;
delete pcurrent;
return ;
}
for(i=0;i<m_count;i++)
{
if(pcurrent->hchain==hchain)
{
if(i==0)
{
(pcurrent->p_next_note)->p_last_note=0;
m_count--;
delete pcurrent;
return;
}
if(i==m_count-1)
{
(pcurrent->p_last_note)->p_next_note=0;
m_count--;
delete pcurrent;
return;
}
(pcurrent->p_next_note)->p_last_note=pcurrent->p_last_note;
(pcurrent->p_last_note)->p_next_note=pcurrent->p_next_note;
m_count--;
delete pcurrent;
return;
}
pcurrent=pcurrent->p_next_note;
}
}
//以顺序位置删除数据
//输入参数:
//返回参数:
//版本: V1.0.0
int fnDelItemWithSequence(int sequence)
{
CDchainInfoP<Type>* pcurrent=p_first_item;
p_current_item=p_first_item;
if(m_count==0)
{
return ERR;
}
if(m_count==1)
{
p_first_item=0;
p_end_item=0;
p_current_item=0;
m_count--;
delete pcurrent;
return OK;
}
if(sequence==m_count-1)
{
pcurrent=p_end_item;
(p_end_item->p_last_note)->p_next_note=0;
p_end_item=p_end_item->p_last_note;
m_count--;
delete pcurrent;
return OK;
}
if(sequence==0)
{
(p_first_item->p_next_note)->p_last_note=0;
p_first_item=p_first_item->p_next_note;
m_count--;
delete pcurrent;
return OK;
}
if(sequence >= m_count)
return ERR;
int i=0;
while(i++<sequence)
{
pcurrent=pcurrent->p_next_note;
}
m_count--;
(pcurrent->p_last_note)->p_next_note=pcurrent->p_next_note;
(pcurrent->p_next_note)->p_last_note=pcurrent->p_last_note;
delete pcurrent;
return OK;
}
//以句柄获取数据
//输入参数:
//返回参数:
//版本: V1.0.0
Type* fnGet(int hchain)
{
CDchainInfoP<Type>* pcurrent=p_first_item;
for(int i=0;i<m_count;i++)
{
if(pcurrent->hchain==hchain)
{
return pcurrent->ptype;
}
pcurrent=pcurrent->p_next_note;
}
return (Type*)0;
}
//以顺序位置获取数据
//输入参数:
//返回参数:
//版本: V1.0.0
Type* operator [](int sequence)
{
CDchainInfoP<Type>* pcurrent=p_first_item;
if(sequence >= m_count)
return (Type*)0;
for(int i=0;i<sequence;i++)
{
pcurrent=pcurrent->p_next_note;
}
return pcurrent->ptype;
}
//获取结构的数目
//输入参数:
//返回参数:
//版本: V1.0.0
int fnCount(void)
{
return m_count;
}
//获取下一个结构的指针
//输入参数:
//返回参数:
//版本: V1.0.0
Type* operator ++()
{
if(m_count==0)
return(0);
if(p_current_item!=0)
{
if(p_current_item->p_next_note==0)
{
return 0;
}
else
{
p_current_item=p_current_item->p_next_note;
return(p_current_item->ptype);
}
}
else
return 0;
}
//获取上一个结构的指针
//输入参数:
//返回参数:
//版本: V1.0.0
Type* operator --()
{
if(m_count==0)
return(0);
if(p_current_item!=0)
{
if(p_current_item->p_last_note==0)
{
return 0;
}
else
{
p_current_item=p_current_item->p_last_note;
return(p_current_item->ptype);
}
}
else
return 0;
}
//
//输入参数:
//返回参数:
//版本: V1.0.0
void fnprintf(void)
{CDchainInfoP<Type>* pcurrent=p_first_item;
cout<<"m_count :"<<m_count<<endl;
cout<<"m_handcount:"<<m_handcount<<endl;
cout<<"p_first_item:"<<p_first_item<<endl;
cout<<"p_end_item:"<<p_end_item<<endl;
while(pcurrent!=0)
{
cout<<"$$"<<endl;
cout<<"pcurrent->this: "<<pcurrent<<endl;
cout<<"pcurrent->hchain: "<<pcurrent->hchain<<endl;
cout<<"pcurrent->p_next_note: "<<pcurrent->p_next_note<<endl;
cout<<"pcurrent->p_last_note: "<<pcurrent->p_last_note<<endl;
cout<<"pcurrent->ptype: "<<pcurrent->ptype<<endl;
cout<<"$$"<<endl;
pcurrent=pcurrent->p_next_note;
}
}
};
/*
*
*
*
*
*
8
*
*
*
*
*
*
*
*
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -