In this article, we are going to study to look at the operations performed in stack data structure. Before moving further to detail about each of the operations, we will have a recap on what stack is and the working of stack data structure and move with an example where all the stack operations will be performed over a stack to grasp the conceptual working of stack operations in data structure.
What is Stack?
Stack is a data structure that is based on Abstract Data Type and follows the property of storing elements one by one in an ordered manner with Last in First Out (LIFO) principle that appends or inserts the element at the top of the stack.
A stack can have a size but in most of the programming languages, stacks are implemented on dynamic arrays that are flexible in nature and resize themselves according to increase in size. Here is an illustration that can help us understand the working on the stack
Stacks Operations in Data Structure can be implemented using an array as well as a linked list. The reason that stack is an abstract data structure and adding an element at the head (or top) or linked list can make all the stack operations possible.
Stack Operations in Data Structure
As we have clarity around the basic concepts and working of the stack data structure, we reiterate to the focus of this article on stack operations in data structure. The operations in stack can be given as follow:-
Stack operation performed to append or insert an element to the stack. The element is inserted, or pushed at the top and the size of the stack is incremented by one after performing this operation.
Stack operation performed to remove an element from the stack. The element is removed, or popped from the top and the size of the stack is incremented by one after performing this operation.
Stack operation to get the element stored at the top of the stack. The element is not removed using this stack operation.
Stack operation to get a boolean result, True or False stating if the stack is empty or not. Such that if there are no elements present in stack, it returns a True else False
Similar to isEmpty , it is a stack operation to get a boolean result, True or False stating if the stack is filled or not. Such that if there are n elements present in stack, n being the Maximum Size of the stack, it returns a True else False.
It is a stack operation in data structure, stack, to extract the total number of elements present in it.
Note that, all the programming languages might have syntactic changes in implementing these stack operations, but the idea and working of these stack operations remains the same universally.
Examples of Stack Operations
Consider, we have an array with its maximum size set as 5, we perform the set of operations as mentioned below as trace step-by-step:-
We create a Stack named p and perform the stack operations in data structure as mentioned.
p.push(20) – 20 is pushed into the stack.
p.push(35) – 35 is pushed into the stack.
p.pop() – the top element i.e. 35 will be popped from the stack.
print(p.top()) – The top element which also turns out to be the lone element is stack is printed without altering it.
p.push(40) – 40 is pushed into the stack as the size of stack is not equal to max size of stack.
print(p.size()) – It prints the total number of elements present in the stack. i.e. 2
p.pop() – The peek element is removed from the stack .i.e 40.
print(isEmpty()) – It checks if the stack is empty or not, in this case, there is already an element, 20 present in the stack. Therefore, False will be returned.
Underflow and Overflow of Stack
On instances, when we keep pushing elements to our stack, there is a size of the stack that acts as a threshold, if we push beyond that, we get a stack overflow. Stack Overflow is an error that arises when we cannot store as the limit of stack is already filled.
Similarly, in an empty stack, it is not possible to pop the elements anymore and upon doing so, we get a stack underflow error. Hereby, Stack Underflow justifies that we cannot pop anymore elements from the stack.
To rectify and get rid of these errors, stack operations in data structure, isEmpty() and isFull() help us avoid them when performing push or pop on the stack.
In this article, we started with an introduction to stack data structure and understood the stack operations one by one by tracing them in a live example. Moreover, we studied how the stack overflow and stack underflow errors can be curbed using other stack operations available to us.
We hope you liked this article on stack operations and hope to see you again with an insightful piece of information.
FAQs Related to Stack
1. What stack operations in data structure are used to avoid underflow and overflow?
isEmpty and isFull tell us to push or pop in a stack and avoid such cases of error.
2. What does the size stack operation do?
Size returns the total number of elements present in the stack and must not be confused with the maximum number of elements or limit of the stack.