📄 stack.h
字号:
#ifndef STACK_CLASS
#define STACK_CLASS
#include<iostream>
#include<cstdlib>
using namespace std;
const int MaxStackSize=100;
template<class T>
class Stack
{
private:
T stacklist[MaxStackSize]; //数组
int top ; //栈顶位置
public:
Stack(void);
void Push(const T& item); //元素item压入栈
T Pop(void); //输出战
void ClearStack(void); //清空栈
T peek(void) const; //访问栈顶元素
int StackEmpty(void) const;//测试是否栈满
int StackFull(void) const; //测试是否栈空
};
template<class T>
Stack<T>::Stack(void)
{ top=-1;}
template<class T>
void Stack<T>::Push(const T& item)
{
if(top==MaxStackSize)
{
std::cerr<<"Stack overflow!"<<endl;
exit(1);
}
top++; //栈顶指针增加1
stacklist[top]=item; //将新元素压入栈顶
}
template<class T>
T Stack<T>::Pop(void)
{
T temp;
if(top==-1)
{
std::cerr<<"Attempt to pop an empty stack!"<<endl;
exit(1);
}
temp=stacklist[top];
top--;
return temp;
}
template<class T>
T Stack<T>::peek(void) const
{
if(top==-1)
{
std::cerr<<"Attempt to peek at an empty stack!"<<endl;
exit(1);
}
return stacklist[top];
}
template<class T>
int Stack<T>::StackEmpty(void) const
{
return top==-1;
}
template<class T>
int Stack<T>::StackFull(void) const
{
return top==MaxStackSize-1;
}
template<class T>
void Stack<T>::ClearStack(void)
{
top=-1;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -