fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. // define default capacity of stack
  6. #define SIZE 10
  7.  
  8. // Class for stack
  9. template <class X>
  10. class stack
  11. {
  12. X *arr;
  13. int top;
  14. int capacity;
  15.  
  16. public:
  17. stack(int size = SIZE); // constructor
  18.  
  19. void push(X);
  20. X pop();
  21. X peek();
  22.  
  23. int size();
  24. bool isEmpty();
  25. bool isFull();
  26.  
  27. // destructor
  28. ~stack(){
  29. delete[] arr;
  30. }
  31. };
  32.  
  33. // Constructor to initialize stack
  34. template <class X>
  35. stack<X>::stack(int size)
  36. {
  37. arr = new X[size];
  38. capacity = size;
  39. top = -1;
  40. }
  41.  
  42. // function to add an element x in the stack
  43. template <class X>
  44. void stack<X>::push(X x)
  45. {
  46. if (isFull())
  47. {
  48. cout << "OverFlow\nProgram Terminated\n";
  49. exit(EXIT_FAILURE);
  50. }
  51.  
  52. cout << "Inserting " << x << endl;
  53. arr[++top] = x;
  54. }
  55.  
  56. // function to pop top element from the stack
  57. template <class X>
  58. X stack<X>::pop()
  59. {
  60. // check for stack underflow
  61. if (isEmpty())
  62. {
  63. cout << "UnderFlow\nProgram Terminated\n";
  64. exit(EXIT_FAILURE);
  65. }
  66.  
  67. cout << "Removing " << peek() << endl;
  68.  
  69. // decrease stack size by 1 and (optionally) return the popped element
  70. return arr[top--];
  71. }
  72.  
  73. // function to return top element in a stack
  74. template <class X>
  75. X stack<X>::peek()
  76. {
  77. if (!isEmpty())
  78. return arr[top];
  79. else
  80. exit(EXIT_FAILURE);
  81. }
  82.  
  83. // Utility function to return the size of the stack
  84. template <class X>
  85. int stack<X>::size()
  86. {
  87. return top + 1;
  88. }
  89.  
  90. // Utility function to check if the stack is empty or not
  91. template <class X>
  92. bool stack<X>::isEmpty()
  93. {
  94. return top == -1; // or return size() == 0;
  95. }
  96.  
  97. // Utility function to check if the stack is full or not
  98. template <class X>
  99. bool stack<X>::isFull()
  100. {
  101. return top == capacity - 1; // or return size() == capacity;
  102. }
  103.  
  104. // main function
  105. int main()
  106. {
  107. stack<string> pt(2);
  108.  
  109. pt.push("A");
  110. pt.push("B");
  111.  
  112. pt.pop();
  113. pt.pop();
  114.  
  115. pt.push("C");
  116.  
  117. // Prints the top of the stack
  118. cout << "Top element is: " << pt.peek() << endl;
  119.  
  120. // Returns the number of elements present in the stack
  121. cout << "Stack size is " << pt.size() << endl;
  122.  
  123. pt.pop();
  124.  
  125. // check if stack is empty or not
  126. if (pt.isEmpty())
  127. cout << "Stack Is Empty\n";
  128. else
  129. cout << "Stack Is Not Empty\n";
  130.  
  131. return 0;
  132. }
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
Inserting A
Inserting B
Removing B
Removing A
Inserting C
Top element is: C
Stack size is 1
Removing C
Stack Is Empty