用栈的顺序存储结构实现栈的各种基本操作

来源:百度知道 编辑:UC知道 时间:2024/06/24 18:55:56
用C++编写

一、顺序栈
栈的顺序存储结构简称为顺序栈,它是运算受限的顺序表。因此,可用数组来实现顺序栈。
因为栈底位置是固定不变的,所以可以将栈底位置设置在数组的两端的任何一个端点;
栈顶位置是随着进栈和退栈操作而变化的.

栈的顺序存储表示 — 顺序栈

#include <assert.h>
template <class Type> class Stack {
private:
int top; //栈顶数组指针
Type *elements; //栈数组
int maxSize; //栈最大容量
public:
Stack ( int=10 ); //构造函数
~Stack ( ) {
delete[ ] elements;
}//析构函数

void Push ( const Type & item ); //入栈
Type Pop ( ); //出栈
Type GetTop ( ); //取栈顶元素
void MakeEmpty ( ) { top=-1; } //置空栈
int IsEmpty ( ) const {
return top == -1;
}
int IsFull ( ) const{
return top == maxSize-1;
}

}
void Push ( const Type & item ); //入栈
Type Pop ( ); //出栈
Type GetTop ( ); //取栈顶元素
void MakeEmpty ( ) { top=-1; } //置空栈
int IsEmpty ( ) const {
return top == -1;
}
int IsFull ( ) const {

retur