用C++模板技术写的单链表
2006-07-27 14:57
381 查看
第一段应用C++类模板技术写的东西,并且用在现在的系统中,效果还不错,感觉代码还不够精简。
//单链表模板类
using namespace std;
template <class ElemType>
class CSingleLink{
private:
ElemType* phead; //头指针
ElemType* ptail; //尾指针
ElemType* pcurr; //当前指针
int NodesNum; //节点数量
int CurrPos; //当前指针的序号(第一个节点的序号为1)
protected:
public:
CSingleLink();
~CSingleLink();
int GetNNum(); //返回节点数量
int GetIndex(); //返回当前指针的序号
ElemType* First();
ElemType* Next(); //返回当前节点的下一个节点的指针
ElemType* Prev(); //返回当前节点的前一个节点的指针
ElemType* MoveTo(int index); //返回序号为index的节点指针(index'svalue from 1)
ElemType* Insert(int pos,ElemType* aNode);//向第pos个节点插入节点aNode(pos >= 1 )
ElemType* Append(ElemType* aNode); //向单链表末尾追加一个节点aNode
ElemType* Delete(int pos); //删除第pos个节点
int Delete(ElemType* p); //删除某个节点
void Clear(); //清除链表
};
template <class ElemType>
CSingleLink<ElemType>::CSingleLink()
{
phead = NULL;
ptail = NULL;
pcurr = NULL;
NodesNum = 0;
CurrPos = 0;
}
template <class ElemType>
CSingleLink<ElemType>::~CSingleLink()
{
Clear();
}
template <class ElemType>
int CSingleLink<ElemType>::GetNNum(){
return (NodesNum);
}
template <class ElemType>
int CSingleLink<ElemType>::GetIndex(){
return(CurrPos);
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::First(){
if(NodesNum > 0){
pcurr = phead;
CurrPos = 1;
return (pcurr);
}
else return NULL;
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Next(){
if(NodesNum > 0){
pcurr = pcurr->pnext;
if(pcurr != NULL)
CurrPos++;
return(pcurr);
}
else return NULL;
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Prev(){
if(NodesNum > 1){ //NodesNum = 1只有头结点
ElemType* pN = phead;
while(pN->next != pcurr)
pN = pN->next;
4000
pcurr = pN;
CurrPos--;
return(pcurr);
}
else return NULL;
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::MoveTo(int index){
if(NodesNum > 0 && index >0 && index <= NodesNum){
pcurr = phead;
CurrPos = 1;
for(int i = 1;i < index; i++)
this->Next();
return(pcurr);
}
else return NULL;
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Insert(int pos,ElemType* aNode){
if(pos < 1 || pos > NodesNum || NodesNum == 0) return NULL;
if(NodesNum == 0){
phead = aNode;
ptail = aNode;
aNode->next = NULL;
CurrPos = 1;
NodesNum = 1;
}
else if(pos == 1){
aNode->next = phead;
phead = aNode;
CurrPos = 1;
NodesNum++;
}
else if(this->MoveTo(pos - 1) != NULL){
aNode->next = pcurr->next;
pcurr->next = aNode;
pcurr = aNode;
CurrPos = pos;
NodesNum++;
}
pcurr = aNode;
return(pcurr);
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Append(ElemType* aNode){
if(NodesNum == 0){
phead = aNode;
ptail = aNode;
}
else {//if(this->MoveTo(NodesNum) != NULL){
ptail->pnext = aNode;
ptail = aNode;
}
NodesNum++;
aNode->pnext = NULL;
pcurr = aNode;
CurrPos = NodesNum;
return (pcurr);
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Delete(int pos){
if(pos < 1 || pos > NodesNum || NodesNum == 0) return NULL;
if(pos == 1){
this->MoveTo(1);
phead = phead->next;
}else if(this->MoveTo(pos - 1) != NULL){
pcurr->next = pcurr->next->next;
}
NodesNum--;
CurrPos = pos - 1;
return(pcurr);
}
template <class ElemType>
int CSingleLink<ElemType>::Delete(ElemType* p){
if(NodesNum > 0){
int r = 1;
pcurr = this->First();
while(pcurr != NULL)
{
if(pcurr != p)
{
pcurr = this->Next();
r++;
}
else break;
}
if(pcurr != NULL)
{
delete pcurr;
NodesNum--;
CurrPos = r - 1;
return r;
}else return NULL;
}
else return NULL;
}
template <class ElemType>
void CSingleLink<ElemType>::Clear(){
if(NodesNum > 0)
{
ElemType *aNode;
for(int i = 0; i< NodesNum;i++){
aNode = pcurr;
delete aNode;
if(pcurr != NULL)
pcurr = pcurr->pnext;
}
}
phead = NULL;
ptail = NULL;
pcurr = NULL;
NodesNum = 0;
CurrPos = 0;
}
//单链表模板类
using namespace std;
template <class ElemType>
class CSingleLink{
private:
ElemType* phead; //头指针
ElemType* ptail; //尾指针
ElemType* pcurr; //当前指针
int NodesNum; //节点数量
int CurrPos; //当前指针的序号(第一个节点的序号为1)
protected:
public:
CSingleLink();
~CSingleLink();
int GetNNum(); //返回节点数量
int GetIndex(); //返回当前指针的序号
ElemType* First();
ElemType* Next(); //返回当前节点的下一个节点的指针
ElemType* Prev(); //返回当前节点的前一个节点的指针
ElemType* MoveTo(int index); //返回序号为index的节点指针(index'svalue from 1)
ElemType* Insert(int pos,ElemType* aNode);//向第pos个节点插入节点aNode(pos >= 1 )
ElemType* Append(ElemType* aNode); //向单链表末尾追加一个节点aNode
ElemType* Delete(int pos); //删除第pos个节点
int Delete(ElemType* p); //删除某个节点
void Clear(); //清除链表
};
template <class ElemType>
CSingleLink<ElemType>::CSingleLink()
{
phead = NULL;
ptail = NULL;
pcurr = NULL;
NodesNum = 0;
CurrPos = 0;
}
template <class ElemType>
CSingleLink<ElemType>::~CSingleLink()
{
Clear();
}
template <class ElemType>
int CSingleLink<ElemType>::GetNNum(){
return (NodesNum);
}
template <class ElemType>
int CSingleLink<ElemType>::GetIndex(){
return(CurrPos);
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::First(){
if(NodesNum > 0){
pcurr = phead;
CurrPos = 1;
return (pcurr);
}
else return NULL;
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Next(){
if(NodesNum > 0){
pcurr = pcurr->pnext;
if(pcurr != NULL)
CurrPos++;
return(pcurr);
}
else return NULL;
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Prev(){
if(NodesNum > 1){ //NodesNum = 1只有头结点
ElemType* pN = phead;
while(pN->next != pcurr)
pN = pN->next;
4000
pcurr = pN;
CurrPos--;
return(pcurr);
}
else return NULL;
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::MoveTo(int index){
if(NodesNum > 0 && index >0 && index <= NodesNum){
pcurr = phead;
CurrPos = 1;
for(int i = 1;i < index; i++)
this->Next();
return(pcurr);
}
else return NULL;
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Insert(int pos,ElemType* aNode){
if(pos < 1 || pos > NodesNum || NodesNum == 0) return NULL;
if(NodesNum == 0){
phead = aNode;
ptail = aNode;
aNode->next = NULL;
CurrPos = 1;
NodesNum = 1;
}
else if(pos == 1){
aNode->next = phead;
phead = aNode;
CurrPos = 1;
NodesNum++;
}
else if(this->MoveTo(pos - 1) != NULL){
aNode->next = pcurr->next;
pcurr->next = aNode;
pcurr = aNode;
CurrPos = pos;
NodesNum++;
}
pcurr = aNode;
return(pcurr);
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Append(ElemType* aNode){
if(NodesNum == 0){
phead = aNode;
ptail = aNode;
}
else {//if(this->MoveTo(NodesNum) != NULL){
ptail->pnext = aNode;
ptail = aNode;
}
NodesNum++;
aNode->pnext = NULL;
pcurr = aNode;
CurrPos = NodesNum;
return (pcurr);
}
template <class ElemType>
ElemType* CSingleLink<ElemType>::Delete(int pos){
if(pos < 1 || pos > NodesNum || NodesNum == 0) return NULL;
if(pos == 1){
this->MoveTo(1);
phead = phead->next;
}else if(this->MoveTo(pos - 1) != NULL){
pcurr->next = pcurr->next->next;
}
NodesNum--;
CurrPos = pos - 1;
return(pcurr);
}
template <class ElemType>
int CSingleLink<ElemType>::Delete(ElemType* p){
if(NodesNum > 0){
int r = 1;
pcurr = this->First();
while(pcurr != NULL)
{
if(pcurr != p)
{
pcurr = this->Next();
r++;
}
else break;
}
if(pcurr != NULL)
{
delete pcurr;
NodesNum--;
CurrPos = r - 1;
return r;
}else return NULL;
}
else return NULL;
}
template <class ElemType>
void CSingleLink<ElemType>::Clear(){
if(NodesNum > 0)
{
ElemType *aNode;
for(int i = 0; i< NodesNum;i++){
aNode = pcurr;
delete aNode;
if(pcurr != NULL)
pcurr = pcurr->pnext;
}
}
phead = NULL;
ptail = NULL;
pcurr = NULL;
NodesNum = 0;
CurrPos = 0;
}
相关文章推荐
- 【C++】建立一个学生信息链表,每个结点包括:学号、姓名、成绩。【原创技术】
- [C++模板技术] 类型映射为类型
- C++利用链表与模板实现栈
- 采用C++模板技术,实现的分布式哈希存储服务器
- C++模板实现泛型链表
- 我的C++实践(4):trait与policy模板技术http://blog.csdn.net/zhoudaxia/article/details/4486487
- C++中的模板技术
- 【C++】模板实现双链表和队列
- C++模板链表实现优先级队列
- C++ 双向链表 使用模板 增删操作
- 【C++】模板实现带头节点的双向循环链表
- C++模板技术
- [C++模板技术] 线性继承体系自动生成
- [C++模板技术] 整数映射为类型
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- 链表C++模板实现
- c++ 模板 链表实现
- C++利用链表与模板实现栈
- 插入排序算法,c++ 模板技术表述,完全兼容标准库容器!!!
- 简单单向链表(C++模版技术实现)