Interpretation of c ++ stack template application code

Source: Internet
Author: User

We will introduce the basic operations on the C ++ stack template in detail today. I hope that my friends who have just been using this language can fully master the application skills through the interpretation of this article to facilitate our practical application.

C ++ stack template declaration

 
 
  1. template <class T> 
  2. struct StackNode  
  3. {  
  4. T typeData;  
  5. StackNode *nextNode;  
  6. };  
  7. template <class T> 
  8. class DZ_Stack   
  9. {  
  10. public:  
  11. bool Push(const T data);  
  12. bool Pop(T& data);   
  13. bool Peek(T& data);  
  14. bool IsEmpty();   
  15. DZ_Stack();  
  16. virtual ~DZ_Stack();  
  17. private:  
  18. StackNode<T> *pStackTop;  
  19. int iNodeCount;  
  20. }; 

C ++ stack template implementation

 
 
  1. template <class T> 
  2. DZ_Stack<T>::DZ_Stack()  
  3. {  
  4. pStackTop=NULL;  
  5. iNodeCount=0;  
  6. }  
  7. template <class T> 
  8. DZ_Stack<T>::~DZ_Stack()  
  9. {  
  10. while(!IsEmpty())  
  11. {  
  12. StackNode<T> *pStackNode= pStackTop;  
  13. pStackToppStackTop=pStackTop->nextNode;  
  14. delete (pStackNode);  
  15. pStackNode=NULL;  
  16. }  
  17. pStackTop=NULL;  
  18. }  
  19. template <class T> 
  20. bool DZ_Stack<T>::Push(const T data)  
  21. {   
  22. StackNode<T> *pStackNode=new StackNode<T>;  
  23. if( NULL == pStackNode )   
  24. return false;  
  25. pStackNode->typeData=data;  
  26. pStackNode->nextNode=pStackTop;  
  27. pStackTop=pStackNode;  
  28. iNodeCount++;  
  29. return true;  
  30. }  
  31. template <class T> 
  32. bool DZ_Stack<T>::Pop(T& data)  
  33. {   
  34. if ( IsEmpty() )  
  35. return false;  
  36. data=pStackTop->typeData;  
  37. StackNode<T> *pStackNode= pStackTop;  
  38. pStackToppStackTop=pStackTop->nextNode;  
  39. delete (pStackNode);  
  40. iNodeCount--;  
  41. return true;   
  42. }  
  43. template <class T> 
  44. bool DZ_Stack<T>::Peek(T& data)  
  45. {   
  46. if (IsEmpty())  
  47. return false;  
  48. data=pStackTop->typeData;  
  49. return true;   
  50. }  
  51. template <class T> 
  52. bool DZ_Stack<T>::IsEmpty()  
  53. {   
  54. if ( NULL == pStackTop )  
  55. return true;  
  56. else  
  57. return false;  
  58. }  
  59. #endif // !defined(AFX_DZ_STACK_H__10036803_D752_4EF7_
    852D_DD6B377D7AB9__INCLUDED_) 

The preceding section describes the C ++ stack template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.