A stack is a non-primitive linear data structure in which element can be inserted or deleted from only one end.
The end that allows addition and deletion operation is known as top of stack or TOS.
As addition and deletion is done only from the top of the stack so the last added element is the one which would first be removed. This makes stack a Last-in-first-out (LIFO) type of list data structure.
Example of stack
Plates in party
In parties we always see plates kept for guests dinners. Refer the diagram above.
Those plates are arranged such that we can only pick one up from the top.
And new plates are also added from the top end only. So we can see both deletion (pick up) and insertion of plates is done from the top end only.
The plate kept last would be the first to be picked. This is the basic concept on which stack works.
Operations on Stacks
From the above example we saw to operations.
New plates are added on the top. This is known as push operation.
Plate at the top are picked up. This is known as pop operation.
When new element is added at the top of the stack then this process is known as PUSH operation.
When element is removed or deleted from the top of the stack then this process is known as POP operation.
If we keep removing element from the stack continuously then a situation will arise when there will be no element left in the stack. This situation is known as stack empty or underflow.
At this scenario, top of the stack is equals to bottom of the stack.
If we keep adding element into the stack continuously then a situation will arise that no more element can be added into it. This situation is known as stack overflow.
At this point top of the size reaches the maximum size of the stack.