fork download
  1. #include <iostream>
  2. #include <stack> // Required for using the stack container
  3. using namespace std;
  4.  
  5. int main() {
  6. // 1. Initialize an empty stack of integers
  7. stack<int> s;
  8.  
  9. // Push elements: 10, 20, 30
  10. s.push(10);
  11. s.push(20);
  12. s.push(30);
  13.  
  14. // Current State Check
  15. cout << "Initial Stack Size: " << s.size() << endl; // Output: 3
  16. cout << "Initial Top Element: " << s.top() << endl; // Output: 30
  17.  
  18. // pop(): Removes the top element (30) from the stack.
  19. s.pop();
  20.  
  21. // State Check After Pop
  22. cout << "New Stack Size: " << s.size() << endl; // Output: 2
  23. cout << "New Top Element: " << s.top() << endl; // Output: 20
  24.  
  25. // empty(): Checks if the stack has any elements.
  26. cout << s.empty() << endl; // Output: 0(which means false)
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
Initial Stack Size: 3
Initial Top Element: 30
New Stack Size: 2
New Top Element: 20
0