Advertisement
wendy890711

190927

Sep 27th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template<class T,int Maxsize=10>
  6. class stack{
  7. public:
  8. void init(){ sp = 0; };
  9. void push(T);
  10. void pop();
  11. private:
  12. int sp;
  13. T buffer[Maxsize];
  14. static void Error(){ cout << "\n Stack Error\n" <<endl ; }
  15. };
  16.  
  17. template<class T,int Maxsize=10>
  18. void stack<T, Maxsize>::push(T data)
  19. {
  20. if (sp == Maxsize)
  21. Error();
  22. else
  23. buffer[sp++] = data;
  24. }
  25.  
  26. template<class T,int Maxsize=10>
  27. void stack<T, Maxsize>::pop()
  28. {
  29. if (sp == 0)
  30. Error();
  31. else
  32. cout << buffer[--sp] << endl;
  33. }
  34.  
  35. void main()
  36. {
  37. stack<int> st1;
  38. stack<char> st2;
  39.  
  40. st1.init();
  41.  
  42. st1.push(1);
  43. st1.push(2);
  44. st1.push(3);
  45. st1.push(4);
  46. st1.push(5);
  47. st1.push(6);
  48. st1.push(7);
  49. st1.push(8);
  50. st1.push(9);
  51. st1.push(10);
  52.  
  53. st1.pop();
  54. st1.pop();
  55. st1.pop();
  56. st1.pop();
  57. st1.pop();
  58. st1.pop();
  59. st1.pop();
  60. st1.pop();
  61. st1.pop();
  62. st1.pop();
  63. st1.pop();
  64.  
  65. st2.init();
  66.  
  67. st2.push('a');
  68. st2.push('b');
  69. st2.push('c');
  70. st2.push('d');
  71. st2.push('e');
  72. st2.push('f');
  73. st2.push('g');
  74. st2.push('h');
  75. st2.push('i');
  76. st2.push('j');
  77.  
  78. st2.pop();
  79. st2.pop();
  80. st2.pop();
  81. st2.pop();
  82. st2.pop();
  83. st2.pop();
  84. st2.pop();
  85. st2.pop();
  86. st2.pop();
  87. st2.pop();
  88. st2.pop();
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement