Hello Viewer! how are you? I hope you are very well. Today I have learned about Stack Data Structure. below I have written whatever I learn today ...
STACK
A Stack is a Linear Data Structure in which insertion and deletion of elements are done at only end, which is known as the "Top of Stack".
Stack is called a Last-in,First-out( LIFO ) structure because the last element which is added to the stack is the 1st element which is deleted from the stack.
In the computer's memory stack can be implemented using arrays or linked lists. fig(3) shows the array implementation of stack.
Every Stack has a variable "top" associated with it.
"top" is used to store the address of the topmost element of the stack.
- It is the position from where the element will be added or deleted.
In fig(3) 👆 , top = 4, so insertion and deletion will be done at this position. Here the Stack can store maximum 6 elements where the indices range from 0-5.
A Stack supports three basic operations: PUSH, POP & PEEP.
The PUSH operation is used to add new element to the "Top Of Stack".
The POP operation is used to remove element from the "Top Of Stack". and
The PEEP operation is used to return the value of "Topmost Stack" without deleting it.
However before inserting new element to the stack we must check for "Overflow" and "Underflow" conditions.
Overflow :- An "Overflow" occurs when we try to insert an element into a stack that is already full.
Similarly "Underflow" occurs when we try to delete an element from the Stack which is already empty.
We will discuss more about Stack operations & implementation after completion of all basic of Data structure.