/*********************************************
文件名: Stack
编写者: Aill
编写日期: 2012/10/14
********************************************/
#include <iostream>
using namespace std;
template <class T>
class Stack
{
public:
Stack();
~Stack();
public:
int Push(T PushData); //压栈,返回值返回栈的长度
int Pop(T *PopData); //弹出栈,形参返回栈顶值,返回值返回栈的长度,
void ClearStack(void); //清空栈
int GetStackLenght(void); //取栈的长度
int GetStackSize(void); //取栈的大小
private:
typedef class Node
{
public:
Node(T Data, Node *pNext = NULL){
m_pNext = pNext;
m_Data = Data;
}
public:
Node *m_pNext;
T m_Data;
}Node, *pNode;
private:
pNode m_pTop;
int m_iLenght;
};
template <class T>
Stack<T>::Stack()
{
m_pTop = NULL;
m_iLenght = 0;
}
template <class T>
Stack<T>::~Stack()
{
if (0 == m_iLenght)
return;
pNode pTemp;
for (int i = 0; i < m_iLenght; i++)
{
pTemp = m_pTop;
m_pTop = m_pTop->m_pNext;
delete pTemp;
}
m_iLenght = 0;
m_pTop = NULL;
}
template <class T>
int Stack<T>::Push(T PushData)
{
m_pTop = new Node(PushData, m_pTop);
if (NULL == m_pTop)
return 0;
m_iLenght++;
return m_iLenght;
}
template <class T>
int Stack<T>::Pop(T *PopData)
{
if (0 == m_iLenght)
{
return 0;
}
pNode pTemp;
*PopData = m_pTop->m_Data;
pTemp = m_pTop;
m_pTop = m_pTop->m_pNext;
delete pTemp;
m_iLenght--;
return m_iLenght;
}
template <class T>
void Stack<T>::ClearStack(void)
{
if (0 == m_iLenght)
return ;
pNode pTemp;
for (int i = 0; i < m_iLenght; i++)
{
pTemp = m_pTop;
m_pTop = m_pTop->m_pNext;
delete pTemp;
}
m_iLenght = 0;
m_pTop = NULL;
}
template <class T>
int Stack<T>::GetStackLenght(void)
{
return m_iLenght;
}
template <class T>
int Stack<T>::GetStackSize(void)
{
return m_iLenght * sizeof(T);
}
int main(void)
{
//Test Stack !
return 0;
}
栈模板
最新推荐文章于 2022-03-10 18:28:55 发布