STACK DATA-STRUCTURE
- Gunda SHASHANK
- Aug 30, 2022
- 3 min read
Updated: Sep 8, 2022

Stack is the linear data structure, Followed in a particular order Last In First Out. If the last element is inserted, the first element is removed from the stack. LAST IN FIRST OUT ( LIFO ) OR FIRST IN LAST OUT ( FILO ) is the Principle. Insertion and Deletion on the same top element.

Simply,
Any object can be accessed only from top
Anything can be added to the top.
There are many real-time Examples:
Books are arranged one on one.
Coins one on one.
Note: Top element is accessed first.
STACK AS ADT(ABSTRACT DATA TYPE)
FIRSTLY WHAT IS ADT?
ADTs are simply the behaviour ( Data, Operations, etc) is defined but the implementation is hidden.
In real life, we used to know the specs of mobile but not the implementation in that. Now, let's know the stack as ADT.
When we define the stack as ADT then we are interested to know the operations of the stack but not the implementation of the stack from a user point of view.
Then why do we have to stop? let us dive into the operations in the stack
Primary Operations In The Stack:
push( ): To insert the data into the stack.
pop( ): To delete the last data inserted into the stack.
Secondary Operation In The Stack:
top( ): Returns the last data inserted without removing it.
size( ): Returns the size or the number of elements in the stack.
isfull( ): Return TRUE if the stack is full, else return FALSE .
isfull(): Return TRUE if the stack is full, else return FALSE
IMPLEMENTATION OF STACK
As the stack is the linear data structure, so we can implement the stack using an array
or the link list.
ARRAY IMPLEMENTATION
Refer to the array structure given in below image to understand better
"ARRAY IMAGE
Let us take an array (stack_arr) having 0 to 5 index values for an example, We know that stack_arr[] is an array and now we can perform operations on this array, thinking this stack_arr[] behaves like a stack.
Note: Insertions and Deletion must be performed in a stack.
Steps To Implement :
Select a variable top to keep track on the last inserted element or the topmost element in the array.
To indicate that stack is empty we will set the value of the top as -1.
Note: If the top = 0 then the topmost element is at index 0 and It presents only one element.
For push operation:
To push an element into the stack the top is incremented by 1, Because by default the stack is empty means the top value is -1.
The new element is pushed at position top in that array.
"image how the element is added or pushed
Simply to understand this operation. Consider the array with five index values.
For example, here we can not load an element because -1 is not an acceptable index value, Meanwhile to load or push an element we will increment the top.
let's increment the variable top, So now the value of top is 0 top(0) so it refers the index 0 of the stack_arr it can store or load or push the element into the index.
let's perform some push operations to push the elements into the indexes.
push(2) pushed into index 0
push(3) pushed into index 1
push(5) pushed into index 2
push(10) pushed into index 3
push(11) pushed into index 4
Note: for every push top is incremented and if there is no space in the stack then the push is not possible this is known as overflow.
For Pop Operation:
1. top is decremented
2. The element at the top will be deleted.
Simply to understand this operation. Consider the array with five index values.
1. for example top(5) here 5 index elements will be deleted.
2. let's decrement the variable top, So now the value of top is 4 top(4) so it refers to the index 4 of the array_stack and deletes or pop element.
operations:
pop(2) -- pop index 0
pop(3) -- pop index 1
pop(5) -- pop index 2
pop(10)-- pop index 3
pop(11)-- pop index 4

Comments