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:

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:

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:

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.
1 2 |
//Sample01: Create LinkedList & use it as Stack Deque<Product> Products = new LinkedList<Product>(); |
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:
1 2 3 4 5 6 7 |
//Sample 02: Construct Sample Products Product p1 = new Product(101, "Pen"); Product p2 = new Product(102, "Pencil"); Product p3 = new Product(103, "Rubber"); Product p4 = new Product(104, "Writing Pad"); Product p5 = new Product(105, "Clips"); Product p6 = new Product(106, "Sharpner"); |
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:
1 2 3 4 5 6 7 |
Products.push(p1); Products.push(p2); Products.push(p3); Products.push(p4); Products.push(p5); Products.push(p6); printProducts(Products); |
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:
1 2 3 |
//Sample 05: Examine element at both end Product Head = Products.peek(); System.out.println("Head is : " + Head); |
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.
1 2 3 4 5 |
//Sample 06: Remove Three Head (Stack: LIFO) Products.pop(); Products.pop(); Products.pop(); printProducts(Products); |
You can watch this entire example as a You Tube demo below.
Using LinkedList as Stack
Categories: Java
Tags: LinkedList, peek, pop, push, Stack