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.

Table of Contents

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.

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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 26 September

    Print all subarrays of a given array

    Table of ContentsProblemSolutionWas this post helpful? 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} […]

  • 08 November

    Dijkstra’s algorithm in java

    Table of ContentsProblemAlgorithmDijkstra’s algorithm exampleWas this post helpful? 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 […]

  • 16 September

    Doubly Linked List in java

    Table of ContentsDoubly linked list in java exampleWas this post helpful? 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 […]

  • 22 October

    Trie data structure in java

    Table of ContentsWhat is Trie :Was this post helpful? 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 […]

  • 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

    Table of ContentsAlgorithm:Java code to convert sorted array to balanced binary search tree:Was this post helpful? 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 […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.