📄 stack.cpp
字号:
//STACK.CPP
#include<iostream.h>
#include<assert.h>
#include <stdlib.h>
#include <string.h>
#include <afx.h>
///////////////////////////////////////////////////////////
template<class Type>class Stack
{
private:
int top;
Type *elems;
int Maxsize;
public:
Stack(int s);
~Stack(){ delete []elems; }
void Push(const Type &);//入栈
Type Pop();//出栈
Type Gettop();//取栈顶
void MakeEmpty() { top=-1; }//置空
int IsEmpty() const { return top==-1; };
int IsFull() const { return top==Maxsize-1; }
friend ostream & operator << (ostream &out,Stack<Type> &ob);
int Qutop();
Type Quelems(int i);
};
///////////////////////////////////////////////////////////
template<class Type>
Type Stack<Type>::Quelems(int i)
{
return elems[i];
}
template<class Type>
int Stack<Type>::Qutop()
{
return top;
}
template<class Type>
Stack<Type>::Stack(int s):top(-1),Maxsize(s)
{
elems=new Type[Maxsize];
assert(elems!=0);
}
///////////////////////////////////////////////////////////
template<class Type>
void Stack<Type>::Push(const Type &item)
{
assert(!IsFull());
elems[++top]=item;
}
///////////////////////////////////////////////////////////
template<class Type>
Type Stack<Type>::Pop()
{
assert(!IsEmpty());
return elems[top--];
}
///////////////////////////////////////////////////////////
template<class Type>
Type Stack<Type>::Gettop()
{
assert(!IsEmpty());
return elems[top];
}
///////////////////////////////////////////////////////////
template<class Type>
ostream & operator << (ostream &out,Stack<Type> &ob)
{
out<<"运算符栈: ";
for(int i=0;i<=ob.top;++i)
{
out<<" "<<ob.elems[i]<<" ";
}
out<<'\n';
if(ob.IsEmpty()) out<<"NULL";
return out;
}
///////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -