What is Stack data structure
A stack is a Linear data structure, used to store the data effectively and efficiently.
It follows Last in First Out(LIFO). Elements are inserted and deleted from the top of a stack.
Let us consider an example of dishes .Here we add the dishes from the top i.e. considered as push and we remove the dishes from the top i.e considered as push
The stack stores different types of the Elements such as strings, integers, etc.
It provides different operations or methods to work with the stack
The stack can be implemented using Array
or LinkedList
Stack Operations
Stack contains the following basic operations
push
: Adds an element to a Stackpop
: Removes an element from a stacktop
: Returns the top element of a stackisEmpty
: true, if the stack is empty, false if the stack is not emptysize
: Returns the size of an element
Stack vs Queue
Stack | Queue |
---|---|
Last In First Out | First In First Out |
Linear Data structure | Linear Data structure |
Add and delete elements from the top of a list | Add and delete elements from both sides. Adds elements to rear list, Delete an element from the |
Front of a list | |
Add an element to Stack called Push | Add an element to Queue called Enqueue |
Removes an element from a stack called Pop | Removes an element is called Dequeue |
Recursion used for Data storage | sequential processing for data process |