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

📄 stack.h

📁 包括一个LR(1)的语法分析程序和一个LL(1)的语法分析程序的例子
💻 H
字号:
#ifndef STACK_DEF
#define STACK_DEF
#include "CommonDef.h"
#include <assert.h>
#define STACK_FULL -2
#define STACK_EMPTY -3
#define STACK_SIZE   100
//****************************************
//描述:自定义的模板堆栈类
//****************************************
template<typename Type>
//备注:如果成员中有指针的话,就应当构造一个Copy Constructor
//否则在函数传递中如果以该类的对象为参数的话,就会发生
//在参数对象的析构函数中将指针释放,而调用的实参通过该指针
//访问内存出现access violation的exception
//现在的解决方案是定义一个较大的栈元素数组
//接下来准备自定义一个Copy Constructor用于对指针进行特殊处理 
class Stack
{
	public:
		Stack();
		//Stack(long argSize);
		~Stack();
  public:
		//栈初始化函数
		Status InitStack(long  argSize);
		//入栈函数
 		Status Push(Type val);
		//出栈函数
		Status Pop(Type& valOut);
		//获得栈顶元素函数
		Status Top(Type& val);
		//判定当前栈是否为空
		bool   IsEmpty();
  private:
		long m_lSize;   //size of the Stack
		//Type * m_elem;  //stack's buffer 
		Type m_elem[STACK_SIZE];
		long m_lTop;		//specifies the top of the stack
		
};

//stack constructor 
template<typename Type>
Stack<Type>::Stack()
{
	//m_elem = NULL;
	m_lSize = STACK_SIZE;
	m_lTop = 0;
}


/*//该方法暂时弃用,等编写了Copy Constructor后才能使用该函数
template<typename Type>
Stack<Type>::Stack(long argSize)
{
	assert(argSize > 0);
	if (argSize < 0)
	{
		//ErrMsg("Failed to allocate Memory");
	}
	else 
	{
		m_elem = new Type[argSize];
		m_lSize = argSize;
	}
	m_lTop = 0;
}
*/

//stack deconstructor 
template<typename Type>
Stack<Type>::~Stack()
{
	/*//暂不使用,wait For Copy Constructor
	if (m_elem != NULL)
	{
		delete []m_elem;
		m_lSize = 0;
	}
	*/
}

//******************************************************
//Description:Push method
//@arg:  Type val  ----data to be pushed into the stack
//@return Status 
//******************************************************
template<typename Type>
Status Stack<Type>::Push(Type val)
{
	if (m_lTop < m_lSize)
	{
		m_elem[m_lTop++] = val;
	}
	else
	{
		return STACK_FULL;
	}
	return OK;
}

//******************************************************
//Description:Pop method
//@arg:  Type& val  ----data to be used to hold the data to be poped
//@return Status 
//******************************************************
template<typename Type>
Status Stack<Type>::Pop(Type& valOut)
{
	if (m_lTop <= 0)
	{
		return STACK_EMPTY;
	}
	valOut = m_elem[--m_lTop];
	return OK;
}

//******************************************************
//Description:Top method
//@arg:  Type& val  ----data to be used to hold the data on top of the stack
//@return Status 
//******************************************************
template<typename Type>
Status Stack<Type>::Top(Type& val)
{
	if (m_lTop <= 0)
	{
		return STACK_EMPTY;
	}
	val = m_elem[m_lTop - 1];
	return OK;
}

/*//暂不使用,Wait for Copy Constructor
//******************************************************
//Description:InitStack method
//@arg:  long argSize  ---stack's initializing size
//@return Status 
//******************************************************
template<typename Type>
Status Stack<Type>::InitStack(long argSize)
{
	if (argSize <= 0)
	{
		//ErrMsg("argSize is less than 0 ");
		return FAIL;
	}
	m_elem = new Type[argSize];
  if (NULL == m_elem)
	{
		//ErrMsg("allocate memory for stack failed");
		return FAIL;
	}
	m_lTop = 0;
	m_lSize = argSize;
	return OK;
}
*/
//******************************************************
//Description:IsEmpty  Method
//@return bool
//******************************************************
template<typename Type>
bool Stack<Type>::IsEmpty()
{
	return m_lTop == 0;
}
#endif

⌨️ 快捷键说明

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