📄 dchain.hpp
字号:
//
// 双向链表类
//
//
//
//
//
//
template <class Type>
class CDchain{
protected:
//
//双向链表子项类
//
//
//
//
template <class Type>
class CDchainInfo{
public:
int hchain; //该子项的唯一标识符
CDchainInfo<Type>* p_next_note; //指向下一个链表子项的指针
CDchainInfo<Type>* p_last_note; //指向上一个链表子项的指针
public:
Type ptype; //指向数据结构的指针
};
int m_count; //用于记录该链表中子项的数目
int m_handcount; //用于给每个链表子项赋值
CDchainInfo<Type>* p_first_item; //指向第一个链表子项的指针
CDchainInfo<Type>* p_end_item; //指向最后一个链表子项的指针
CDchainInfo<Type>* p_current_item;
public:
//
//
//
CDchain()
{
m_count=0;
m_handcount=0;
p_first_item=(CDchainInfo<Type>*)0;
p_end_item=(CDchainInfo<Type>*)0;
p_current_item=(CDchainInfo<Type>*)0;
}
//
//
//
~CDchain()
{
CDchainInfo<Type>* pcurrent=p_first_item;
for(int i=0;i<m_count;i++)
{
}
}
//在链表尾插入数据
//输入参数:
//返回参数:1~
//版本: V1.0.0
int fnAddItem(Type pdat)
{
CDchainInfo<Type> *newchain=new CDchainInfo<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;
CDchainInfo<Type> *newchain=new CDchainInfo<Type>;
CDchainInfo<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;
CDchainInfo<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;
p_first_item=p_first_item->p_next_note;
m_count--;
delete pcurrent;
return;
}
if(i==m_count-1)
{
(pcurrent->p_last_note)->p_next_note=0;
p_end_item=p_end_item->p_last_note;
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)
{
CDchainInfo<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)
{
CDchainInfo<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;
}
}
Type* fnGetp(int hchain)
{
CDchainInfo<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;
}
}
void fnChange(int hchain,Type type)
{
CDchainInfo<Type>* pcurrent=p_first_item;
for(int i=0;i<m_count;i++)
{
if(pcurrent->hchain==hchain)
{
pcurrent->ptype = type;
return ;
}
pcurrent=pcurrent->p_next_note;
}
}
/*
Type* fnGet(int hchain)
{Type type;
CDchainInfo<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;
}
}*/
/*
//以顺序位置获取数据
//输入参数:
//返回参数:
//版本: V1.0.0
Type operator [](int sequence)
{
CDchainInfo<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)
{CDchainInfo<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;
}
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -