⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack.h

📁 二叉树
💻 H
字号:


#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -