fork download
  1. #include <iostream>
  2. #include <stack>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. stack<int> myStack;
  9.  
  10. // A) Add at least 10 elements into the stack
  11. vector<int> elements = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  12. for (int element : elements) {
  13. myStack.push(element);
  14. }
  15.  
  16. // B) Remove even numbers from the stack
  17. stack<int> tempStack;
  18. while (!myStack.empty()) {
  19. int topElement = myStack.top();
  20. myStack.pop();
  21. if (topElement % 2 != 0) {
  22. tempStack.push(topElement);
  23. }
  24. }
  25. myStack = tempStack; // Restore the stack with only odd numbers
  26.  
  27. // C) Display the content of the stack
  28. cout << "Stack content: ";
  29. while (!myStack.empty()) {
  30. cout << myStack.top() << " ";
  31. myStack.pop();
  32. }
  33. cout << endl;
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Stack content: 1 3 5 7 9 11