Feature and Grammatical Corrections (#29581)

* Feature and Grammatical Corrections

Added a feature of stack . 
Made corrections, which were grammatically incorrect.

* Update index.md

* Update index.md
This commit is contained in:
Aaditree
2018-11-26 09:39:15 +05:30
committed by Randell Dawson
parent 2aeb03d17d
commit 690f83f1da

View File

@ -6,13 +6,17 @@ title: stack
`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. `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.
It has a list of elements in which an element can be inserted or deleted only from one end ,called top of the stack.
`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** `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<int> stack_name` 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<int> stack_name`
### Stack LIFO Explanation ### 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. `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") ![alt text](https://github.com/mohammadaziz313/helloworld/blob/master/Lifo_stack.png "LIFO Stack Push and Pop Example")
@ -47,7 +51,7 @@ int main ()
#### Top #### Top
Allows you to access the the top element without removing it from your stack. Allows you to access the the top element without removing it from your stack.
```cpp ```cpp
//Top operation in Stack //Top operation in Stack
@ -135,6 +139,7 @@ int main ()
#### Empty #### Empty
Returns whether the `stack` is empty ,i.e. whether your stack size is zero. Returns whether the `stack` is empty ,i.e. whether your stack size is zero.
It returns `true` if the size of the stack is 0 else returns `false`. An alternative to using this is It returns `true` if the size of the stack is 0 else returns `false`. An alternative to using this is
```cpp ```cpp
if (stackname.size()==0) if (stackname.size()==0)
@ -170,7 +175,7 @@ int main ()
## Uses of Stack ## Uses of Stack
1. Expression Evaluation and Conversion. 1. Expression Evaluation and Conversion.
Stacks are used to evaluate and convert expressions like prefix, postfix and infix expression. 2. Stacks are used to evaluate and convert expressions like Prefix, Postfix and Infix expression.
2. In Recursive functions to keep information about the active functions or subroutines. 3. In Recursive functions, Stacks are used to keep information about the active functions or subroutines.
3. In Backtracking, as in DFS algorithm. 4. In Backtracking, as in DFS algorithm.
4. Memory management, run-time environment for nested language features. etc 5. Memory management, run-time environment for nested language features. etc