C++ Stack
std::stack follows the LIFO (Last In, First Out) principle. Elements are added with push(), removed with pop(), and the top element is accessed with top(). Stacks are useful for undo operations, expression evaluation, and depth-first traversal.
#include <iostream>
#include <stack>
int main() {
std::stack<int> s;
s.push(10);
s.push(20);
s.push(30);
std::cout << "Top: " << s.top() << "\n";
s.pop();
std::cout << "Top after pop: " << s.top();
return 0;
}
Try it Yourself →