头文件:include/Stack.h
#ifndef _STACK_H_
#define
_STACK_H_

#include
<
assert.h
>

namespace
DataStructure

...
{

template<typename DataType>
class Stack

...{
public:
Stack(int StackSize);
~Stack();
bool Pop(DataType* Out);
bool Push(DataType& In);
bool IsEmpty();
bool IsFull();
bool GetTop(DataType* Out);
int Count();
private:
Stack();
Stack(const Stack& rsh);
Stack& operator=(Stack& rsh);
//-----------------------------------------------------------------------------
protected:

private:
const int STACK_SIZE;
int m_Top;
DataType* m_Data;
};

#include "../src/Stack.hpp"

}
//
end namespace
#endif
文件:src/Stack.hpp
template
<
typename DataType
>
Stack
<
DataType
>
::Stack(
int
StackSize):
STACK_SIZE(StackSize),
m_Top(
0
)

...
{
assert(StackSize>0);
m_Data = new DataType[StackSize];
}

template
<
typename DataType
>
Stack
<
DataType
>
::
~
Stack()

...
{
delete[] m_Data;
m_Data = NULL;
}

template
<
typename DataType
>
bool
Stack
<
DataType
>
::Pop(DataType
*
Out)

...
{
if (0==m_Top)

...{
return false;
}
m_Top--;
*Out = m_Data[m_Top];
return true;
}

template
<
typename DataType
>
bool
Stack
<
DataType
>
::Push(DataType
&
In)

...
{
if (m_Top>=STACK_SIZE-1)

...{
return false;
}
m_Data[m_Top] = In;
m_Top++;
return true;
}

template
<
typename DataType
>
bool
Stack
<
DataType
>
::IsEmpty()

...
{
return 0==m_Top;
}

template
<
typename DataType
>
bool
Stack
<
DataType
>
::IsFull()

...
{
return m_Top==STACK_SIZE-1;
}

template
<
typename DataType
>
bool
Stack
<
DataType
>
::GetTop(DataType
*
Out)

...
{
if (0==m_Top)

...{
return false;
}
*Out = m_Data[m_Top];
return true;
}

template
<
typename DataType
>
int
Stack
<
DataType
>
::Count()

...
{
return m_Top-1;
}
调用例子:
#include
"
include/Stack.h
"

using
namespace
DataStructure;

void
Test()

...
{
Stack<int> s(100);
int temp = 1;
if (!s.Push(temp))

...{
printf("栈满!");
return;
}
if (!s.Pop(&temp))

...{
printf("栈空! ");
return;
}
}








































文件:src/Stack.hpp

















































































调用例子:























