Using Java LinkedList as Stack

Remove Element to a Stack via pop Method

The Java Stack operates on ‘Last In First Out (LIFO)’ principle. In the past example, we used the LinkedList as Queue which operated on ‘First In First Out’. Let us say, the Hotel server is keeping the coffee cups on top of each one. When he serves the coffee to the tables, he will take out the cup from the top. This means, the person will take out cup which he placed last. This shows the LIFO principle. The same way Stack works in Java.

Java Collection does not provide Stack Interface. We can use the Deque interface which adds element in the front (or we can say Top) and removes element from the front using special methods of stack. In this example, we are going to use our Linked List of Products as stack.

Read More