Stack implementation in java

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

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

Introduction

Stack is abstract data type which demonstrates Last in first out (LIFO) behavior. We will implement same behavior using Array.
Stack
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.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

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.

Conclusion

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

That’s all about Stack implementation in java.


import_contacts

You may also like:

Related Posts

  • 26 September

    Print all subarrays of a given array

    If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm programs. In this post, we will see how to print all subarrays of given array. Problem Print all print all subarrays of given array. For example: If array is {1,2,3} then you need to print {1}, […]

  • 08 November

    Dijkstra’s algorithm in java

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see Dijkstra algorithm for find shortest path from source to all other vertices. Problem You will be given graph with weight for each edge,source vertex and you need to find minimum distance from […]

  • 16 September

    Doubly Linked List in java

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see about Doubly LinkedList implementation in java. We have already seen the implementation of singly linked list. You can consider this as an extension of Singly linked list.It is quite complex to implement […]

  • 22 October

    Trie data structure in java

    If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post, we will see about trie data structure in java. What is Trie : Trie is data structure which stores data in such a way that it can be retrieved faster and improve the performance. […]

  • 12 October

    Implement Queue using Linked List in java

    If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post , we will see how to implement Queue using Linked List in java. Queue is abstract data type which demonstrates First in first out (FIFO) behaviour. We will implement same behaviour using Array. Although java provides implementation […]

  • 10 October

    Convert sorted array to balanced binary search tree

    If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post ,  we will see how to convert sorted array to balanced binary search tree. Algorithm: You might know that inorder traversal of binary search tree results in sorted array. We will use this property […]

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.