2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-26 09:39:15 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								It has a list of elements in which an element can be inserted or deleted only from one end ,called top of the stack.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								`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` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Stack LIFO Explanation
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-26 09:39:15 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`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.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### 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 <iostream>       // std::cout
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#include <stack>          // std::stack
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int main ()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::stack< int >  s;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(1);    //Pushing 1 at top of the stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(2);    //Pushing 2 at top of the stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Top
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-26 09:39:15 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Allows you to access the the top element without removing it from your stack.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```cpp
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//Top operation in Stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#include <iostream>       // std::cout
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#include <stack>          // std::stack
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int main ()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::stack< int >  s;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(1);    //Pushing 1 at top of the stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(2);    //Pushing 2 at top of the stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::cout< < s.top ()<<' \n';      // Accessing  the  top  of  the  stack 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::cout< < s.top ()<<' \n';      // Accessing  the  top  of  the  stack 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Pop
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Removes the element on top of the stack, effectively reducing your stack's size by one.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```cpp
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//Pop operation in Stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#include <iostream>       // std::cout
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#include <stack>          // std::stack
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int main ()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::stack< int >  s;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(1);    //Pushing 1 at top of the stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(2);    //Pushing 2 at top of the stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::cout< < s.top ()<<' \n';    // Accessing  the  top  of  the  stack 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.pop();                    //Removing element from the top of stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::cout< < s.top ()<<' \n';    // Accessing  the  top  of  the  stack 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Size
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Returns the number of elements in the `stack` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```cpp
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//Size operation in Stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#include <iostream>       // std::cout
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#include <stack>          // std::stack
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int main ()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::stack< int >  s;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(1);    //Pushing 1 at top of the stack      
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(2);    //Pushing 2 at top of the stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::cout< < s.size ()<<' \n';   // Showing  the  size  of  the  stack 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.pop();                    //Removing element from the top of stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::cout< < s.size ()<<' \n';   // Showing  the  size  of  the  stack 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.pop();                    //Removing element from the top of stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::cout< < s.size ()<<' \n';   // Showing  the  size  of  the  stack 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Empty
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Returns whether the `stack`  is empty ,i.e. whether your stack size is zero.
							 
						 
					
						
							
								
									
										
										
										
											2018-11-26 09:39:15 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-26 07:26:34 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								It returns `true`  if the size of the stack is 0 else returns `false` . An alternative to using this is
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```cpp 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (stackname.size()==0)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```cpp
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//Empty operation in Stack
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#include <iostream>       // std::cout
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#include <stack>          // std::stack
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								int main ()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::stack< int >  s;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  s.push(2);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  while(s.empty() != false){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      std::cout< < s.top ()<<' \n'; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      s.pop();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  std::cout<< "Out of loop"<< '\n';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Output:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Out of loop
							 
						 
					
						
							
								
									
										
										
										
											2018-10-16 10:08:19 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Uses of Stack
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1.  Expression Evaluation and Conversion. 
						 
					
						
							
								
									
										
										
										
											2018-11-26 09:39:15 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								2.  Stacks are used to evaluate and convert expressions like Prefix, Postfix and Infix expression.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								3.  In Recursive functions, Stacks are used to keep information about the active functions or subroutines. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								4.  In Backtracking, as in DFS algorithm. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								5.  Memory management, run-time environment for nested language features. etc