#ifndef STL_LINK_SEQUENCESTACK_H
#define STL_LINK_SEQUENCESTACK_H
/************************************************************************/
/* 以下是顺序栈(数组)C++ 简单不在注释
/************************************************************************/
enum Status
{
EMPTY,
HAVE,
FULL
};
template <class Elemplent>
class SequenceStack
{
protected:
int top;
int maxSize;
int countTotal;
Elemplent *ptr;
public:
SequenceStack(int size = 1000);
SequenceStack(const SequenceStack<Elemplent>& tempPtr );
SequenceStack<Elemplent> & operator= (const SequenceStack<Elemplent>& tempPtr);
~SequenceStack();
void InitSequenceStack(int size);
public:
int GetLength();
Status GetStatus();
bool Push(const Elemplent& tempElemplent);
bool GetTop(Elemplent & tempElemplent) const;
bool Pop();
bool Clear();
};
template<class Elemplent>
SequenceStack<Elemplent>::SequenceStack(int size )
{
ptr = NULL;
InitSequenceStack(size);
}
template<class Elemplent>
SequenceStack<Elemplent>::~SequenceStack()
{
Clear();
delete []ptr;
}
template <class Elemplent>
void SequenceStack<Elemplent>::InitSequenceStack(int size)
{
maxSize =size;
ptr = new Elemplent[maxSize]
countTotal = 0;
}
template <class Elemplent>
int SequenceStack<Elemplent>::GetLength()
{
return countTotal;
}
template <class Elemplent>
Status SequenceStack<Elemplent>::GetStatus()
{
if (top == 0)
{
return EMPTY;
}
if (top ==maxSize)
{
return FULL;
}
return HAVE;
}
template <class Elemplent>
bool SequenceStack<Elemplent>::Push(const Elemplent& tempElemplent)
{
if (FULL == GetStatus())
{
return false;
}
ptr[top++] = tempElemplent;
countTotal++;
return true;
}
template<class Elemplent>
bool SequenceStack<Elemplent>::GetTop(Elemplent & tempElemplent)const
{
if (EMPTY == GetStatus())
{
return false;
}
tempElemplent = ptr[top];
return true;
}
template<class Elemplent>
bool SequenceStack<Elemplent>::Pop()
{
if (EMPTY == GetStatus())
{
return false;
}
top --;
countTotal--;
return true;
}
template<class Elemplent>
bool SequenceStack<Elemplent>::Clear()
{
if (EMPTY == GetStatus())
{
return true;
}
top = 0;
countTotal = 0;
return true;
}
template<class Elemplent>
SequenceStack<Elemplent>::SequenceStack(const SequenceStack<Elemplent>& tempPtr )
{
int length = tempPtr.countTotal;
InitSequenceStack(tempPtr.maxSize);
for (int i = 1;i<=length;i++)
{
ptr[i-1] = tempPtr.ptr[i-1];
}
return *this;
}
template<class Elemplent>
SequenceStack<Elemplent>::operator=(const SequenceStack<Elemplent>& tempPtr )
{
if (this = &tempPtr)
{
return *this;
}
int length = tempPtr.GetLength();
Clear();
InitSequenceStack(tempPtr.maxSize);
for (int i = 1;i<= length;i++)
{
ptr[i-1] = tempPtr.ptr[i-1];
}
return *this;
}
#endif
数据结构之顺序栈(数组)C++(模板)
最新推荐文章于 2021-01-09 22:25:16 发布