我们今天将会为大家详细介绍一下有关C++链栈模板的基本操作方法。希望刚刚基础这款语言的朋友们可以通过对这篇文章的解读,充分的掌握这方面的应用技巧,以方便我们在实际应用中熟练应用。
C++链栈模板声明
- template <class T>
- struct StackNode
- {
- T typeData;
- StackNode *nextNode;
- };
- template <class T>
- class DZ_Stack
- {
- public:
- bool Push(const T data);
- bool Pop(T& data);
- bool Peek(T& data);
- bool IsEmpty();
- DZ_Stack();
- virtual ~DZ_Stack();
- private:
- StackNode<T> *pStackTop;
- int iNodeCount;
- };
C++链栈模板实现
- template <class T>
- DZ_Stack<T>::DZ_Stack()
- {
- pStackTop=NULL;
- iNodeCount=0;
- }
- template <class T>
- DZ_Stack<T>::~DZ_Stack()
- {
- while(!IsEmpty())
- {
- StackNode<T> *pStackNode= pStackTop;
- pStackToppStackTop=pStackTop->nextNode;
- delete (pStackNode);
- pStackNode=NULL;
- }
- pStackTop=NULL;
- }
- template <class T>
- bool DZ_Stack<T>::Push(const T data)
- {
- StackNode<T> *pStackNode=new StackNode<T>;
- if( NULL == pStackNode )
- return false;
- pStackNode->typeData=data;
- pStackNode->nextNode=pStackTop;
- pStackTop=pStackNode;
- iNodeCount++;
- return true;
- }
- template <class T>
- bool DZ_Stack<T>::Pop(T& data)
- {
- if ( IsEmpty() )
- return false;
- data=pStackTop->typeData;
- StackNode<T> *pStackNode= pStackTop;
- pStackToppStackTop=pStackTop->nextNode;
- delete (pStackNode);
- iNodeCount--;
- return true;
- }
- template <class T>
- bool DZ_Stack<T>::Peek(T& data)
- {
- if (IsEmpty())
- return false;
- data=pStackTop->typeData;
- return true;
- }
- template <class T>
- bool DZ_Stack<T>::IsEmpty()
- {
- if ( NULL == pStackTop )
- return true;
- else
- return false;
- }
- #endif // !defined(AFX_DZ_STACK_H__10036803_D752_4EF7_
852D_DD6B377D7AB9__INCLUDED_)
以上就是对C++链栈模板的相关介绍。
【编辑推荐】