Home / Data structures and Algorithms by Java Examples / Stacks / Linked List Stack Implementation using JAVA Example
Linked List Stack Implementation using JAVA Example
1999 views.
LinkedListBasedImplementation.java
//LinkedList Node: Singly linked list Implementation
class Node {
    private int data;
    private Node next;
    
    Node(int data) {
        this.data = data;
    }
    
    public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
}

//push, pop, top, size, isEmpty, toString
class LinkedListStack {
    private Node top;
    private int length;
    
    public int size() {
        return length;
    }
    
    public boolean isEmpty(){
        return length == 0;
    }
    
    public void push(int data) {
        
        //Creating new node
        Node node = new Node(data);
        
        //Setting current node to ndex
        node.setNext(top);
        
        //New node in the top
        top = node;
        
        //Increasing the length of the list
        length++;
    }
    
    public int pop() throws Exception {
        if (isEmpty()) {
            throw new Exception("Stack is empty!");
        }
        
        //reading data from top element
        int data = top.getData();
        
        //pointing next to top
        top = top.getNext();
        
        //reducing length
        length--;
        
        //returning data
        return data;
    }
    
    public int top() throws Exception {
        if (isEmpty()) {
            throw new Exception("Stack is empty!");
        }
        
        return top.getData();
    }
    
    public String toString(){
        String data = "[";
        
        if (!isEmpty()) {
            data += top.getData();
            
            Node temp = top;
            while (temp.getNext() != null) {
                temp = temp.getNext();
                data += "," + temp.getData();
            }
        }
        
        data += "]";
        return data;
    }
}

/*
Example program for Custom Linked List based implementation using JAVA
*/
public class LinkedListBasedImplementation {
    public static void main(String[] args) throws Exception {
        //Creating new stack instance
        LinkedListStack stack = new LinkedListStack();
        
        //Check stack is Empty
        System.out.println("isEmpty: "+stack.isEmpty());
        
        //Inserting new elements
        stack.push(10);
        stack.push(20);
        stack.push(30);
        stack.push(40);
        stack.push(50);
        
        //Display stack
        System.out.println("Stack: "+stack);
        
        //Print size
        System.out.println("Size: "+stack.size());
        
        //Check stack is Empty
        System.out.println("isEmpty: "+stack.isEmpty());    
        
        //Reading top element
        System.out.println("Top: "+stack.top());
        
        //Pop element
        System.out.println("Pop: "+stack.pop());
        
        //Display stack
        System.out.println("Stack: "+stack);
        
        //Print size
        System.out.println("Size: "+stack.size());
    }
}
Output
isEmpty: true
Stack: [50,40,30,20,10]
Size: 5
isEmpty: false
Top: 50
Pop: 50
Stack: [40,30,20,10]
Size: 4
Related Examples
   Simple Array Stack Implementation using JAVA Example
   Dynamic Array Stack Implementation using JAVA Example
   Linked List Stack Implementation using JAVA Example
Copyright © 2016 Learn by Examples, All rights reserved