stack.h
来自「二叉树」· C头文件 代码 · 共 84 行
H
84 行
#ifndef STACK
#define STACK
template<class T>
class Stack
{
public:
Stack(int n=10)
{
top=-1;
setCapacity(n);
pool=new T[capacity];
}
~Stack()
{
if(pool!=0)
delete[] pool;
}
void setCapacity(int n) { capacity=n; }
void clear(); //清空栈。
bool isEmpty(); //判断栈是否为空。
bool isFull(); //判断是否满
bool push( T el); //将元素el放到栈的顶部。
bool pop(T& el); //取出栈顶部的元素。
T topEl(); //获取栈顶部的元素,但不删除该元素。
private:
T* pool;
int capacity;
int top;
};
template<class T>
void Stack<T>::clear() //清空栈
{
top=-1;
}
template<class T>
bool Stack<T>::isEmpty() //判断栈是否为空。
{
return top==-1;
}
template<class T>
bool Stack<T>::isFull()
{
return top==capacity-1;
}
template<class T>
bool Stack<T>::push(T el) //将元素el放到栈的顶部。
{
if(!isFull())
{
top++;
pool[top]=el;
return true;
}
else
return false;
}
template<class T>
bool Stack<T>::pop(T& el) //取出栈顶部的元素。
{
if(!isEmpty())
{
el=pool[top];
top--;
return true;
}
else
return false;
}
template<class T>
T Stack<T>::topEl() //获取栈顶部的元素,但不删除该元素。
{
return pool[top];
}
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?