Stack

A stack is a sequentially ordered data structure that uses the last in, first out (LIFO) principle for adding and removing items, meaning that the last item placed onto a stack is the first item removed. The operations for inserting and removing items from a stack are known as push and pop, respectively. An operating system often uses a stack when invoking function calls. Parameters, local variables, and the return address are pushed onto the stack when a function is called; returning from the function call pops those items off the stack.

Implementation

C++

source: https://www.geeksforgeeks.org/stack-in-cpp-stl/

`#include <iostream>`

`#include <stack>`

`using` `namespace` `std;`

`int` `main() {`

    `stack<``int``> stack;`

    `stack.push(21);`

    `stack.push(22);`

    `stack.push(24);`

    `stack.push(25);`

         `stack.pop();`

    `stack.pop();`

    `while` `(!stack.empty()) {`

        `cout << stack.top() <<``" "``;`

        `stack.pop();`

    `}`

`}`