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