笔记【数据结构】第三章 栈和队列
特殊线性表——栈
栈:限定仅在表尾进行插入和删除操作的线性表。
空栈:不含任何数据元素的栈。
允许插入和删除的一端称为栈顶,另一端称为栈底。
顺序栈类的声明
const
int MAX_SIZE=100;
template
<class T>
class seqStack
{
public:
seqStack ( ) ;
~seqStack ( );
void Push ( T x );
T Pop ( );
T GetTop ( );
bool Empty ( );
private:
T data[MAX_SIZE];
int top;
}
顺序栈的实现——入栈
操作接口: void Push( T
x );
template <class T>
void
seqStack<T>::Push ( T x)
{
if (top==MAX_SIZE-1) throw “溢出”;
top++;
data[top]=x;
}
顺序栈的实现—判断是否是空栈
操作接口: bool Empty( )
template <class T>
bool
seqStack<T>::Empty ()
{
if (top==-1)
return true;
return false;
}
顺序栈的实现——取栈顶
操作接口: T
GetTop( );
template <class T>
T
seqStack<T>::GetTop ( )
{
if (Empty()) throw ”空栈” ;
return data[top];
}
顺序栈的实现——出栈
操作接口: T Pop( );
template
<class T>
T
seqStack<T>:: Pop ( )
{
if (top==-1) throw “溢出”;
x=data[top];
top--;
return x;
}
两栈共享空间(双端栈