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

📄 stack.h

📁 栈容器:静态栈:栈容量确定
💻 H
字号:
//Stack.h
#ifndef STACK
#define STACK

#include <iostream>
using namespace std;

template <class T,int MAXSIZE>
class Stack
{//说明: m_top指向栈顶元素的下一个位置
public:
	Stack():m_top(0){}
	void Push(const T& value)
	{
		if (m_top>=MAXSIZE) throw exception("Stack::Push():full stack");
		m_c[m_top++] = value;
	}
	void Destroy() {m_top==0;}
	void Pop()
	{
		if (m_top<=0) throw exception("Stack::Pop():empty stack");
		m_top--;
	}
	T& Top()
	{
		if (m_top<=0) throw exception("Stack::Top():empty stack");
		return m_c[m_top-1];
	}
	const T& operator [] (int index) const
	{
		if (index<0) throw underflow_error("Stack::operator [] ():underflow error")
		else if(index>=MAXSIZE) throw overflow_error("Stack::operator [] ():overflow error");
		return m_c[index];
	}
	int Size() const {return m_top;}
	bool Full() const {return m_top>=MAXSIZE;}
	bool Empty() const {return m_top<=0;}
private:
	T m_c[MAXSIZE];
	int m_top;
};

#endif//STACK

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -