Stack sacrifice the flexibility of other data structures for more systematic interactions eith the data.
It mimic a physical stack and operate in a LIFO context.
Each elements added to the stack is both the top and bottom of the stack.
The only element that can be removed is the top one.
Include the stack library.
#include <stack>
Create with:
std::stack[
std::stack<int> my_numbers;
The type cannot be changed after declaration.
Use .push() to insert a new element at the top.
my_numbers.push(2);
my_numbers.push(6);
Use .pop() to removes the top element.
my_numbers.pop();
Use .top() to access the top element (only element accessible).
my_numbers.top();
Use .size() to get the number of elements in a stack.
std::cout << my_numbers.size();
Use the .empty() function to know if a stack is empty or not.
Return 0 if empty, 1 otherwise.