fork download
  1. /* C++ program to implement basic stack
  2.   operations */
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. #define MAX 1000
  7.  
  8. class Stack
  9. {
  10. int top;
  11. public:
  12. int a[MAX]; //Maximum size of Stack
  13.  
  14. Stack() { top = -1; }
  15. bool push(int x);
  16. int pop();
  17. bool isEmpty();
  18. };
  19.  
  20. bool Stack::push(int x)
  21. {
  22. if (top >= (MAX-1))
  23. {
  24. cout << "Stack Overflow";
  25. return false;
  26. }
  27. else
  28. {
  29. a[++top] = x;
  30. return true;
  31. }
  32. }
  33.  
  34. int Stack::pop()
  35. {
  36. if (top < 0)
  37. {
  38. cout << "Stack Underflow";
  39. return 0;
  40. }
  41. else
  42. {
  43. int x = a[top--];
  44. return x;
  45. }
  46. }
  47.  
  48. bool Stack::isEmpty()
  49. {
  50. return (top < 0);
  51. }
  52.  
  53. // Driver program to test above functions
  54. int main()
  55. {
  56. struct Stack s;
  57. s.push(10);
  58. s.push(20);
  59. s.push(30);
  60.  
  61. cout << s.pop() << " Popped from stack\n";
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0s 4472KB
stdin
Standard input is empty
stdout
30 Popped from stack