--- title: stack --- ## Stacks `stack` is one of the most used containers in C++. A container is a data structure that stores a collection of objects, some in order, some not. All containers have a different set of functions that allow you to access an object(s) in that collection. `std::stack` is part of the C++ standard library (hence the prefix `std::`) and allows you to store data in Last In First Out (LIFO) order. NOTE: **All objects within a stack must be of the same data type** The data type you store within a stack goes within angle brackets next to the stack keyword. For example, if you would like to store a collection of integers the stack would be `std::stack stack_name` ### Stack LIFO Explanation `stack` allows us to push and pop in specific order. **Push** means inserting an object at the top of the stack. **Pop** means pulling out the last inserted object from the top of the stack. So when you push it is at the top and when you pop you extract the last inserted element. ![alt text](https://github.com/mohammadaziz313/helloworld/blob/master/Lifo_stack.png "LIFO Stack Push and Pop Example") ### Stack Operations The stack container supports the following operations: - push - pop - empty - size - back #### Push Allows you to insert a new element at the top of the stack, above the current top element. ```cpp //Push operation in Stack #include // std::cout #include // std::stack int main () { std::stack s; s.push(1); //Pushing 1 at top of the stack s.push(2); //Pushing 2 at top of the stack return 0; } ``` #### Top Allows you to access the the top element without removing it from your stack. ```cpp //Top operation in Stack #include // std::cout #include // std::stack int main () { std::stack s; s.push(1); //Pushing 1 at top of the stack s.push(2); //Pushing 2 at top of the stack std::cout< // std::cout #include // std::stack int main () { std::stack s; s.push(1); //Pushing 1 at top of the stack s.push(2); //Pushing 2 at top of the stack std::cout< // std::cout #include // std::stack int main () { std::stack s; s.push(1); //Pushing 1 at top of the stack s.push(2); //Pushing 2 at top of the stack std::cout< // std::cout #include // std::stack int main () { std::stack s; s.push(1); s.push(2); while(s.empty() != false){ std::cout<