#ifndef STL_LINK_SEQUENCELIST_H
#define STL_LINK_SEQUENCELIST_H
/************************************************************************/
/* 以下为链表实现栈——C++
/************************************************************************/
/************************************************************************/
/* 节点类
/************************************************************************/
//结点定义
template <class Elemplent>
class CNode
{
public:
Elemplent data;
CNode<Elemplent> *next;
public:
CNode<Elemplent> ();
CNode(Elemplent tempElemplent,CNode<Elemplent>* tempNext = NULL);
~CNode();
};
//结点构造函数
template<class Elemplent>
CNode<Elemplent>::CNode()
{
next = NULL;
}
//结点析构函数
template<class Elemplent>
CNode<Elemplent>::~CNode()
{
}
//创建新节点函数
template<class Elemplent>
CNode<Elemplent>::CNode(Elemplent tempElemplent,CNode<Elemplent>* tempNext )
{
data = tempElemplent;
next = tempNext;
}
/************************************************************************/
/* 链表类实现
/************************************************************************/
enum Status
{
EMPTY,
HAVE,
FULL
};
template <class Elemplent>
class SquenceList
{
protected:
int CountTotal;
CNode<Elemplent> *topPtr;
public:
SquenceList();
~SquenceList();
void InitSquenceList();
SquenceList(const SquenceList<Elemplent>& tempptr);
SquenceList<Elemplent>& operator = (const SquenceList<Elemplent>& tempptr);
public:
int GetLength();
bool IsEmpty();
bool Clear();
bool GetTop(Elemplent & temp);
bool Push(const Elemplent& temp);
bool Pop();
};
template <class Elemplent>
SquenceList<Elemplent>::SquenceList()
{
InitSquenceList();
}
template<class Elemplent>
void SquenceList<Elemplent>::InitSquenceList()
{
CountTotal = 0;
topPtr = new CNode<Elemplent>;
}
template <class Elemplent>
int SquenceList<Elemplent>::GetLength()
{
return CountTotal;
}
template <class Elemplent>
bool SquenceList<Elemplent>::IsEmpty()
{
return topPtr ==NULL;
}
template <class Elemplent>
bool SquenceList<Elemplent>::GetTop(Elemplent & temp)
{
if (IsEmpty())
{
return false;
}
temp = topPtr->data;
return true;
}
template <class Elemplent>
bool SquenceList<Elemplent>::Push(const Elemplent& temp)
{
//it's fault.
//CNode<Elemplent> * newptr =new CNode<Elemplent>(temp,NULL);
CNode<Elemplent>* newptr = new CNode<Elemplent> (temp,topPtr);
topPtr= newptr;
CountTotal ++;
return true;
}
template<class Elemplent>
bool SquenceList<Elemplent>::Pop()
{
if (IsEmpty())
{
return false;
}
CNode<Elemplent> * deleteptr =topPtr;
topPtr = topPtr->next;
delete deleteptr;
CountTotal--;
return true;
}
template<class Elemplent>
bool SquenceList<Elemplent>::Clear()
{
while(IsEmpty()!)
{
Pop();
}
return false
}
template<class Elemplent>
SquenceList<Elemplent>::SquenceList(const SquenceList<Elemplent>& tempptr)
{
int length =tempptr.GetLength;
Clear();
InitSquenceList();
CNode<Elemplent> *curptr = tempptr.topPtr;
//因为是FILO所以,为了复制,必要倒叙一下,
for (int i = 1;i<=length;i++)
{
for (int j = 1;j<=length-i;j++)
{
curptr =curptr->next;
}
topPtr->data = curptr->next;
curptr = tempptr.topPtr;
}
return *this;
}
template <class Elemplent>
SquenceList<Elemplent>& SquenceList<Elemplent>::operator=(const SquenceList<Elemplent>& tempptr)
{
if (this == &tempptr)
{
return *this;
}
int length =tempptr.GetLength;
Clear();
InitSquenceList();
CNode<Elemplent> *curptr = tempptr.topPtr;
//因为是FILO所以,为了复制,必要倒叙一下,
for(int i = 1;i<=length;i++)
{
for (int j = 1;j<=length-i;j++)
{
curptr =curptr->next;
}
topPtr->data = curptr->next;
curptr = tempptr.topPtr;
}
return this;
}
#endif
数据结构之单链表实现栈(C++)
最新推荐文章于 2021-07-16 15:02:14 发布