How to Create a LinkList in Java

Today we will learn to create a custom LinkedList Program in Java which will have two basic operations - Adding a node and Deleting a node from the list.

We create a class LinkedNode having a int as value and a next pointer to point to next node in the list.
class LinkedNode {
    protected int value;
    protected LinkedNode next;
    public LinkedNode(int value) {
        this.value = value;
        this.next = null;
    }
    
}
We have a custom class LinkedList which have to three basic operations of a LinkedList -
(1) addNode() - Adding a node to the list 
(2) deleteNode() - Deleting a node to the list. 
(3) printList() - Printing LinkedList.

 Here we are adding a node to the last node of the list.

 Here is the full code in Java. We have a static variable head which always point to the head of the list -
public class LinkedList {

    static LinkedNode head =  null;
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        LinkedNode node = new LinkedNode(1);
        linkedList.addNode(node);
        LinkedNode node2 = new LinkedNode(2);
        linkedList.addNode(node2);
        LinkedNode node3 = new LinkedNode(3);
        linkedList.addNode(node3);
        System.out.println("After adding nodes, the list is -");
        linkedList.printList();
        linkedList.deleteNode(new LinkedNode(2));
        System.out.println("After deleting node 2 if present, the list is -");
        linkedList.printList();

    }

    private void deleteNode(LinkedNode node2) {
        if(head.value == node2.value){
            head = head.next;
            return;
        }else{
            LinkedNode temp1 = head;
            LinkedNode temp = head.next;
            while(temp != null && temp.value != node2.value ){
                temp1 = temp;
                temp = temp.next;
            }
            if(temp == null){
                System.out.println("The node to be delete is not found ");
            }else{
                temp1.next = temp.next;
            }
        }
    }

    private void printList() {
        if(head != null){
            LinkedNode temp = head;
            while(temp != null){
                System.out.print(temp.value + " ");
                temp = temp.next;
            }
            System.out.println();
        }else{
            System.out.println("The List is empty, Please add node elements first");
        }
        
        
    }

    private void addNode(LinkedNode node) {
        if(head == null){
            head = node;
            return;
        }
        else{
            LinkedNode temp = head;
            while(temp.next != null){
                temp = temp.next;
            }
            temp.next = node;
        }
    }

}

class LinkedNode {
    protected int value;
    protected LinkedNode next;
    public LinkedNode(int value) {
        this.value = value;
        this.next = null;
    }
    
}
Hope you like it. Please comment on any doubts. Happy Learning !!!
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment