Stack implementation in java

If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm interview questions.

In this post, we will see how to implement Stack using Array in java.


Stack is abstract data type which demonstrates Last in first out (LIFO) behavior. We will implement same behavior using Array.
Although java provides implementation for all abstract data types such as Stack,Queue and LinkedList but it is always good idea to understand basic data structures and implement them yourself.
Please note that Array implementation of Stack is not dynamic in nature. You can implement Stack through linked list for dynamic behavior.

Stack basic operations

Stack supports following basic operations.

push: Push element to the top of the Stack.This operation will increase size of stack by 1.
pop: Remove element from the top of the Stack and returns the deleleted Object.This operation will decrease size of stack by 1.
isEmpty: Check if Stack is empty or not.
isFull: Check if Stack is full or not.
peek: Returns top element from the stack without removing it.

Please note that time complexity of all above operation is constant i.e. O(1)

Stack implementation using Array

When you run above program, you will get below output:

Stack is empty !
Pushed element:10
Pushed element:30
Pushed element:50
Pushed element:40
Popped element :40
Popped element :50
Popped element :30

As you can see we have pushed 40 in last, so it is popped first as Stack is of Last In First Out(LIFO) nature.


You have learnt about Stack, its basic operations and stack implementation in java using array.

That’s all about Stack implementation in java.


You may also like:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.