stack.h

来自「栈容器:静态栈:栈容量确定」· C头文件 代码 · 共 44 行

H
44
字号
//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 + =
减小字号Ctrl + -
显示快捷键?