fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. //Creating a stack for integers
  7. class Stack {
  8.  
  9. public:
  10. Stack(int capacity) {
  11. this->capacity = capacity;
  12. }
  13. void push(int data) {
  14. if(isFull()) {
  15. cout<<"Full Stack"<<endl;
  16. return;
  17. }
  18. cout<<"Added to Stack: "<<data<<"\n";
  19. container.push_back(data);
  20. }
  21.  
  22. void pop() {
  23. if(isEmpty()) {
  24. cout<<"Empty Stack!";
  25. return;
  26. }
  27. cout<<"Pop: "<<container.back()<<endl;
  28. container.pop_back();
  29. }
  30.  
  31. int top() {
  32. return container[container.size()-1];
  33. }
  34.  
  35. bool isEmpty() {
  36. return container.empty();
  37. }
  38.  
  39. bool isFull() {
  40. return capacity == container.size();
  41. }
  42.  
  43. void display() {
  44. if(isEmpty()){
  45. cout<<endl<<"Empty Stack. Nothing to show!"<<endl;
  46. return;
  47. }
  48. for(int n:container) {
  49. cout<<n<<"\n";
  50. }
  51. }
  52. private:
  53. vector<int> container;
  54. int capacity;
  55. };
  56.  
  57. int main(int argc, char const *argv[]) {
  58. Stack stack(100);
  59. stack.push(1);
  60. stack.push(2);
  61. stack.push(3);
  62. stack.push(4);
  63. stack.pop();
  64. stack.pop();
  65. stack.pop();
  66. stack.pop();
  67. stack.pop();
  68. stack.display();
  69. return 0;
  70. }
Success #stdin #stdout 0.01s 5332KB
stdin
Standard input is empty
stdout
Added to Stack: 1
Added to Stack: 2
Added to Stack: 3
Added to Stack: 4
Pop: 4
Pop: 3
Pop: 2
Pop: 1
Empty Stack!
Empty Stack. Nothing to show!