How to Implement Stack Data Structure In Java

Stack, Deque Implementations in Java to achieve L.I.F.O

Suraj Mishra
Javarevisited

--

Originally Published in https://asyncq.com/

Introduction

  • There are many ways to store and operate over data. For different situations, we use different data structures that are perfect or near-perfect for the intended operation.
  • One such operation that is very common in software development is the Last In First Out operation or L.I.F.O as it is popularly known.
    The data structure itself is called a Stack and the operation that it supports is L.I.F.O.

Use Case

  • L.I.F.O behavior can be seen in real life as well as in computer programming. A stack of plates is a very good example where the last dish will be placed at the top and taken out from the top as well.
  • In computer programming, recursion is a good example of stack data structure. As we call functions recursively they are being stacked and executed in reverse order.
  • Other would-be undo operations in software. Consider games such as Chess or Sudoku which allow us to undo up-to certain moves or paint that allows us to go back, they both behave as L.I.F.O order.

Let’s see how can we achieve L.I.F.O behavior in Java.

Implementation In Java

  • In this article, we will discuss two Stack implementations in java.

--

--

Suraj Mishra
Javarevisited

Staff Software Engineer @PayPal ( All opinions are my own and not of my employer )