fork download
  1.  
  2. // main.cpp
  3. // CPPSTACK
  4. //
  5. // Created by wpfang on 2021/3/15.
  6. //
  7. #include <iostream>
  8. #include <string>
  9. using namespace std;
  10. template<class T>
  11. class CSTACK
  12. {
  13. private:
  14. T A[5];
  15. int Index;
  16. public:
  17. CSTACK()
  18. {
  19. Index=0;
  20. }
  21. bool isEmpty()
  22. {
  23. /*
  24.   if(0==Index) return true;
  25.   else return false;
  26.   */
  27. //return Index==0;
  28. return !Index;
  29. }
  30. bool isFull()
  31. {
  32. return Index>=5;
  33. }
  34. bool push(T data)
  35. {
  36. if(isFull()) return false;
  37. /*
  38.   A[Index]=data;
  39.   Index = Index+1;
  40.   */
  41. A[Index++]=data;
  42. return true;
  43. }
  44. bool pop(T* data)
  45. {
  46. if(isEmpty()) return false;
  47. /*
  48.   Index--;
  49.   *data=A[Index];
  50.   */
  51. *data=A[--Index];
  52. return true;
  53. }
  54. void show()
  55. {
  56. int i;
  57. for(i=0;i<Index;i++)
  58. cout<<A[i]<<" ";
  59. cout<<endl;
  60. }
  61. };
  62. int main(int argc, const char * argv[]) {
  63. CSTACK<int> s1,s2;
  64. int d;
  65. s1.push(1);s1.show();
  66. s1.push(2);s1.show();
  67. s2.push(100);s2.show();
  68. s1.push(3);s1.show();
  69. s1.pop(&d);s1.show();
  70. s2.push(200);s2.show();
  71. s1.push(4);s1.show();
  72. string temp;
  73. CSTACK<string> s3;
  74. s3.push("apple"); s3.show();
  75. s3.push("pineapple");s3.show();
  76. s3.push("water melon");s3.show();
  77. return 0;
  78. }
Success #stdin #stdout 0.01s 5528KB
stdin
Standard input is empty
stdout
1 
1 2 
100 
1 2 3 
1 2 
100 200 
1 2 4 
apple 
apple pineapple 
apple pineapple water melon