CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <stack>
int main() {
std::stack<int> s;
// Example stack elements
s.push(10);
s.push(20);
s.push(30);
s.push(40);
// If the stack is empty
if (s.empty()) {
std::cout << "Stack is empty" << std::endl;
return 0;
}
// Temporary stack to reverse the original stack
std::stack<int> tempStack;
// Transfer elements to the temporary stack
while (!s.empty()) {
tempStack.push(s.top());
s.pop();
}
// The top element of the temporary stack is the first element of the original stack
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run