033.cpp
来自「用C++编写的《算法与程序设计》(王晓东版)的书中的数据结构类(全集)」· C++ 代码 · 共 60 行
CPP
60 行
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
template<class T>
class Stack{
public:
Stack(int Max=100);
~Stack() {delete[] stack;}
bool Empty() const {return top==-1;}
bool Full() const {return top==MaxTop;}
T Top() const;
Stack<T>& Push(const T& x);
Stack<T>& Pop(T& x);
int top;
int MaxTop;
T *stack;
};
template<class T>
Stack<T>::Stack(int Max)
{
MaxTop=Max-1;
stack=new T[Max];
top=-1;
}
template<class T>
T Stack<T>::Top() const
{
if(Empty()) cout<<"it is wrong";
else return stack[top];
}
template<class T>
Stack<T> & Stack<T>::Push(const T& x)
{
if(Full()) cout<<"it is wrong";
stack[++top]=x;
return *this;
}
template<class T>
Stack<T>&Stack<T>::Pop( T& x)
{
if(Empty())cout<<"It is wrong";
x=stack[top--];
return *this;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?