?? stack.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 + -