Skip to content

Using Java LinkedList as Stack

1. Java Stack – Introduction

The Java Stack operates on ‘Last In First Out (LIFO)’ principle. In the past example, we used the Linked list 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 a stack.

2. Java Stack Methods

Let us say, our linked list is having three elements in it. This is shown below:

Initial-Deque-Linked-List
Initial-Deque-Linked-List

2.1 The Push Method

The push method of the Deque interface adds an item towards the front of the LinkedList. In our case, if we add a new element E4 by using the push method of the Deque interface, the element stays as a head element in the Linked List. Note, push is the commonly used method for Stack in other programming languages. This is shown below:

Add Element to a Stack via push Method
Add Element to a Stack via push Method

2.2 the pop method

The pop method removes the element from the top of the stack. In our case, the method removes the head item from the LinkedList. This means, after the call, the Element E4 will be removed and E1 becomes the new head item. The below picture shows this:

Remove Element to a Stack via pop Method
Remove Element to a Stack via pop Method

2.3 The peek method

The peek method is used to get the head element from the LinkedList means top element from the stack. Unlike the pop method, the peek method will not remove the element from the data structure. We can use this method to inspect the element from the top of the stack.

3. Java Stack Example

3.1 Product Class

As in our previous examples, we will use the Product class as the collection item. You can refer the code for it from the ArrayList Example.

3.2 Create LinkedList & Deque

In the below code, we create a LinkedList and use the Deque interface to refer it. We will use this Deque interface to operate our linked list as a stack.

3.3 Create Six Products

Next, we create six products which acts as the collection items for our linked list. Note, here in this example, we will treat this linked list as a stack. Below is code:

3.4 Add Items Using push Method

In our example, we add all six products created in the previous step to our linked list using the push method. Note, since we use the push method, Java treats our linked list as Stack and adds the element in the list’s front. This means, 106, Sharpner is the head element. The next element will be 105, Clips. Below is the code:

3.5 Inspect Element Via peek Method

We know that the head element right now in our example is “Sharpner”. The peek method will get that element to caller. Note, the peek method will not remove the item# 106 from the front of the LinkedList. Below is the code snippet for peek method call:

3.6 Code Snippet for pop Method

The below code snippet shows how we use the pop method. We called the method three times and hence the products in the front of the linked list will be removed. Note that unlike the peek method, the pop will remove the items from the data structure.

You can watch this entire example as a You Tube demo below.

Using LinkedList as Stack

Categories

Java

Tags

, , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: